Skip to content

Commit 7de7724

Browse files
Refactoring typing_inspect.py (#1435)
* refactor typing_inspect * deleted import * updated test * lint --------- Co-authored-by: gavin-aguiar <80794152+gavin-aguiar@users.noreply.github.com>
1 parent 8f1c7b1 commit 7de7724

File tree

2 files changed

+39
-151
lines changed

2 files changed

+39
-151
lines changed

azure_functions_worker/_thirdparty/typing_inspect.py

Lines changed: 38 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@
1212
# NOTE: This module must support Python 2.7 in addition to Python 3.x
1313

1414
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+
)
2819

2920
NEW_39_TYPING = sys.version_info[:3] >= (3, 9, 0) # PEP 560
3021
if NEW_39_TYPING:
@@ -33,17 +24,6 @@
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-
4727
def 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

7855
def 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

10580
def 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

131104
def 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

145116
def 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

170139
def 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

194155
def 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

272210
def _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

tests/unittests/test_typing_inspect.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
MutableMapping, Iterable, Generic, List, Any, Dict, Tuple, NamedTuple,
1515
)
1616

17-
import sys
18-
NEW_TYPING = sys.version_info[:3] >= (3, 7, 0) # PEP 560
19-
2017

2118
class IsUtilityTestCase(TestCase):
2219
def sample_test(self, fun, samples, nonsamples):
@@ -74,23 +71,13 @@ def test_classvar(self):
7471

7572
class GetUtilityTestCase(TestCase):
7673

77-
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
78-
def test_last_origin(self):
79-
T = TypeVar('T')
80-
self.assertEqual(get_last_origin(int), None)
81-
self.assertEqual(get_last_origin(ClassVar[int]), None)
82-
self.assertEqual(get_last_origin(Generic[T]), Generic)
83-
self.assertEqual(get_last_origin(Union[T, int][str]), Union[T, int])
84-
self.assertEqual(get_last_origin(List[Tuple[T, T]][int]), List[Tuple[T, T]])
85-
self.assertEqual(get_last_origin(List), List)
86-
8774
def test_origin(self):
8875
T = TypeVar('T')
8976
self.assertEqual(get_origin(int), None)
9077
self.assertEqual(get_origin(ClassVar[int]), None)
9178
self.assertEqual(get_origin(Generic), Generic)
9279
self.assertEqual(get_origin(Generic[T]), Generic)
93-
self.assertEqual(get_origin(List[Tuple[T, T]][int]), list if NEW_TYPING else List)
80+
self.assertEqual(get_origin(List[Tuple[T, T]][int]), list)
9481

9582
def test_parameters(self):
9683
T = TypeVar('T')
@@ -105,27 +92,6 @@ def test_parameters(self):
10592
self.assertEqual(get_parameters(Union[S_co, Tuple[T, T]][int, U]), (U,))
10693
self.assertEqual(get_parameters(Mapping[T, Tuple[S_co, T]]), (T, S_co))
10794

108-
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
109-
def test_last_args(self):
110-
T = TypeVar('T')
111-
S = TypeVar('S')
112-
self.assertEqual(get_last_args(int), ())
113-
self.assertEqual(get_last_args(Union), ())
114-
self.assertEqual(get_last_args(ClassVar[int]), (int,))
115-
self.assertEqual(get_last_args(Union[T, int]), (T, int))
116-
self.assertEqual(get_last_args(Iterable[Tuple[T, S]][int, T]), (int, T))
117-
self.assertEqual(get_last_args(Callable[[T, S], int]), (T, S, int))
118-
self.assertEqual(get_last_args(Callable[[], int]), (int,))
119-
120-
@skipIf(NEW_TYPING, "Not supported in Python 3.7")
121-
def test_args(self):
122-
T = TypeVar('T')
123-
self.assertEqual(get_args(Union[int, Tuple[T, int]][str]),
124-
(int, (Tuple, str, int)))
125-
self.assertEqual(get_args(Union[int, Union[T, int], str][int]),
126-
(int, str))
127-
self.assertEqual(get_args(int), ())
128-
12995
def test_args_evaluated(self):
13096
T = TypeVar('T')
13197
self.assertEqual(get_args(Union[int, Tuple[T, int]][str], evaluate=True),

0 commit comments

Comments
 (0)