Skip to content

Commit

Permalink
Fix a false positive for unused-variable when there is an import …
Browse files Browse the repository at this point in the history
…in a ``if TYPE_CHECKING:`` block and ``allow-global-unused-variables`` is set to ``no`` in the configuration. (#8713)

Closes #8696
  • Loading branch information
mbyrnepr2 authored Jun 6, 2023
1 parent 331e48f commit e5584d5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8696.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a false positive for ``unused-variable`` when there is an import in a ``if TYPE_CHECKING:`` block and ``allow-global-unused-variables`` is set to ``no`` in the configuration.

Closes #8696
2 changes: 2 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3104,6 +3104,8 @@ def _check_globals(self, not_consumed: dict[str, nodes.NodeNG]) -> None:
return
for name, node_lst in not_consumed.items():
for node in node_lst:
if in_type_checking_block(node):
continue
self.add_message("unused-variable", args=(name,), node=node)

# pylint: disable = too-many-branches
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/u/unused/unused_global_variable2.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# pylint: disable=missing-docstring


from typing import TYPE_CHECKING


if TYPE_CHECKING:
import math


VAR = 'pylint' # [unused-variable]
2 changes: 1 addition & 1 deletion tests/functional/u/unused/unused_global_variable2.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
unused-variable:2:0:2:3::Unused variable 'VAR':UNDEFINED
unused-variable:11:0:11:3::Unused variable 'VAR':UNDEFINED

0 comments on commit e5584d5

Please sign in to comment.