summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-01-05 13:35:28 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2020-01-05 13:37:53 +0100
commit631539e8ca8cbdb73b1c8655214de10bdd51e466 (patch)
tree3c3a6d91032e961e12c58e6c5f4dff2d8850f512
parentdd8bcd3f640f947d91d8179dc98e3ccb4613f190 (diff)
downloadastroid-git-631539e8ca8cbdb73b1c8655214de10bdd51e466.tar.gz
Use the parent of the node when inferring aug assign nodes instead of the statement
In 19b5af02304e3339fdd2a26cfafc337960eeebce we added a check for `AugAssign` nodes when inferring `Assign` values. Unfortunately the `infer_assign` function was also used by `infer_assignname`, which meant that when trying to infer an `AssignName` node, we were inferring its value as the result of the `AugAssign` inference, leading to spurious false positives. Close PyCQA/pylint#2911 Close PyCQA/pylint#3214
-rw-r--r--ChangeLog5
-rw-r--r--astroid/inference.py5
-rw-r--r--tests/unittest_inference.py18
3 files changed, 25 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index e1419a79..c54d321a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -12,6 +12,11 @@ Release Date: TBA
Close #666
+* Use the parent of the node when inferring aug assign nodes instead of the statement
+
+ Close PyCQA/pylint#2911
+ Close PyCQA/pylint#3214
+
* Added some functions to the ``brain_numpy_core_umath`` module
Close PyCQA/pylint#3319
diff --git a/astroid/inference.py b/astroid/inference.py
index a563287d..9c613cfe 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -837,9 +837,8 @@ def infer_assign(self, context=None):
"""infer a AssignName/AssignAttr: need to inspect the RHS part of the
assign node
"""
- stmt = self.statement()
- if isinstance(stmt, nodes.AugAssign):
- return stmt.infer(context)
+ if isinstance(self.parent, nodes.AugAssign):
+ return self.parent.infer(context)
stmts = list(self.assigned_stmts(context=context))
return bases._infer_stmts(stmts, context)
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index a5ed96b1..008e3d1f 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -5516,5 +5516,23 @@ def test_recursion_error_inferring_builtin_containers():
helpers.safe_infer(node.targets[0])
+def test_inferaugassign_picking_parent_instead_of_stmt():
+ code = """
+ from collections import namedtuple
+ SomeClass = namedtuple('SomeClass', ['name'])
+ items = [SomeClass(name='some name')]
+
+ some_str = ''
+ some_str += ', '.join(__(item) for item in items)
+ """
+ # item needs to be inferrd as `SomeClass` but it was inferred
+ # as a string because the entire `AugAssign` node was inferred
+ # as a string.
+ node = extract_node(code)
+ inferred = next(node.infer())
+ assert isinstance(inferred, Instance)
+ assert inferred.name == "SomeClass"
+
+
if __name__ == "__main__":
unittest.main()