Skip to content

Commit

Permalink
WIP 4
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Mar 26, 2021
1 parent 9817886 commit 37a2499
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
55 changes: 49 additions & 6 deletions astroid/brain/brain_typing.py
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions astroid/scoped_nodes.py
Expand Up @@ -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]

Expand Down

0 comments on commit 37a2499

Please sign in to comment.