12
12
# NOTE: This module must support Python 2.7 in addition to Python 3.x
13
13
14
14
import 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
+ )
28
19
29
20
NEW_39_TYPING = sys .version_info [:3 ] >= (3 , 9 , 0 ) # PEP 560
30
21
if NEW_39_TYPING :
33
24
34
25
# from mypy_extensions import _TypedDictMeta
35
26
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
-
47
27
def is_generic_type (tp ):
48
28
"""Test if the given type is a generic type. This includes Generic itself,
49
29
but excludes special typing constructs such as Union, Tuple, Callable,
@@ -66,13 +46,10 @@ def is_generic_type(tp):
66
46
return (isinstance (tp , type ) and issubclass (tp , Generic )
67
47
or ((isinstance (tp , _GenericAlias ) or isinstance (tp , _SpecialGenericAlias )) # NoQA E501
68
48
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
76
53
77
54
78
55
def is_callable_type (tp ):
@@ -94,12 +71,10 @@ class MyClass(Callable[[int], int]):
94
71
95
72
get_origin(tp) is collections.abc.Callable # Callable prior to Python 3.7 # NoQA E501
96
73
"""
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 ))
103
78
104
79
105
80
def is_tuple_type (tp ):
@@ -120,12 +95,10 @@ class MyClass(Tuple[str, int]):
120
95
121
96
get_origin(tp) is tuple # Tuple prior to Python 3.7
122
97
"""
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 ))
129
102
130
103
131
104
def is_union_type (tp ):
@@ -136,10 +109,8 @@ def is_union_type(tp):
136
109
is_union_type(Union[int, int]) == False
137
110
is_union_type(Union[T, int]) == True
138
111
"""
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 )
143
114
144
115
145
116
def is_typevar (tp ):
@@ -161,10 +132,8 @@ def is_classvar(tp):
161
132
is_classvar(ClassVar[int]) == True
162
133
is_classvar(ClassVar[List[T]]) == True
163
134
"""
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 )
168
137
169
138
170
139
def get_last_origin (tp ):
@@ -179,16 +148,8 @@ def get_last_origin(tp):
179
148
get_last_origin(List[Tuple[T, T]][int]) == List[Tuple[T, T]]
180
149
get_last_origin(List) == List
181
150
"""
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' )
192
153
193
154
194
155
def get_origin (tp ):
@@ -202,17 +163,10 @@ def get_origin(tp):
202
163
get_origin(Union[T, int]) == Union
203
164
get_origin(List[Tuple[T, T]][int]) == list # List prior to Python 3.7
204
165
"""
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
216
170
return None
217
171
218
172
@@ -231,16 +185,9 @@ def get_parameters(tp):
231
185
get_parameters(Union[S_co, Tuple[T, T]][int, U]) == (U,)
232
186
get_parameters(Mapping[T, Tuple[S_co, T]]) == (T, S_co)
233
187
"""
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__
244
191
return ()
245
192
246
193
@@ -256,17 +203,8 @@ def get_last_args(tp):
256
203
get_last_args(Callable[[T], int]) == (T, int)
257
204
get_last_args(Callable[[], int]) == (int,)
258
205
"""
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' )
270
208
271
209
272
210
def _eval_args (args ):
@@ -307,29 +245,13 @@ def get_args(tp, evaluate=None):
307
245
(int, Tuple[Optional[int], Optional[int]])
308
246
get_args(Callable[[], T][int], evaluate=True) == ([], int,)
309
247
"""
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
333
255
return ()
334
256
335
257
0 commit comments