Skip to content

Commit c68007e

Browse files
Fix crash from unexpected assignment (#8839) (#8843)
(cherry picked from commit e71f63b) Co-authored-by: Zen Lee <[email protected]>
1 parent 1928589 commit c68007e

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/whatsnew/fragments/8754.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix crash when a variable is assigned to a class attribute of identical name.
2+
3+
Closes #8754

pylint/checkers/variables.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,9 @@ def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG] | None:
640640
and parent_node == found_nodes[0].parent
641641
):
642642
lhs = found_nodes[0].parent.targets[0]
643-
if lhs.name == name: # this name is defined in this very statement
643+
if (
644+
isinstance(lhs, nodes.AssignName) and lhs.name == name
645+
): # this name is defined in this very statement
644646
found_nodes = None
645647

646648
if (

tests/functional/u/used/used_before_assignment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,8 @@ def turn_on2(**kwargs):
116116
var, *args = (1, "restore_dimmer_state")
117117

118118
print(var, *args)
119+
120+
attr = 'test' # pylint: disable=invalid-name
121+
class T: # pylint: disable=invalid-name, too-few-public-methods, undefined-variable
122+
'''Issue #8754, no crash from unexpected assignment between attribute and variable'''
123+
T.attr = attr

0 commit comments

Comments
 (0)