Skip to content

Commit

Permalink
Emit used-before-assignment for further imports guarded by TYPE_CHE…
Browse files Browse the repository at this point in the history
…CKING (#7806)

Previously, this message was only emitted for imports guarded directly under
TYPE_CHECKING, not guarded two if-branches deep, nor when TYPE_CHECKING
was imported from typing under an alias.
  • Loading branch information
jacobtylerwalls committed Dec 10, 2022
1 parent b5a85f6 commit e7ef1fd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
7 changes: 7 additions & 0 deletions doc/whatsnew/fragments/7539.false_negative
@@ -0,0 +1,7 @@
Emit ``used-before-assignment`` for further imports guarded by ``TYPE_CHECKING``

Previously, this message was only emitted for imports guarded directly under
``TYPE_CHECKING``, not guarded two if-branches deep, nor when ``TYPE_CHECKING``
was imported from ``typing`` under an alias.

Closes #7539
9 changes: 2 additions & 7 deletions pylint/checkers/variables.py
Expand Up @@ -28,12 +28,7 @@
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

Expand Down Expand Up @@ -2201,7 +2196,7 @@ def _is_variable_violation(
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)
):
defstmt_parent = defstmt.parent

Expand Down
3 changes: 0 additions & 3 deletions pylint/constants.py
Expand Up @@ -99,9 +99,6 @@ class WarningScope:
)


TYPING_TYPE_CHECKS_GUARDS = frozenset({"typing.TYPE_CHECKING", "TYPE_CHECKING"})


def _warn_about_old_home(pylint_home: pathlib.Path) -> None:
"""Warn users about the old pylint home being deprecated.
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/u/used/used_before_assignment_typing.py
Expand Up @@ -5,6 +5,8 @@
from typing import List, Optional, TYPE_CHECKING

if TYPE_CHECKING:
if True: # pylint: disable=using-constant-test
import math
import datetime

class MyClass:
Expand Down Expand Up @@ -78,6 +80,15 @@ def other_function(self) -> None:
_x: MyThirdClass = self


class MyFourthClass: # pylint: disable=too-few-public-methods
"""Class to test conditional imports guarded by TYPE_CHECKING two levels
up then used in function annotation. See https://github.com/PyCQA/pylint/issues/7539"""

def is_close(self, comparator: math.isclose, first, second): # [used-before-assignment]
"""Conditional imports guarded are only valid for variable annotations."""
comparator(first, second)


class VariableAnnotationsGuardedByTypeChecking: # pylint: disable=too-few-public-methods
"""Class to test conditional imports guarded by TYPE_CHECKING then used in
local (function) variable annotations, which are not evaluated at runtime.
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/u/used/used_before_assignment_typing.txt
@@ -1,4 +1,5 @@
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: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

0 comments on commit e7ef1fd

Please sign in to comment.