Skip to content

Commit

Permalink
Fix quoted type annotations in typing.TypeVar contexts (#534)
Browse files Browse the repository at this point in the history
TypeVar has such contexts in the positional arguments (for constraints)
and in the `bound` keyword argument.
  • Loading branch information
bluetech committed Jan 4, 2021
1 parent 0e4194b commit d2ab3ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,19 @@ def CALL(self, node):
with self._enter_annotation(AnnotationState.STRING):
self.handleNode(node.args[0], node)

elif _is_typing(node.func, 'TypeVar', self.scopeStack):
# TypeVar("T", "int", "str")
for arg in node.args[1:]:
if isinstance(arg, ast.Str):
with self._enter_annotation():
self.handleNode(arg, node)

# TypeVar("T", bound="str")
for keyword in node.keywords:
if keyword.arg == 'bound' and isinstance(keyword.value, ast.Str):
with self._enter_annotation():
self.handleNode(keyword.value, node)

self.handleChildren(node)

def _handle_percent_format(self, node):
Expand Down
15 changes: 15 additions & 0 deletions pyflakes/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,21 @@ def test_quoted_type_cast_renamed_import(self):
maybe_int = tsac('Maybe[int]', 42)
""")

def test_quoted_TypeVar_constraints(self):
self.flakes("""
from typing import TypeVar, Optional
T = TypeVar('T', 'str', 'Optional[int]', bytes)
""")

def test_quoted_TypeVar_bound(self):
self.flakes("""
from typing import TypeVar, Optional, List
T = TypeVar('T', bound='Optional[int]')
S = TypeVar('S', int, bound='List[int]')
""")

@skipIf(version_info < (3,), 'new in Python 3')
def test_literal_type_typing(self):
self.flakes("""
Expand Down

0 comments on commit d2ab3ec

Please sign in to comment.