Commit e7c7730c authored by Stefan Behnel's avatar Stefan Behnel

Implement type inference for stared unpacking targets as list.

parent dbc563c4
......@@ -778,8 +778,11 @@ class ControlFlowAnalysis(CythonTransform):
self.flow.mark_assignment(lhs, rhs, entry)
elif lhs.is_sequence_constructor:
for i, arg in enumerate(lhs.args):
if not rhs or arg.is_starred:
item_node = None
if arg.is_starred:
# "a, *b = x" assigns a list to "b"
item_node = TypedExprNode(Builtin.list_type, may_be_none=False, pos=arg.pos)
elif rhs is self.object_expr:
item_node = rhs
else:
item_node = rhs.inferable_item_node(i)
self.mark_assignment(arg, item_node)
......
......@@ -287,6 +287,35 @@ def cascaded_assignment():
e = a + b + c + d
assert typeof(e) == "double"
def unpacking(x):
"""
>>> unpacking(0)
"""
a, b, c, d = x, 1, 2.0, [3]
assert typeof(a) == "Python object", typeof(a)
assert typeof(b) == "long", typeof(b)
assert typeof(c) == "double", typeof(c)
assert typeof(d) == "list object", typeof(d)
def star_unpacking(*x):
"""
>>> star_unpacking(1, 2)
"""
a, b = x
c, *d = x
*e, f = x
*g, g = x # re-assignment
assert typeof(a) == "Python object", typeof(a)
assert typeof(b) == "Python object", typeof(b)
assert typeof(c) == "Python object", typeof(c)
assert typeof(d) == "list object", typeof(d)
assert typeof(e) == "list object", typeof(e)
assert typeof(f) == "Python object", typeof(f)
assert typeof(g) == "Python object", typeof(f)
def increment():
"""
>>> increment()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment