Skip to content

Commit 6459607

Browse files
fix: avoid false positive when module-level names match class-level names
- Add scope comparision to avoid module-level constants to be incorrectly classified as variables when a class-level attribute with the same name exists Closes #10719
1 parent 4f0716a commit 6459607

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed false positive for ``invalid-name`` where module-level constants were incorrectly classified as variables when a class-level attribute with the same name exists.
2+
3+
Closes #10719

pylint/checkers/utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,9 +1847,10 @@ def is_reassigned_before_current(node: nodes.NodeNG, varname: str) -> bool:
18471847
"""Check if the given variable name is reassigned in the same scope before the
18481848
current node.
18491849
"""
1850+
node_scope = node.scope()
18501851
return any(
1851-
a.name == varname and a.lineno < node.lineno
1852-
for a in node.scope().nodes_of_class(
1852+
a.name == varname and a.lineno < node.lineno and a.scope() == node_scope
1853+
for a in node_scope.nodes_of_class(
18531854
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
18541855
)
18551856
)
@@ -1859,9 +1860,10 @@ def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool:
18591860
"""Check if the given variable name is reassigned in the same scope after the
18601861
current node.
18611862
"""
1863+
node_scope = node.scope()
18621864
return any(
1863-
a.name == varname and a.lineno > node.lineno
1864-
for a in node.scope().nodes_of_class(
1865+
a.name == varname and a.lineno > node.lineno and a.scope() == node_scope
1866+
for a in node_scope.nodes_of_class(
18651867
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
18661868
)
18671869
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Test module-level constants with class attribute same name
2+
Regression test for #10719.
3+
"""
4+
# pylint: disable=missing-docstring, too-few-public-methods, redefined-builtin
5+
6+
7+
class Theme:
8+
INPUT = ">>> "
9+
10+
11+
INPUT = Theme()
12+
input = Theme()
13+
OUTPUT = Theme()
14+
output = Theme()

0 commit comments

Comments
 (0)