diff options
-rw-r--r-- | doc/whatsnew/fragments/7979.false_positive | 4 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 1 | ||||
-rw-r--r-- | tests/functional/u/used/used_before_assignment_typing.py | 7 | ||||
-rw-r--r-- | tests/functional/u/used/used_before_assignment_typing.txt | 10 |
4 files changed, 17 insertions, 5 deletions
diff --git a/doc/whatsnew/fragments/7979.false_positive b/doc/whatsnew/fragments/7979.false_positive new file mode 100644 index 000000000..1c10641ae --- /dev/null +++ b/doc/whatsnew/fragments/7979.false_positive @@ -0,0 +1,4 @@ +Prevent ``used-before-assignment`` when imports guarded by ``if TYPE_CHECKING`` +are guarded again when used. + +Closes #7979 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 29911c471..b7e1b1cac 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -2193,6 +2193,7 @@ class VariablesChecker(BaseChecker): isinstance(defstmt, (nodes.Import, nodes.ImportFrom)) and isinstance(defstmt.parent, nodes.If) and in_type_checking_block(defstmt) + and not in_type_checking_block(node) ): defstmt_parent = defstmt.parent diff --git a/tests/functional/u/used/used_before_assignment_typing.py b/tests/functional/u/used/used_before_assignment_typing.py index c374b1a67..c0cb484e0 100644 --- a/tests/functional/u/used/used_before_assignment_typing.py +++ b/tests/functional/u/used/used_before_assignment_typing.py @@ -8,6 +8,7 @@ if TYPE_CHECKING: if True: # pylint: disable=using-constant-test import math import datetime + from urllib.request import urlopen class MyClass: """Type annotation or default values for first level methods can't refer to their own class""" @@ -101,3 +102,9 @@ class VariableAnnotationsGuardedByTypeChecking: # pylint: disable=too-few-publi def print_date(self, date) -> None: date: datetime.date = date print(date) + + +class ConditionalImportGuardedWhenUsed: # pylint: disable=too-few-public-methods + """Conditional imports also guarded by TYPE_CHECKING when used.""" + if TYPE_CHECKING: + print(urlopen) diff --git a/tests/functional/u/used/used_before_assignment_typing.txt b/tests/functional/u/used/used_before_assignment_typing.txt index 7d3223f4a..680c3430d 100644 --- a/tests/functional/u/used/used_before_assignment_typing.txt +++ b/tests/functional/u/used/used_before_assignment_typing.txt @@ -1,5 +1,5 @@ -undefined-variable:16:21:16:28:MyClass.incorrect_typing_method:Undefined variable 'MyClass':UNDEFINED -undefined-variable:21:26:21:33:MyClass.incorrect_nested_typing_method:Undefined variable 'MyClass':UNDEFINED -undefined-variable:26:20:26:27:MyClass.incorrect_default_method:Undefined variable 'MyClass':UNDEFINED -used-before-assignment:87:35:87:39:MyFourthClass.is_close:Using variable 'math' before assignment:HIGH -used-before-assignment:99:20:99:28:VariableAnnotationsGuardedByTypeChecking:Using variable 'datetime' before assignment:HIGH +undefined-variable:17:21:17:28:MyClass.incorrect_typing_method:Undefined variable 'MyClass':UNDEFINED +undefined-variable:22:26:22:33:MyClass.incorrect_nested_typing_method:Undefined variable 'MyClass':UNDEFINED +undefined-variable:27:20:27:27:MyClass.incorrect_default_method:Undefined variable 'MyClass':UNDEFINED +used-before-assignment:88:35:88:39:MyFourthClass.is_close:Using variable 'math' before assignment:HIGH +used-before-assignment:100:20:100:28:VariableAnnotationsGuardedByTypeChecking:Using variable 'datetime' before assignment:HIGH |