diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-12-25 04:20:53 -0500 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-12-26 14:00:48 +0100 |
commit | c3d7b3963b5e36939debcc5d608567fab429724c (patch) | |
tree | 8bdd0c1879478ec6a1d3988d64ee1ef0becb54f9 | |
parent | 1ded4d081480cccd573ddc6c592adccdc6723bb5 (diff) | |
download | pylint-git-c3d7b3963b5e36939debcc5d608567fab429724c.tar.gz |
Fix `used-before-assignment` if conditional imports guarded again when used (#7980)
(cherry picked from commit e6f0bc5d32965eef37e498e15f05914d4275afb5)
-rw-r--r-- | doc/whatsnew/fragments/7979.false_positive | 4 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 10 | ||||
-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 | 8 |
4 files changed, 18 insertions, 11 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 8e9a3c9bb..1128577c4 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -27,12 +27,7 @@ from pylint.checkers.utils import ( in_type_checking_block, is_postponed_evaluation_enabled, ) -from pylint.constants import ( - PY39_PLUS, - TYPING_NEVER, - TYPING_NORETURN, - TYPING_TYPE_CHECKS_GUARDS, -) +from pylint.constants import PY39_PLUS, TYPING_NEVER, TYPING_NORETURN from pylint.interfaces import CONTROL_FLOW, HIGH, INFERENCE, INFERENCE_FAILURE from pylint.typing import MessageDefinitionTuple @@ -2003,7 +1998,8 @@ class VariablesChecker(BaseChecker): if ( isinstance(defstmt, (nodes.Import, nodes.ImportFrom)) and isinstance(defstmt.parent, nodes.If) - and defstmt.parent.test.as_string() in TYPING_TYPE_CHECKS_GUARDS + 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 a6ad92617..e2cefc798 100644 --- a/tests/functional/u/used/used_before_assignment_typing.py +++ b/tests/functional/u/used/used_before_assignment_typing.py @@ -6,6 +6,7 @@ from typing import List, Optional, TYPE_CHECKING if TYPE_CHECKING: 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""" @@ -90,3 +91,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 4000013bf..551bad98c 100644 --- a/tests/functional/u/used/used_before_assignment_typing.txt +++ b/tests/functional/u/used/used_before_assignment_typing.txt @@ -1,4 +1,4 @@ -undefined-variable:14:21:14:28:MyClass.incorrect_typing_method:Undefined variable 'MyClass':UNDEFINED -undefined-variable:19:26:19:33:MyClass.incorrect_nested_typing_method:Undefined variable 'MyClass':UNDEFINED -undefined-variable:24:20:24:27:MyClass.incorrect_default_method:Undefined variable 'MyClass':UNDEFINED -used-before-assignment:88:20:88:28:VariableAnnotationsGuardedByTypeChecking:Using variable 'datetime' before assignment:HIGH +undefined-variable:15:21:15:28:MyClass.incorrect_typing_method:Undefined variable 'MyClass':UNDEFINED +undefined-variable:20:26:20:33:MyClass.incorrect_nested_typing_method:Undefined variable 'MyClass':UNDEFINED +undefined-variable:25:20:25:27:MyClass.incorrect_default_method:Undefined variable 'MyClass':UNDEFINED +used-before-assignment:89:20:89:28:VariableAnnotationsGuardedByTypeChecking:Using variable 'datetime' before assignment:HIGH |