diff --git a/pyflakes/checker.py b/pyflakes/checker.py index a6975f23..8ddb17e2 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -671,7 +671,7 @@ class AnnotationState: NONE = 0 STRING = 1 BARE = 2 - TYPE_ALIAS = 3 + STR_AS_TYPE = 3 def in_annotation(func): @@ -1070,7 +1070,7 @@ def handleNodeLoad(self, node, parent): binding.is_lazy and func_depth == 0 and ( self._in_annotation == AnnotationState.NONE or - self._in_annotation == AnnotationState.TYPE_ALIAS + self._in_annotation == AnnotationState.STR_AS_TYPE ) ): self.report(messages.EagerUseOfLazyImport, node, name, binding.source) @@ -1522,7 +1522,7 @@ def CALL(self, node): self._handle_string_dot_format(node) def _annotation(n: ast.AST) -> None: - with self._enter_annotation(): + with self._enter_annotation(AnnotationState.STR_AS_TYPE): self.handleNode(n, node) def _annotations(nodes: list[ast.AST]) -> None: @@ -2056,7 +2056,7 @@ def ANNASSIGN(self, node): if node.value: # If the annotation is `TypeAlias`, handle the *value* as an annotation. if _is_typing(node.annotation, 'TypeAlias', self.scopeStack): - with self._enter_annotation(AnnotationState.TYPE_ALIAS): + with self._enter_annotation(AnnotationState.STR_AS_TYPE): self.handleNode(node.value, node) else: self.handleNode(node.value, node) diff --git a/pyflakes/test/test_lazy_imports.py b/pyflakes/test/test_lazy_imports.py index d7b9e9ec..044f9644 100644 --- a/pyflakes/test/test_lazy_imports.py +++ b/pyflakes/test/test_lazy_imports.py @@ -61,3 +61,10 @@ def test_lazy_imports_eager_use_in_type_alias(self): lazy from x import y alias: TypeAlias = y # evaluated eagerly ''', m.EagerUseOfLazyImport) + + def test_eager_use_in_TypeVar(self): + self.flakes(''' + from typing import TypeVar + lazy from x import y + T = TypeVar("T", y) + ''', m.EagerUseOfLazyImport)