From f53330b21832537282a49b31274573d473706fb5 Mon Sep 17 00:00:00 2001 From: tristanlatr <19967168+tristanlatr@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:15:39 -0400 Subject: [PATCH] Fix bug in assignments (#68) --- beniget/beniget.py | 2 +- tests/test_chains.py | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/beniget/beniget.py b/beniget/beniget.py index 5bfa863..0ed773e 100644 --- a/beniget/beniget.py +++ b/beniget/beniget.py @@ -1203,7 +1203,7 @@ def visit_Destructured(self, node): tmp_store, elt.ctx = elt.ctx, tmp_store self.visit(elt) tmp_store, elt.ctx = elt.ctx, tmp_store - elif isinstance(elt, (ast.Subscript, ast.Starred)): + elif isinstance(elt, (ast.Subscript, ast.Starred, ast.Attribute)): self.visit(elt) elif isinstance(elt, (ast.List, ast.Tuple)): self.visit_Destructured(elt) diff --git a/tests/test_chains.py b/tests/test_chains.py index 8520e81..5c90ead 100644 --- a/tests/test_chains.py +++ b/tests/test_chains.py @@ -561,7 +561,44 @@ class Attr(Attr):pass ['Attr -> (Attr -> (Attr -> ()))', 'Visitor -> ()']) self.assertEqual(c.dump_chains(node.body[-1]), ['Attr -> ()']) - + + @skipIf(sys.version_info < (3, 0), 'Python 3 syntax') + def test_star_assignment(self): + code = ''' +curr, *parts = [1,2,3] +while curr: + print(curr) + if parts: + curr, *parts = parts + else: + break +''' + self.checkChains(code, ['curr -> (curr -> (), curr -> (Call -> ()))', + 'parts -> (parts -> (), parts -> ())']*2) + + @skipIf(sys.version_info < (3, 0), 'Python 3 syntax') + def test_star_assignment_nested(self): + code = ''' +(curr, *parts),i = [1,2,3],0 +while curr: + print(curr) + if parts: + (curr, *parts),i = parts,i + else: + break +''' + self.checkChains(code, ['curr -> (curr -> (), curr -> (Call -> ()))', + 'parts -> (parts -> (), parts -> (Tuple -> ()))', + 'i -> (i -> (Tuple -> ()))']*2) + + def test_attribute_assignment(self): + code = "d=object();d.name,x = 't',1" + self.checkChains(code, ['d -> (d -> (Attribute -> ()))', + 'x -> ()']) + + def test_call_assignment(self): + code = "NameError().name = 't'" + self.checkChains(code, []) @skipIf(sys.version_info < (3, 0), 'Python 3 syntax') def test_annotation_uses_class_level_name(self):