diff --git a/doc/whatsnew/fragments/8754.bugfix b/doc/whatsnew/fragments/8754.bugfix new file mode 100644 index 0000000000..513ce7db15 --- /dev/null +++ b/doc/whatsnew/fragments/8754.bugfix @@ -0,0 +1,3 @@ +Fix crash when a variable is assigned to a class attribute of identical name. + +Closes #8754 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 03c9c2ae18..a83aa6e475 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -640,7 +640,9 @@ def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG] | None: and parent_node == found_nodes[0].parent ): lhs = found_nodes[0].parent.targets[0] - if lhs.name == name: # this name is defined in this very statement + if ( + isinstance(lhs, nodes.AssignName) and lhs.name == name + ): # this name is defined in this very statement found_nodes = None if ( diff --git a/tests/functional/u/used/used_before_assignment.py b/tests/functional/u/used/used_before_assignment.py index cb6d9c06ca..f7af9a7372 100644 --- a/tests/functional/u/used/used_before_assignment.py +++ b/tests/functional/u/used/used_before_assignment.py @@ -116,3 +116,8 @@ def turn_on2(**kwargs): var, *args = (1, "restore_dimmer_state") print(var, *args) + +attr = 'test' # pylint: disable=invalid-name +class T: # pylint: disable=invalid-name, too-few-public-methods, undefined-variable + '''Issue #8754, no crash from unexpected assignment between attribute and variable''' + T.attr = attr