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
40 changes: 28 additions & 12 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,24 +1847,40 @@ 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.
"""
return any(
a.name == varname and a.lineno < node.lineno
for a in node.scope().nodes_of_class(
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
)
)
node_scope = node.scope()
node_lineno = node.lineno
if node_lineno is None:
return False
for a in node_scope.nodes_of_class(
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
):
if a.name == varname and a.lineno is not None and a.lineno < node_lineno:
if isinstance(a, (nodes.ClassDef, nodes.FunctionDef)):
if a.parent and a.parent.scope() == node_scope:
return True
Comment on lines +1858 to +1860
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I remove this condition I get no test failures. I'd like to see a test to make sure this is what we want.

elif a.scope() == node_scope:
return True
return False
Comment on lines +1850 to +1863
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we factor out the duplication with the other util?



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.
"""
return any(
a.name == varname and a.lineno > node.lineno
for a in node.scope().nodes_of_class(
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
)
)
node_scope = node.scope()
node_lineno = node.lineno
if node_lineno is None:
return False
for a in node_scope.nodes_of_class(
(nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
):
if a.name == varname and a.lineno is not None and a.lineno > node_lineno:
if isinstance(a, (nodes.ClassDef, nodes.FunctionDef)):
if a.parent and a.parent.scope() == node_scope:
return True
elif a.scope() == node_scope:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is is usually used for specific node comparisons.

return True
return False


def is_deleted_after_current(node: nodes.NodeNG, varname: str) -> bool:
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()
1 change: 1 addition & 0 deletions tests/functional/n/no/no_dummy_redefined.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Make sure warnings about redefinitions do not trigger for dummy variables."""
# pylint: disable=invalid-name


_, INTERESTING = 'a=b'.split('=')
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/n/no/no_dummy_redefined.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
redefined-outer-name:11:4:11:9:clobbering:Redefining name 'value' from outer scope (line 6):UNDEFINED
redefined-outer-name:12:4:12:9:clobbering:Redefining name 'value' from outer scope (line 7):UNDEFINED