Skip to content

Fix consider-using-augmented-assign nested attribute access #8088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2066,22 +2066,6 @@ def is_hashable(node: nodes.NodeNG) -> bool:
return True


def get_full_name_of_attribute(node: nodes.Attribute | nodes.AssignAttr) -> str:
"""Return the full name of an attribute and the classes it belongs to.

For example: "Class1.Class2.attr"
"""
parent = node.parent
ret = node.attrname or ""
while isinstance(parent, (nodes.Attribute, nodes.Name)):
if isinstance(parent, nodes.Attribute):
ret = f"{parent.attrname}.{ret}"
else:
ret = f"{parent.name}.{ret}"
parent = parent.parent
return ret


def _is_target_name_in_binop_side(
target: nodes.AssignName | nodes.AssignAttr, side: nodes.NodeNG | None
) -> bool:
Expand All @@ -2091,7 +2075,7 @@ def _is_target_name_in_binop_side(
return target.name == side.name # type: ignore[no-any-return]
return False
if isinstance(side, nodes.Attribute) and isinstance(target, nodes.AssignAttr):
return get_full_name_of_attribute(target) == get_full_name_of_attribute(side)
return target.as_string() == side.as_string() # type: ignore[no-any-return]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that's clever ! And probably faster too.

return False


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Tests for consider-using-augmented-assign."""

# pylint: disable=invalid-name,too-few-public-methods,import-error,consider-using-f-string
# pylint: disable=invalid-name,too-few-public-methods,import-error,consider-using-f-string,missing-docstring

from unknown import Unknown

Expand Down Expand Up @@ -119,3 +119,17 @@ def return_str() -> str:

x = x <= 3
x = 3 <= x


# https://github.com/PyCQA/pylint/issues/8086
# consider-using-augmented-assign should only be flagged
# if names attribute names match exactly.

class A:
def __init__(self) -> None:
self.a = 1
self.b = A()

def test(self) -> None:
self.a = self.a + 1 # [consider-using-augmented-assign]
self.b.a = self.a + 1 # Names don't match!
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ consider-using-augmented-assign:104:0:104:9::Use '&=' to do an augmented assign
consider-using-augmented-assign:105:0:105:9::Use '&=' to do an augmented assign directly:INFERENCE
consider-using-augmented-assign:108:0:108:9::Use '|=' to do an augmented assign directly:INFERENCE
consider-using-augmented-assign:109:0:109:9::Use '|=' to do an augmented assign directly:INFERENCE
consider-using-augmented-assign:134:8:134:27:A.test:Use '+=' to do an augmented assign directly:INFERENCE