From 37a249993cf2be02e82881c48f782ae196503e0d Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 26 Mar 2021 18:57:04 +0100 Subject: [PATCH] WIP 4 --- astroid/brain/brain_typing.py | 55 +++++++++++++++++++++++++++++++---- astroid/scoped_nodes.py | 3 -- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index 5313a04446..9cd57a7bac 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -41,6 +41,51 @@ class {0}(metaclass=Meta): """ TYPING_MEMBERS = set(typing.__all__) +TYPING_ALIAS = frozenset( + ( + "typing.Hashable", + "typing.Awaitable", + "typing.Coroutine", + "typing.AsyncIterable", + "typing.AsyncIterator", + "typing.Iterable", + "typing.Iterator", + "typing.Reversible", + "typing.Sized", + "typing.Container", + "typing.Collection", + "typing.Callable", + "typing.AbstractSet", + "typing.MutableSet", + "typing.Mapping", + "typing.MutableMapping", + "typing.Sequence", + "typing.MutableSequence", + "typing.ByteString", + "typing.Tuple", + "typing.List", + "typing.Deque", + "typing.Set", + "typing.FrozenSet", + "typing.MappingView", + "typing.KeysView", + "typing.ItemsView", + "typing.ValuesView", + "typing.ContextManager", + "typing.AsyncContextManager", + "typing.Dict", + "typing.DefaultDict", + "typing.OrderedDict", + "typing.Counter", + "typing.ChainMap", + "typing.Generator", + "typing.AsyncGenerator", + "typing.Type", + "typing.Pattern", + "typing.Match", + ) +) + def looks_like_typing_typevar_or_newtype(node): func = node.func @@ -86,7 +131,7 @@ def infer_typing_attr(node, context=None): except InferenceError as exc: raise UseInferenceDefault from exc - if not value.qname().startswith("typing."): + if not value.qname().startswith("typing.") or value.qname() in TYPING_ALIAS: raise UseInferenceDefault node = extract_node(TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1])) @@ -161,7 +206,6 @@ def infer_typing_alias( class_def = nodes.ClassDef( name=assign_name.name, lineno=assign_name.lineno, - # tolineno=assign_name.lineno, col_offset=assign_name.col_offset, parent=node.parent, ) @@ -198,10 +242,9 @@ def infer_typing_alias( inference_tip(infer_typing_typevar_or_newtype), looks_like_typing_typevar_or_newtype, ) -if not PY37: - MANAGER.register_transform( - nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript - ) +MANAGER.register_transform( + nodes.Subscript, inference_tip(infer_typing_attr), _looks_like_typing_subscript +) if PY39: MANAGER.register_transform( diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 1a573c7baf..c098ec24e0 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -2627,15 +2627,12 @@ def getitem(self, index, context=None): # Here it is assumed that the __class_getitem__ node is # a FunctionDef. One possible improvment would be to deal # with more generic inference. - if not isinstance(methods[0], FunctionDef): - raise exceptions.AttributeInferenceError except exceptions.AttributeInferenceError: raise exceptions.AstroidTypeError( node=self, context=context ) from exc else: raise exceptions.AstroidTypeError(node=self, context=context) from exc - # raise exceptions.AstroidTypeError(node=self, context=context) from exc method = methods[0]