Skip to content

Commit

Permalink
Fix crash from unexpected assignment (#8839)
Browse files Browse the repository at this point in the history
  • Loading branch information
zenlyj committed Jul 12, 2023
1 parent 0560ddf commit e71f63b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/8754.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix crash when a variable is assigned to a class attribute of identical name.

Closes #8754
4 changes: 3 additions & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,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 (
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/u/used/used_before_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,9 @@ def give_me_none():

if give_me_none():
print(ALL_DONE) # [used-before-assignment]


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

0 comments on commit e71f63b

Please sign in to comment.