Skip to content

Commit

Permalink
Fix crash from unexpected assignment (#8839) (#8843)
Browse files Browse the repository at this point in the history
(cherry picked from commit e71f63b)

Co-authored-by: Zen Lee <53538590+zenlyj@users.noreply.github.com>
  • Loading branch information
jacobtylerwalls and zenlyj committed Jul 12, 2023
1 parent 1928589 commit c68007e
Show file tree
Hide file tree
Showing 3 changed files with 11 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 @@ -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 (
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/u/used/used_before_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c68007e

Please sign in to comment.