Skip to content

Commit 37a2499

Browse files
committed
WIP 4
1 parent 9817886 commit 37a2499

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

astroid/brain/brain_typing.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,51 @@ class {0}(metaclass=Meta):
4141
"""
4242
TYPING_MEMBERS = set(typing.__all__)
4343

44+
TYPING_ALIAS = frozenset(
45+
(
46+
"typing.Hashable",
47+
"typing.Awaitable",
48+
"typing.Coroutine",
49+
"typing.AsyncIterable",
50+
"typing.AsyncIterator",
51+
"typing.Iterable",
52+
"typing.Iterator",
53+
"typing.Reversible",
54+
"typing.Sized",
55+
"typing.Container",
56+
"typing.Collection",
57+
"typing.Callable",
58+
"typing.AbstractSet",
59+
"typing.MutableSet",
60+
"typing.Mapping",
61+
"typing.MutableMapping",
62+
"typing.Sequence",
63+
"typing.MutableSequence",
64+
"typing.ByteString",
65+
"typing.Tuple",
66+
"typing.List",
67+
"typing.Deque",
68+
"typing.Set",
69+
"typing.FrozenSet",
70+
"typing.MappingView",
71+
"typing.KeysView",
72+
"typing.ItemsView",
73+
"typing.ValuesView",
74+
"typing.ContextManager",
75+
"typing.AsyncContextManager",
76+
"typing.Dict",
77+
"typing.DefaultDict",
78+
"typing.OrderedDict",
79+
"typing.Counter",
80+
"typing.ChainMap",
81+
"typing.Generator",
82+
"typing.AsyncGenerator",
83+
"typing.Type",
84+
"typing.Pattern",
85+
"typing.Match",
86+
)
87+
)
88+
4489

4590
def looks_like_typing_typevar_or_newtype(node):
4691
func = node.func
@@ -86,7 +131,7 @@ def infer_typing_attr(node, context=None):
86131
except InferenceError as exc:
87132
raise UseInferenceDefault from exc
88133

89-
if not value.qname().startswith("typing."):
134+
if not value.qname().startswith("typing.") or value.qname() in TYPING_ALIAS:
90135
raise UseInferenceDefault
91136

92137
node = extract_node(TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1]))
@@ -161,7 +206,6 @@ def infer_typing_alias(
161206
class_def = nodes.ClassDef(
162207
name=assign_name.name,
163208
lineno=assign_name.lineno,
164-
# tolineno=assign_name.lineno,
165209
col_offset=assign_name.col_offset,
166210
parent=node.parent,
167211
)
@@ -198,10 +242,9 @@ def infer_typing_alias(
198242
inference_tip(infer_typing_typevar_or_newtype),
199243
looks_like_typing_typevar_or_newtype,
200244
)
201-
if not PY37:
202-
MANAGER.register_transform(
203-
nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript
204-
)
245+
MANAGER.register_transform(
246+
nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript
247+
)
205248

206249
if PY39:
207250
MANAGER.register_transform(

astroid/scoped_nodes.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2627,15 +2627,12 @@ def getitem(self, index, context=None):
26272627
# Here it is assumed that the __class_getitem__ node is
26282628
# a FunctionDef. One possible improvment would be to deal
26292629
# with more generic inference.
2630-
if not isinstance(methods[0], FunctionDef):
2631-
raise exceptions.AttributeInferenceError
26322630
except exceptions.AttributeInferenceError:
26332631
raise exceptions.AstroidTypeError(
26342632
node=self, context=context
26352633
) from exc
26362634
else:
26372635
raise exceptions.AstroidTypeError(node=self, context=context) from exc
2638-
# raise exceptions.AstroidTypeError(node=self, context=context) from exc
26392636

26402637
method = methods[0]
26412638

0 commit comments

Comments
 (0)