diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | astroid/inference.py | 5 | ||||
-rw-r--r-- | tests/unittest_inference.py | 18 |
3 files changed, 25 insertions, 3 deletions
@@ -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() |