14
14
from collections .abc import Callable , Generator , Iterable , Mapping
15
15
from inspect import getattr_static
16
16
from re import Pattern
17
- from types import FunctionType , MethodType , UnionType
17
+ from types import FunctionType , GenericAlias , MethodType , UnionType
18
18
from typing import Any , ForwardRef , Generic , TypeVar , Union , cast , get_type_hints
19
19
20
20
import typing_inspect
21
21
22
22
from ...api import (
23
23
AllTracker ,
24
- get_generic_bases ,
25
24
inheritdoc ,
26
25
public_module_prefix ,
27
26
subsdoc ,
@@ -273,9 +272,10 @@ def _class_tag(
273
272
def _typevar_name (self , cls : TypeVar ) -> str :
274
273
if isinstance (cls , TypeVar ):
275
274
args : list [str ] = [
276
- self ._class_name_with_generics (c ) for c in cls .__constraints__
275
+ self ._class_name_with_generics (c )
276
+ for c in getattr (cls , "__constraints__" , ())
277
277
]
278
- if cls . __bound__ :
278
+ if getattr ( cls , " __bound__" , None ) :
279
279
args .append (f"bound= { self ._class_name_with_generics (cls .__bound__ )} " )
280
280
return f'{ cls } ({ ", " .join (args )} )' if args else str (cls )
281
281
else :
@@ -288,7 +288,7 @@ def _get_generics(self, child_class: type) -> list[str]:
288
288
self ._typevar_name (arg )
289
289
for arg in typing_inspect .get_args (base , evaluate = True )
290
290
)
291
- for base in get_generic_bases (child_class )
291
+ for base in _get_generic_bases (child_class )
292
292
if typing_inspect .get_origin (base ) is Generic
293
293
)
294
294
)
@@ -640,7 +640,7 @@ def _inner(_subclass: type, _include_subclass: bool) -> Generator[type, None, No
640
640
# get the base classes; try generic bases first then fall back to regular
641
641
# bases
642
642
base_classes : tuple [type , ...] = (
643
- get_generic_bases (_subclass ) or _subclass .__bases__
643
+ _get_generic_bases (_subclass ) or _subclass .__bases__
644
644
)
645
645
646
646
# include the _subclass itself in the list of bases, if requested
@@ -773,7 +773,7 @@ def _get_parameter_bindings(
773
773
774
774
superclass_bindings = {
775
775
superclass : bindings
776
- for generic_superclass in get_generic_bases (cls )
776
+ for generic_superclass in _get_generic_bases (cls )
777
777
for superclass , bindings in (
778
778
self ._get_parameter_bindings (
779
779
cls = generic_superclass , subclass_bindings = class_bindings
@@ -1287,3 +1287,22 @@ def _copy_generic_type_with_arguments(
1287
1287
new_arguments = (* new_arguments [0 ], * new_arguments [1 :])
1288
1288
1289
1289
return copy_with (new_arguments )
1290
+
1291
+
1292
+ def _get_generic_bases (class_ : type ) -> tuple [type , ...]:
1293
+ """
1294
+ Bugfix version of :func:`typing_inspect.get_generic_bases`.
1295
+
1296
+ Prevents getting the generic bases of the parent class if not defined for the given
1297
+ class.
1298
+
1299
+ :param class_: class to get the generic bases for
1300
+ :return: the generic base classes of the given class
1301
+ """
1302
+ bases : tuple [type , ...] = typing_inspect .get_generic_bases (class_ )
1303
+ if not isinstance (
1304
+ class_ , GenericAlias
1305
+ ) and bases is typing_inspect .get_generic_bases (super (class_ , class_ )):
1306
+ return ()
1307
+ else :
1308
+ return bases
0 commit comments