Skip to content

Commit b5e9607

Browse files
h1whelanclaude
authored andcommitted
Fix false positive unused-variable for globals matching dummy-variables-rgx
When `allow-global-unused-variables` is disabled, global variables whose names match `dummy-variables-rgx` were still reported as unused (W0612). This adds a check for the dummy variable regex in `_check_globals`, consistent with how local variables are already handled. Closes #10890 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 6ae32db commit b5e9607

5 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix a false positive for ``unused-variable`` where global variables matching
2+
``dummy-variables-rgx`` were still reported as unused when
3+
``allow-global-unused-variables`` was disabled.
4+
5+
Closes #10890

pylint/checkers/variables.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,6 +3286,8 @@ def _check_globals(self, not_consumed: Consumption) -> None:
32863286
and node.modname == "__future__"
32873287
):
32883288
continue
3289+
if self._is_name_ignored(node, name):
3290+
continue
32893291
self.add_message("unused-variable", args=(name,), node=node)
32903292

32913293
# pylint: disable = too-many-branches
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# pylint: disable=missing-docstring,invalid-name
2+
3+
# Test that global variables matching dummy-variables-rgx are not
4+
# reported as unused (issue #10890).
5+
6+
_ = [] # This should NOT trigger unused-variable
7+
__ = {} # This should NOT trigger unused-variable
8+
9+
var = "pylint" # [unused-variable]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[variables]
2+
allow-global-unused-variables=no
3+
dummy-variables-rgx=_+$
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unused-variable:9:0:9:3::Unused variable 'var':UNDEFINED

0 commit comments

Comments
 (0)