Skip to content

Commit 684f14d

Browse files
authored
Be more cautious when identifying typing Literal (#517)
1 parent 7641643 commit 684f14d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pyflakes/checker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,15 @@ def ignore(self, node):
14571457
STARRED = NAMECONSTANT = NAMEDEXPR = handleChildren
14581458

14591459
def SUBSCRIPT(self, node):
1460-
if _is_typing(node.value, 'Literal', self.scopeStack):
1460+
if (
1461+
(
1462+
isinstance(node.value, ast.Name) and
1463+
node.value.id == 'Literal'
1464+
) or (
1465+
isinstance(node.value, ast.Attribute) and
1466+
node.value.attr == 'Literal'
1467+
)
1468+
):
14611469
orig, self._in_typing_literal = self._in_typing_literal, True
14621470
try:
14631471
self.handleChildren(node)

pyflakes/test/test_type_annotations.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,19 @@ def f(x: Literal['some string']) -> None:
507507
return None
508508
""")
509509

510+
@skipIf(version_info < (3,), 'new in Python 3')
511+
def test_literal_type_some_other_module(self):
512+
"""err on the side of false-negatives for types named Literal"""
513+
self.flakes("""
514+
from my_module import compat
515+
from my_module.compat import Literal
516+
517+
def f(x: compat.Literal['some string']) -> None:
518+
return None
519+
def g(x: Literal['some string']) -> None:
520+
return None
521+
""")
522+
510523
@skipIf(version_info < (3,), 'new in Python 3')
511524
def test_literal_union_type_typing(self):
512525
self.flakes("""

0 commit comments

Comments
 (0)