Skip to content

Commit f37d4cb

Browse files
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. (#8713) (#8744)
Closes #8696 (cherry picked from commit e5584d5) Co-authored-by: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
1 parent 9c45686 commit f37d4cb

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
@@ -3040,6 +3040,8 @@ def _check_globals(self, not_consumed: dict[str, nodes.NodeNG]) -> None:
30403040
return
30413041
for name, node_lst in not_consumed.items():
30423042
for node in node_lst:
3043+
if in_type_checking_block(node):
3044+
continue
30433045
self.add_message("unused-variable", args=(name,), node=node)
30443046

30453047
# 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)