diff options
author | Anthony Sottile <asottile@umich.edu> | 2020-03-17 14:08:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-17 14:08:05 -0700 |
commit | 684f14db591e9f23a6f27c04300497f56413da8d (patch) | |
tree | 301f81e9f71594b42ed0896ff654fe5750b508af | |
parent | 76416437ef22077b5e2949e78fa3000b3580e319 (diff) | |
download | pyflakes-684f14db591e9f23a6f27c04300497f56413da8d.tar.gz |
Be more cautious when identifying typing Literal (#517)
-rw-r--r-- | pyflakes/checker.py | 10 | ||||
-rw-r--r-- | pyflakes/test/test_type_annotations.py | 13 |
2 files changed, 22 insertions, 1 deletions
diff --git a/pyflakes/checker.py b/pyflakes/checker.py index c239950..2b6a98f 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1457,7 +1457,15 @@ class Checker(object): STARRED = NAMECONSTANT = NAMEDEXPR = handleChildren def SUBSCRIPT(self, node): - if _is_typing(node.value, 'Literal', self.scopeStack): + if ( + ( + isinstance(node.value, ast.Name) and + node.value.id == 'Literal' + ) or ( + isinstance(node.value, ast.Attribute) and + node.value.attr == 'Literal' + ) + ): orig, self._in_typing_literal = self._in_typing_literal, True try: self.handleChildren(node) diff --git a/pyflakes/test/test_type_annotations.py b/pyflakes/test/test_type_annotations.py index 4804dda..ed28127 100644 --- a/pyflakes/test/test_type_annotations.py +++ b/pyflakes/test/test_type_annotations.py @@ -508,6 +508,19 @@ class TestTypeAnnotations(TestCase): """) @skipIf(version_info < (3,), 'new in Python 3') + def test_literal_type_some_other_module(self): + """err on the side of false-negatives for types named Literal""" + self.flakes(""" + from my_module import compat + from my_module.compat import Literal + + def f(x: compat.Literal['some string']) -> None: + return None + def g(x: Literal['some string']) -> None: + return None + """) + + @skipIf(version_info < (3,), 'new in Python 3') def test_literal_union_type_typing(self): self.flakes(""" from typing import Literal |