Skip to content

Commit

Permalink
DOC: make documentation build work with Python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
j-ittner committed Jun 25, 2024
1 parent 695f0fa commit 6f86268
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
6 changes: 3 additions & 3 deletions RELEASE_NOTES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ Python 3.10, and drops support for Python versions.
class of two types
- new function :func:`.get_common_generic_subclass` to retrieve the common generic
subclass of two types
- new function :func:`.get_generic_bases` to retrieve the generic base classes of a
type
- new function :func:`~pytools.typing.get_generic_bases` to retrieve the generic base
classes of a type
- new function :func:`.get_generic_instance` to retrieve the generic instance of a
type
- new function :func:`.get_type_arguments` to retrieve the type arguments of a generic
Expand All @@ -99,7 +99,7 @@ Python 3.10, and drops support for Python versions.
- API: new class :class:`.HTMLStyle` for rendering HTML content with drawers
- API: new function :func:`.is_running_in_notebook` to check if the code is running
in a Jupyter or Colab notebook
- API: new property :attr:`.hex` for :class:`.RgbColor` and :class:`.RgbaColor` to
- API: new property :attr:`.RgbColor.hex` and :attr:`.RgbaColor.hex` to
return the color as a hexadecimal string

- Various adjustments to maintain compatibility with recent Python versions
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ docs = [
"jupyter == 1",
"docutils ~= 0.17",
"xlrd ~= 1.2",
"m2r ~= 0.2"
"m2r ~= 0.2",
"mypy ~= 1.10",
]

[tool.flit.metadata.urls]
Expand Down
19 changes: 0 additions & 19 deletions src/pytools/api/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import numpy as np
import pandas as pd
import typing_inspect

from ._alltracker import AllTracker
from ._decorators import subsdoc
Expand All @@ -20,7 +19,6 @@
__all__ = [
"deprecated",
"deprecation_warning",
"get_generic_bases",
"is_list_like",
"as_collection",
"as_list",
Expand Down Expand Up @@ -439,23 +437,6 @@ def _raise_type_mismatch(
)


def get_generic_bases(class_: type) -> tuple[type, ...]:
"""
Bugfix version of :func:`typing_inspect.get_generic_bases`.
Prevents getting the generic bases of the parent class if not defined for the given
class.
:param class_: class to get the generic bases for
:return: the generic base classes of the given class
"""
bases: tuple[type, ...] = typing_inspect.get_generic_bases(class_)
if bases is typing_inspect.get_generic_bases(super(class_, class_)):
return ()
else:
return bases


#
# Decorators
#
Expand Down
33 changes: 26 additions & 7 deletions src/pytools/sphinx/util/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@
from collections.abc import Callable, Generator, Iterable, Mapping
from inspect import getattr_static
from re import Pattern
from types import FunctionType, MethodType, UnionType
from types import FunctionType, GenericAlias, MethodType, UnionType
from typing import Any, ForwardRef, Generic, TypeVar, Union, cast, get_type_hints

import typing_inspect

from ...api import (
AllTracker,
get_generic_bases,
inheritdoc,
public_module_prefix,
subsdoc,
Expand Down Expand Up @@ -273,9 +272,10 @@ def _class_tag(
def _typevar_name(self, cls: TypeVar) -> str:
if isinstance(cls, TypeVar):
args: list[str] = [
self._class_name_with_generics(c) for c in cls.__constraints__
self._class_name_with_generics(c)
for c in getattr(cls, "__constraints__", ())
]
if cls.__bound__:
if getattr(cls, "__bound__", None):
args.append(f"bound= {self._class_name_with_generics(cls.__bound__)}")
return f'{cls}({", ".join(args)})' if args else str(cls)
else:
Expand All @@ -288,7 +288,7 @@ def _get_generics(self, child_class: type) -> list[str]:
self._typevar_name(arg)
for arg in typing_inspect.get_args(base, evaluate=True)
)
for base in get_generic_bases(child_class)
for base in _get_generic_bases(child_class)
if typing_inspect.get_origin(base) is Generic
)
)
Expand Down Expand Up @@ -640,7 +640,7 @@ def _inner(_subclass: type, _include_subclass: bool) -> Generator[type, None, No
# get the base classes; try generic bases first then fall back to regular
# bases
base_classes: tuple[type, ...] = (
get_generic_bases(_subclass) or _subclass.__bases__
_get_generic_bases(_subclass) or _subclass.__bases__
)

# include the _subclass itself in the list of bases, if requested
Expand Down Expand Up @@ -773,7 +773,7 @@ def _get_parameter_bindings(

superclass_bindings = {
superclass: bindings
for generic_superclass in get_generic_bases(cls)
for generic_superclass in _get_generic_bases(cls)
for superclass, bindings in (
self._get_parameter_bindings(
cls=generic_superclass, subclass_bindings=class_bindings
Expand Down Expand Up @@ -1287,3 +1287,22 @@ def _copy_generic_type_with_arguments(
new_arguments = (*new_arguments[0], *new_arguments[1:])

return copy_with(new_arguments)


def _get_generic_bases(class_: type) -> tuple[type, ...]:
"""
Bugfix version of :func:`typing_inspect.get_generic_bases`.
Prevents getting the generic bases of the parent class if not defined for the given
class.
:param class_: class to get the generic bases for
:return: the generic base classes of the given class
"""
bases: tuple[type, ...] = typing_inspect.get_generic_bases(class_)
if not isinstance(
class_, GenericAlias
) and bases is typing_inspect.get_generic_bases(super(class_, class_)):
return ()
else:
return bases

0 comments on commit 6f86268

Please sign in to comment.