Skip to content

Fix used-before-assignment if conditional imports guarded again when used #7980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/7979.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Prevent ``used-before-assignment`` when imports guarded by ``if TYPE_CHECKING``
are guarded again when used.

Closes #7979
1 change: 1 addition & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,7 @@ def _is_variable_violation(
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

Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/used/used_before_assignment_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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"""
Expand Down Expand Up @@ -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)
10 changes: 5 additions & 5 deletions tests/functional/u/used/used_before_assignment_typing.txt
Original file line number Diff line number Diff line change
@@ -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