1212# NOTE: This module must support Python 2.7 in addition to Python 3.x
1313
1414import sys
15- NEW_TYPING = sys .version_info [:3 ] >= (3 , 7 , 0 ) # PEP 560
16- if NEW_TYPING :
17- import collections .abc
18-
19- if NEW_TYPING :
20- from typing import (
21- Generic , Callable , Union , TypeVar , ClassVar , Tuple , _GenericAlias
22- )
23- else :
24- from typing import (
25- Callable , CallableMeta , Union , _Union , TupleMeta , TypeVar ,
26- _ClassVar , GenericMeta ,
27- )
15+ import collections .abc
16+ from typing import (
17+ Generic , Callable , Union , TypeVar , ClassVar , Tuple , _GenericAlias
18+ )
2819
2920NEW_39_TYPING = sys .version_info [:3 ] >= (3 , 9 , 0 ) # PEP 560
3021if NEW_39_TYPING :
3324
3425# from mypy_extensions import _TypedDictMeta
3526
36-
37- def _gorg (cls ):
38- """This function exists for compatibility with old typing versions."""
39- assert isinstance (cls , GenericMeta )
40- if hasattr (cls , '_gorg' ):
41- return cls ._gorg
42- while cls .__origin__ is not None :
43- cls = cls .__origin__
44- return cls
45-
46-
4727def is_generic_type (tp ):
4828 """Test if the given type is a generic type. This includes Generic itself,
4929 but excludes special typing constructs such as Union, Tuple, Callable,
@@ -66,13 +46,10 @@ def is_generic_type(tp):
6646 return (isinstance (tp , type ) and issubclass (tp , Generic )
6747 or ((isinstance (tp , _GenericAlias ) or isinstance (tp , _SpecialGenericAlias )) # NoQA E501
6848 and tp .__origin__ not in (Union , tuple , ClassVar , collections .abc .Callable ))) # NoQA E501
69- if NEW_TYPING :
70- return (isinstance (tp , type )
71- and issubclass (tp , Generic )
72- or isinstance (tp , _GenericAlias )
73- and tp .__origin__ not in (Union , tuple , ClassVar , collections .abc .Callable )) # NoQA E501
74- return (isinstance (tp , GenericMeta ) and not
75- isinstance (tp , (CallableMeta , TupleMeta )))
49+ return (isinstance (tp , type )
50+ and issubclass (tp , Generic )
51+ or isinstance (tp , _GenericAlias )
52+ and tp .__origin__ not in (Union , tuple , ClassVar , collections .abc .Callable )) # NoQA E501
7653
7754
7855def is_callable_type (tp ):
@@ -94,12 +71,10 @@ class MyClass(Callable[[int], int]):
9471
9572 get_origin(tp) is collections.abc.Callable # Callable prior to Python 3.7 # NoQA E501
9673 """
97- if NEW_TYPING :
98- return (tp is Callable or isinstance (tp , _GenericAlias ) and
99- tp .__origin__ is collections .abc .Callable or
100- isinstance (tp , type ) and issubclass (tp , Generic ) and
101- issubclass (tp , collections .abc .Callable ))
102- return type (tp ) is CallableMeta
74+ return (tp is Callable or isinstance (tp , _GenericAlias ) and
75+ tp .__origin__ is collections .abc .Callable or
76+ isinstance (tp , type ) and issubclass (tp , Generic ) and
77+ issubclass (tp , collections .abc .Callable ))
10378
10479
10580def is_tuple_type (tp ):
@@ -120,12 +95,10 @@ class MyClass(Tuple[str, int]):
12095
12196 get_origin(tp) is tuple # Tuple prior to Python 3.7
12297 """
123- if NEW_TYPING :
124- return (tp is Tuple or isinstance (tp , _GenericAlias ) and
125- tp .__origin__ is tuple or
126- isinstance (tp , type ) and issubclass (tp , Generic ) and
127- issubclass (tp , tuple ))
128- return type (tp ) is TupleMeta
98+ return (tp is Tuple or isinstance (tp , _GenericAlias ) and
99+ tp .__origin__ is tuple or
100+ isinstance (tp , type ) and issubclass (tp , Generic ) and
101+ issubclass (tp , tuple ))
129102
130103
131104def is_union_type (tp ):
@@ -136,10 +109,8 @@ def is_union_type(tp):
136109 is_union_type(Union[int, int]) == False
137110 is_union_type(Union[T, int]) == True
138111 """
139- if NEW_TYPING :
140- return (tp is Union or
141- isinstance (tp , _GenericAlias ) and tp .__origin__ is Union )
142- return type (tp ) is _Union
112+ return (tp is Union or
113+ isinstance (tp , _GenericAlias ) and tp .__origin__ is Union )
143114
144115
145116def is_typevar (tp ):
@@ -161,10 +132,8 @@ def is_classvar(tp):
161132 is_classvar(ClassVar[int]) == True
162133 is_classvar(ClassVar[List[T]]) == True
163134 """
164- if NEW_TYPING :
165- return (tp is ClassVar or
166- isinstance (tp , _GenericAlias ) and tp .__origin__ is ClassVar )
167- return type (tp ) is _ClassVar
135+ return (tp is ClassVar or
136+ isinstance (tp , _GenericAlias ) and tp .__origin__ is ClassVar )
168137
169138
170139def get_last_origin (tp ):
@@ -179,16 +148,8 @@ def get_last_origin(tp):
179148 get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]]
180149 get_last_origin(List) == List
181150 """
182- if NEW_TYPING :
183- raise ValueError ('This function is only supported in Python 3.6,'
184- ' use get_origin instead' )
185- sentinel = object ()
186- origin = getattr (tp , '__origin__' , sentinel )
187- if origin is sentinel :
188- return None
189- if origin is None :
190- return tp
191- return origin
151+ raise ValueError ('This function is only supported in Python 3.6,'
152+ ' use get_origin instead' )
192153
193154
194155def get_origin (tp ):
@@ -202,17 +163,10 @@ def get_origin(tp):
202163 get_origin(Union[T, int]) == Union
203164 get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7
204165 """
205- if NEW_TYPING :
206- if isinstance (tp , _GenericAlias ):
207- return tp .__origin__ if tp .__origin__ is not ClassVar else None
208- if tp is Generic :
209- return Generic
210- return None
211- if isinstance (tp , GenericMeta ):
212- return _gorg (tp )
213- if is_union_type (tp ):
214- return Union
215-
166+ if isinstance (tp , _GenericAlias ):
167+ return tp .__origin__ if tp .__origin__ is not ClassVar else None
168+ if tp is Generic :
169+ return Generic
216170 return None
217171
218172
@@ -231,16 +185,9 @@ def get_parameters(tp):
231185 get_parameters(Union[S_co, Tuple[T, T]][int, U]) == (U,)
232186 get_parameters(Mapping[T, Tuple[S_co, T]]) == (T, S_co)
233187 """
234- if NEW_TYPING :
235- if (isinstance (tp , _GenericAlias ) or isinstance (tp , type ) and
236- issubclass (tp , Generic ) and tp is not Generic ): # NoQA E129
237- return tp .__parameters__
238- return ()
239- if (
240- is_generic_type (tp ) or is_union_type (tp ) or
241- is_callable_type (tp ) or is_tuple_type (tp )
242- ):
243- return tp .__parameters__ if tp .__parameters__ is not None else ()
188+ if (isinstance (tp , _GenericAlias ) or isinstance (tp , type ) and
189+ issubclass (tp , Generic ) and tp is not Generic ): # NoQA E129
190+ return tp .__parameters__
244191 return ()
245192
246193
@@ -256,17 +203,8 @@ def get_last_args(tp):
256203 get_last_args(Callable[[T], int]) == (T, int)
257204 get_last_args(Callable[[], int]) == (int,)
258205 """
259- if NEW_TYPING :
260- raise ValueError ('This function is only supported in Python 3.6,'
261- ' use get_args instead' )
262- if is_classvar (tp ):
263- return (tp .__type__ ,) if tp .__type__ is not None else ()
264- if (
265- is_generic_type (tp ) or is_union_type (tp ) or
266- is_callable_type (tp ) or is_tuple_type (tp )
267- ):
268- return tp .__args__ if tp .__args__ is not None else ()
269- return ()
206+ raise ValueError ('This function is only supported in Python 3.6,'
207+ ' use get_args instead' )
270208
271209
272210def _eval_args (args ):
@@ -307,29 +245,13 @@ def get_args(tp, evaluate=None):
307245 (int, Tuple[Optional[int], Optional[int]])
308246 get_args(Callable[[], T][int], evaluate=True) == ([], int,)
309247 """
310- if NEW_TYPING :
311- if evaluate is not None and not evaluate :
312- raise ValueError ('evaluate can only be True in Python 3.7' )
313- if isinstance (tp , _GenericAlias ):
314- res = tp .__args__
315- if get_origin (tp ) is collections .abc .Callable and res [0 ] is not Ellipsis : # NoQA E501
316- res = (list (res [:- 1 ]), res [- 1 ])
317- return res
318- return ()
319- if is_classvar (tp ):
320- return (tp .__type__ ,)
321- if (
322- is_generic_type (tp ) or is_union_type (tp ) or
323- is_callable_type (tp ) or is_tuple_type (tp )
324- ):
325- tree = tp ._subs_tree ()
326- if isinstance (tree , tuple ) and len (tree ) > 1 :
327- if not evaluate :
328- return tree [1 :]
329- res = _eval_args (tree [1 :])
330- if get_origin (tp ) is Callable and res [0 ] is not Ellipsis :
331- res = (list (res [:- 1 ]), res [- 1 ])
332- return res
248+ if evaluate is not None and not evaluate :
249+ raise ValueError ('evaluate can only be True in Python 3.7' )
250+ if isinstance (tp , _GenericAlias ):
251+ res = tp .__args__
252+ if get_origin (tp ) is collections .abc .Callable and res [0 ] is not Ellipsis : # NoQA E501
253+ res = (list (res [:- 1 ]), res [- 1 ])
254+ return res
333255 return ()
334256
335257
0 commit comments