Skip to content

Commit 54bd2f1

Browse files
committed
Fix consider-using-augmented-assign nested attribute access
1 parent b87cf16 commit 54bd2f1

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix ``consider-using-augmented-assign`` checker for nested attribute access.
2+
3+
Closes #8086

pylint/checkers/utils.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,22 +2066,6 @@ def is_hashable(node: nodes.NodeNG) -> bool:
20662066
return True
20672067

20682068

2069-
def get_full_name_of_attribute(node: nodes.Attribute | nodes.AssignAttr) -> str:
2070-
"""Return the full name of an attribute and the classes it belongs to.
2071-
2072-
For example: "Class1.Class2.attr"
2073-
"""
2074-
parent = node.parent
2075-
ret = node.attrname or ""
2076-
while isinstance(parent, (nodes.Attribute, nodes.Name)):
2077-
if isinstance(parent, nodes.Attribute):
2078-
ret = f"{parent.attrname}.{ret}"
2079-
else:
2080-
ret = f"{parent.name}.{ret}"
2081-
parent = parent.parent
2082-
return ret
2083-
2084-
20852069
def _is_target_name_in_binop_side(
20862070
target: nodes.AssignName | nodes.AssignAttr, side: nodes.NodeNG | None
20872071
) -> bool:
@@ -2091,7 +2075,7 @@ def _is_target_name_in_binop_side(
20912075
return target.name == side.name # type: ignore[no-any-return]
20922076
return False
20932077
if isinstance(side, nodes.Attribute) and isinstance(target, nodes.AssignAttr):
2094-
return get_full_name_of_attribute(target) == get_full_name_of_attribute(side)
2078+
return target.as_string() == side.as_string() # type: ignore[no-any-return]
20952079
return False
20962080

20972081

tests/functional/ext/code_style/cs_consider_using_augmented_assign.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for consider-using-augmented-assign."""
22

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

55
from unknown import Unknown
66

@@ -119,3 +119,17 @@ def return_str() -> str:
119119

120120
x = x <= 3
121121
x = 3 <= x
122+
123+
124+
# https://github.com/PyCQA/pylint/issues/8086
125+
# consider-using-augmented-assign should only be flagged
126+
# if names attribute names match exactly.
127+
128+
class A:
129+
def __init__(self) -> None:
130+
self.a = 1
131+
self.b = A()
132+
133+
def test(self) -> None:
134+
self.a = self.a + 1 # [consider-using-augmented-assign]
135+
self.b.a = self.a + 1 # Names don't match!

tests/functional/ext/code_style/cs_consider_using_augmented_assign.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ consider-using-augmented-assign:104:0:104:9::Use '&=' to do an augmented assign
2424
consider-using-augmented-assign:105:0:105:9::Use '&=' to do an augmented assign directly:INFERENCE
2525
consider-using-augmented-assign:108:0:108:9::Use '|=' to do an augmented assign directly:INFERENCE
2626
consider-using-augmented-assign:109:0:109:9::Use '|=' to do an augmented assign directly:INFERENCE
27+
consider-using-augmented-assign:134:8:134:27:A.test:Use '+=' to do an augmented assign directly:INFERENCE

0 commit comments

Comments
 (0)