Skip to content

Commit 1ec1be2

Browse files
committed
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 pylint-dev#8696
1 parent 6bfa040 commit 1ec1be2

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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.
2+
3+
Closes #8696

pylint/checkers/variables.py

+2
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,8 @@ def _check_globals(self, not_consumed: dict[str, nodes.NodeNG]) -> None:
31013101
return
31023102
for name, node_lst in not_consumed.items():
31033103
for node in node_lst:
3104+
if in_type_checking_block(node):
3105+
continue
31043106
self.add_message("unused-variable", args=(name,), node=node)
31053107

31063108
# pylint: disable = too-many-branches
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# pylint: disable=missing-docstring
2+
3+
4+
from typing import TYPE_CHECKING
5+
6+
7+
if TYPE_CHECKING:
8+
import math
9+
10+
211
VAR = 'pylint' # [unused-variable]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
unused-variable:2:0:2:3::Unused variable 'VAR':UNDEFINED
1+
unused-variable:11:0:11:3::Unused variable 'VAR':UNDEFINED

0 commit comments

Comments
 (0)