Skip to content

Commit e71f63b

Browse files
authored
Fix crash from unexpected assignment (#8839)
1 parent 0560ddf commit e71f63b

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-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
@@ -609,7 +609,9 @@ def get_next_to_consume(self, node: nodes.Name) -> list[nodes.NodeNG] | None:
609609
and parent_node == found_nodes[0].parent
610610
):
611611
lhs = found_nodes[0].parent.targets[0]
612-
if lhs.name == name: # this name is defined in this very statement
612+
if (
613+
isinstance(lhs, nodes.AssignName) and lhs.name == name
614+
): # this name is defined in this very statement
613615
found_nodes = None
614616

615617
if (

tests/functional/u/used/used_before_assignment.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,9 @@ def give_me_none():
174174

175175
if give_me_none():
176176
print(ALL_DONE) # [used-before-assignment]
177+
178+
179+
attr = 'test' # pylint: disable=invalid-name
180+
class T: # pylint: disable=invalid-name, too-few-public-methods, undefined-variable
181+
'''Issue #8754, no crash from unexpected assignment between attribute and variable'''
182+
T.attr = attr

0 commit comments

Comments
 (0)