88
99from typing import Any , Generic
1010from typing import GenericAlias as TypesGenericAlias # type: ignore[attr-defined]
11- from typing import Optional , Protocol , TypeGuard , TypeVar
11+ from typing import Optional , Protocol , TypeVar , Union
1212from typing import _GenericAlias as TypingGenericAlias # type: ignore[attr-defined]
1313from typing import get_args , get_origin
1414
@@ -22,7 +22,7 @@ class GenericType(Protocol):
2222 __orig_bases__ : tuple [type , ...]
2323
2424
25- def get_type_vars (type_ : type | GenericType ) -> tuple [TypeVar , ...]:
25+ def get_type_vars (type_ : Union [ type , GenericType ] ) -> tuple [TypeVar , ...]:
2626 """
2727 For a given generic type, return a tuple of its type variables. The type variables are collected through the
2828 supertypes arguments `Generic` if present.
@@ -57,14 +57,14 @@ def get_type_vars(type_: type | GenericType) -> tuple[TypeVar, ...]:
5757 if not _generic_metaclass_executed_on_type (type_ ):
5858 return ()
5959
60- for base in type_ .__orig_bases__ :
60+ for base in type_ .__orig_bases__ : # type: ignore[union-attr]
6161 if get_origin (base ) is Generic :
6262 return get_args (base )
6363
6464 # if we get here, the type has not `Generic` directly as supertype. Therefore, collect the type variables from
6565 # all the supertypes arguments.
6666 type_vars : list [TypeVar ] = []
67- for base in type_ .__orig_bases__ :
67+ for base in type_ .__orig_bases__ : # type: ignore[union-attr]
6868 if isinstance (base , (TypingGenericAlias , TypesGenericAlias )):
6969 for arg in get_args (base ):
7070 if isinstance (arg , TypeVar ) and arg not in type_vars :
@@ -73,7 +73,7 @@ def get_type_vars(type_: type | GenericType) -> tuple[TypeVar, ...]:
7373 return tuple (type_vars )
7474
7575
76- def _generic_metaclass_executed_on_type (type_ : type | GenericType ) -> TypeGuard [ GenericType ] :
76+ def _generic_metaclass_executed_on_type (type_ : Union [ type , GenericType ] ) -> bool :
7777 """
7878 This function determines if the type was processed by a `_GenericAlias` with all its `__mro_entries__` magic.
7979 I.e. if the type has `Generic` as supertype or something like `A[T]` in its supertypes.
@@ -111,7 +111,7 @@ def _find_super_type_trace(type_: type, search_for_type: type) -> Optional[list[
111111
112112
113113def _process_inputs_of_get_filled_type (
114- type_or_instance : Any , type_var_defining_super_type : type , type_var_or_position : TypeVar | int
114+ type_or_instance : Any , type_var_defining_super_type : type , type_var_or_position : Union [ TypeVar , int ]
115115) -> tuple [type , type , int ]:
116116 """
117117 This function processes the inputs of `get_filled_type`. It returns a tuple of the filled type, the super type and
@@ -142,7 +142,7 @@ def _process_inputs_of_get_filled_type(
142142
143143# pylint: disable=too-many-branches, too-many-locals
144144def get_filled_type (
145- type_or_instance : Any , type_var_defining_super_type : type , type_var_or_position : TypeVar | int
145+ type_or_instance : Any , type_var_defining_super_type : type , type_var_or_position : Union [ TypeVar , int ]
146146) -> Any :
147147 """
148148 Determines the type of the `type_var_or_position` defined by the type `type_var_defining_super_type`.
@@ -187,7 +187,7 @@ def get_filled_type(
187187 continue
188188 if not _generic_metaclass_executed_on_type (type_ ):
189189 raise TypeError (f"Could not determine the type in { filled_type !r} : { type_ !r} is not generic" )
190- for orig_base in type_ .__orig_bases__ :
190+ for orig_base in type_ .__orig_bases__ : # type: ignore[attr-defined]
191191 if get_origin (orig_base ) == type_trace [- reversed_index + 1 ]:
192192 orig_base_args = get_args (orig_base )
193193 if len (orig_base_args ) < type_var_index :
0 commit comments