Skip to content

Commit 13cad91

Browse files
Detect typing module attributes with 'import typing as <name>' (#632)
* added functionality to detect typing module attributes with 'import typing as <name>' * remove async keyword from test_aliased_import
1 parent cf75971 commit 13cad91

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pyflakes/checker.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,14 +720,24 @@ def _bare_name_is_attr(name):
720720

721721
return False
722722

723+
def _module_scope_is_typing(name):
724+
for scope in reversed(scope_stack):
725+
if name in scope:
726+
return (
727+
isinstance(scope[name], Importation) and
728+
scope[name].fullName in TYPING_MODULES
729+
)
730+
731+
return False
732+
723733
return (
724734
(
725735
isinstance(node, ast.Name) and
726736
_bare_name_is_attr(node.id)
727737
) or (
728738
isinstance(node, ast.Attribute) and
729739
isinstance(node.value, ast.Name) and
730-
node.value.id in TYPING_MODULES and
740+
_module_scope_is_typing(node.value.id) and
731741
is_name_match_fn(node.attr)
732742
)
733743
)

pyflakes/test/test_type_annotations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ def f(self, x): # type: (str) -> str
121121
def f(self, x): return x
122122
""")
123123

124+
def test_aliased_import(self):
125+
"""Detect when typing is imported as another name"""
126+
self.flakes("""
127+
import typing as t
128+
129+
@t.overload
130+
def f(s): # type: (None) -> None
131+
pass
132+
133+
@t.overload
134+
def f(s): # type: (int) -> int
135+
pass
136+
137+
def f(s):
138+
return s
139+
""")
140+
124141
def test_not_a_typing_overload(self):
125142
"""regression test for @typing.overload detection bug in 2.1.0"""
126143
self.flakes("""

0 commit comments

Comments
 (0)