Skip to content

Commit

Permalink
Preserve module name when subscripting a type from the 'typing' module
Browse files Browse the repository at this point in the history
When no module name is passed, it defaults to the empty string,
which leads to an incorrect qname on the inferrence result.
  • Loading branch information
mthuurne committed Apr 27, 2020
1 parent 6c8bfb4 commit c44fa4e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def infer_typing_attr(node, context=None):
if not value.qname().startswith("typing."):
raise UseInferenceDefault

node = extract_node(TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1]))
node = extract_node(
TYPING_TYPE_TEMPLATE.format(value.qname().split(".")[-1]),
"typing"
)
return node.infer(context=context)


Expand Down
11 changes: 11 additions & 0 deletions tests/unittest_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,17 @@ def test_typing_namedtuple_dont_crash_on_no_fields(self):
inferred = next(node.infer())
self.assertIsInstance(inferred, astroid.Instance)

def test_generic_subscript(self):
node = builder.extract_node(
"""
from typing import Generic, TypeVar
T = TypeVar('T')
Generic[T] #@
"""
)
inferred = next(node.infer())
self.assertEqual(inferred.qname(), "typing.Generic")


class ReBrainTest(unittest.TestCase):
def test_regex_flags(self):
Expand Down

0 comments on commit c44fa4e

Please sign in to comment.