diff options
-rw-r--r-- | pyflakes/checker.py | 12 | ||||
-rw-r--r-- | pyflakes/test/test_type_annotations.py | 17 |
2 files changed, 28 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 6fafe89..45c7a4a 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -720,6 +720,16 @@ def _is_typing_helper(node, is_name_match_fn, scope_stack): return False + def _module_scope_is_typing(name): + for scope in reversed(scope_stack): + if name in scope: + return ( + isinstance(scope[name], Importation) and + scope[name].fullName in TYPING_MODULES + ) + + return False + return ( ( isinstance(node, ast.Name) and @@ -727,7 +737,7 @@ def _is_typing_helper(node, is_name_match_fn, scope_stack): ) or ( isinstance(node, ast.Attribute) and isinstance(node.value, ast.Name) and - node.value.id in TYPING_MODULES and + _module_scope_is_typing(node.value.id) and is_name_match_fn(node.attr) ) ) diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py index 6a66bcd..f3b6c24 100644 --- a/pyflakes/test/test_type_annotations.py +++ b/pyflakes/test/test_type_annotations.py @@ -121,6 +121,23 @@ class TestTypeAnnotations(TestCase): def f(self, x): return x """) + def test_aliased_import(self): + """Detect when typing is imported as another name""" + self.flakes(""" + import typing as t + + @t.overload + def f(s): # type: (None) -> None + pass + + @t.overload + def f(s): # type: (int) -> int + pass + + def f(s): + return s + """) + def test_not_a_typing_overload(self): """regression test for @typing.overload detection bug in 2.1.0""" self.flakes(""" |