Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10719.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.

Closes #10719
10 changes: 6 additions & 4 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,9 +1847,10 @@ def is_reassigned_before_current(node: nodes.NodeNG, varname: str) -> bool:
"""Check if the given variable name is reassigned in the same scope before the
current node.
"""
node_scope = node.scope()
return any(
a.name == varname and a.lineno < node.lineno
for a in node.scope().nodes_of_class(
a.name == varname and a.lineno < node.lineno and a.scope() == node_scope
for a in node_scope.nodes_of_class(
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
)
)
Expand All @@ -1859,9 +1860,10 @@ def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool:
"""Check if the given variable name is reassigned in the same scope after the
current node.
"""
node_scope = node.scope()
return any(
a.name == varname and a.lineno > node.lineno
for a in node.scope().nodes_of_class(
a.name == varname and a.lineno > node.lineno and a.scope() == node_scope
for a in node_scope.nodes_of_class(
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
)
)
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/c/const_class_attribute_same_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Test module-level constants with class attribute same name
Regression test for #10719.
"""
# pylint: disable=missing-docstring, too-few-public-methods, redefined-builtin


class Theme:
INPUT = ">>> "


INPUT = Theme()
input = Theme()
OUTPUT = Theme()
output = Theme()
Loading