Skip to content

Commit c3d7b39

Browse files
jacobtylerwallsPierre-Sassoulas
authored andcommitted
Fix used-before-assignment if conditional imports guarded again when used (#7980)
(cherry picked from commit e6f0bc5)
1 parent 1ded4d0 commit c3d7b39

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Prevent ``used-before-assignment`` when imports guarded by ``if TYPE_CHECKING``
2+
are guarded again when used.
3+
4+
Closes #7979

pylint/checkers/variables.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@
2727
in_type_checking_block,
2828
is_postponed_evaluation_enabled,
2929
)
30-
from pylint.constants import (
31-
PY39_PLUS,
32-
TYPING_NEVER,
33-
TYPING_NORETURN,
34-
TYPING_TYPE_CHECKS_GUARDS,
35-
)
30+
from pylint.constants import PY39_PLUS, TYPING_NEVER, TYPING_NORETURN
3631
from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE
3732
from pylint.typing import MessageDefinitionTuple
3833

@@ -2003,7 +1998,8 @@ def _is_variable_violation(
20031998
if (
20041999
isinstance(defstmt, (nodes.Import, nodes.ImportFrom))
20052000
and isinstance(defstmt.parent, nodes.If)
2006-
and defstmt.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS
2001+
and in_type_checking_block(defstmt)
2002+
and not in_type_checking_block(node)
20072003
):
20082004
defstmt_parent = defstmt.parent
20092005

tests/functional/u/used/used_before_assignment_typing.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
if TYPE_CHECKING:
88
import datetime
9+
from urllib.request import urlopen
910

1011
class MyClass:
1112
"""Type annotation or default values for first level methods can't refer to their own class"""
@@ -90,3 +91,9 @@ class VariableAnnotationsGuardedByTypeChecking: # pylint: disable=too-few-publi
9091
def print_date(self, date) -> None:
9192
date: datetime.date = date
9293
print(date)
94+
95+
96+
class ConditionalImportGuardedWhenUsed: # pylint: disable=too-few-public-methods
97+
"""Conditional imports also guarded by TYPE_CHECKING when used."""
98+
if TYPE_CHECKING:
99+
print(urlopen)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
undefined-variable:14:21:14:28:MyClass.incorrect_typing_method:Undefined variable 'MyClass':UNDEFINED
2-
undefined-variable:19:26:19:33:MyClass.incorrect_nested_typing_method:Undefined variable 'MyClass':UNDEFINED
3-
undefined-variable:24:20:24:27:MyClass.incorrect_default_method:Undefined variable 'MyClass':UNDEFINED
4-
used-before-assignment:88:20:88:28:VariableAnnotationsGuardedByTypeChecking:Using variable 'datetime' before assignment:HIGH
1+
undefined-variable:15:21:15:28:MyClass.incorrect_typing_method:Undefined variable 'MyClass':UNDEFINED
2+
undefined-variable:20:26:20:33:MyClass.incorrect_nested_typing_method:Undefined variable 'MyClass':UNDEFINED
3+
undefined-variable:25:20:25:27:MyClass.incorrect_default_method:Undefined variable 'MyClass':UNDEFINED
4+
used-before-assignment:89:20:89:28:VariableAnnotationsGuardedByTypeChecking:Using variable 'datetime' before assignment:HIGH

0 commit comments

Comments
 (0)