diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -337,7 +337,6 @@ def baz(): check('x=1\nfrom __future__ import division', 2, 1) check('foo(1=2)', 1, 5) check('def f():\n x, y: int', 2, 3) - check('[*x for x in xs]', 1, 2) check('foo(x for x in range(10), 100)', 1, 5) check('for 1 in []: pass', 1, 5) check('(yield i) = 2', 1, 2) @@ -2433,7 +2432,8 @@ def test_encodings(self): ) err = run_script(source.encode('cp437')) self.assertEqual(err[-3], ' "¢¢¢¢¢¢" + f(4, x for x in range(1))') - self.assertEqual(err[-2], ' ^^^') + self.assertEqual(err[-2], ' ^^^^^^^^^^^^^^^^^^^') + self.assertEqual(err[-1], 'SyntaxError: Generator expression must be parenthesized') # Check backwards tokenizer errors source = '# -*- coding: ascii -*-\n\n(\n' diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py --- a/Lib/test/test_unpack_ex.py +++ b/Lib/test/test_unpack_ex.py @@ -141,54 +141,214 @@ >>> {0:1, **{0:2}, 0:3, 0:4} {0: 4} -List comprehension element unpacking +Comprehension element unpacking >>> a, b, c = [0, 1, 2], 3, 4 >>> [*a, b, c] [0, 1, 2, 3, 4] >>> l = [a, (3, 4), {5}, {6: None}, (i for i in range(7, 10))] >>> [*item for item in l] + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + + >>> [*[0, 1] for i in range(5)] + [0, 1, 0, 1, 0, 1, 0, 1, 0, 1] + + >>> [*'a' for i in range(5)] + ['a', 'a', 'a', 'a', 'a'] + + >>> [*[] for i in range(10)] + [] + + >>> [*(x*2) for x in [[1, 2, 3], [], 'cat']] + [1, 2, 3, 1, 2, 3, 'c', 'a', 't', 'c', 'a', 't'] + + >>> {**{} for a in [1]} + {} + + >>> {**{7: i} for i in range(10)} + {7: 9} + + >>> dicts = [{1: 2}, {3: 4}, {5: 6, 7: 8}, {}, {9: 10}, {1: 0}] + >>> {**d for d in dicts} + {1: 0, 3: 4, 5: 6, 7: 8, 9: 10} + + >>> gen = (*(0, 1) for i in range(5)) + >>> next(gen) + 0 + >>> list(gen) + [1, 0, 1, 0, 1, 0, 1, 0, 1] + +Comprehension unpacking with conditionals and double loops + + >>> [*[i, i+1] for i in range(5) if i % 2 == 0] + [0, 1, 2, 3, 4, 5] + + >>> [*y for x in [[[0], [1, 2, 3], [], [4, 5]], [[6, 7]]] for y in x] + [0, 1, 2, 3, 4, 5, 6, 7] + + >>> [*y for x in [[[0], [1, 2, 3], [], [4, 5]], [[6, 7]]] for y in x if y and y[0]>0] + [1, 2, 3, 4, 5, 6, 7] + + >>> dicts = [{1: 2}, {3: 4}, {5: 6, 7: 8}, {}, {9: 10}, {1: 0}] + >>> {**d for d in dicts if len(d) != 2} + {1: 0, 3: 4, 9: 10} + +Scoping of assignment expressions in comprehensions + + >>> [*((y := i**2), 2*y) for i in range(4)] + [0, 0, 1, 2, 4, 8, 9, 18] + >>> y + 9 + + >>> [*(y := [i, i+1, i+2]) for i in range(4)] + [0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5] + >>> y + [3, 4, 5] + + >>> g = (*(z := [i, i+1, i+2]) for i in range(4)) + >>> z Traceback (most recent call last): ... - SyntaxError: iterable unpacking cannot be used in comprehension + NameError: name 'z' is not defined + >>> next(g) + 0 + >>> z + [0, 1, 2] + >>> next(g) + 1 + >>> z + [0, 1, 2] + >>> next(g) + 2 + >>> z + [0, 1, 2] + >>> next(g) + 1 + >>> z + [1, 2, 3] + + >>> x = [1, 2, 3] + >>> y = [4, 5, 6] + >>> def f(*args): + ... print(args) + + >>> f(*x if x else y) + (1, 2, 3) + + +Malformed comperehension element unpacking + + >>> [*x for x in [1, 2, 3]] + Traceback (most recent call last): + ... + [*x for x in [1, 2, 3]] + ^^ + TypeError: Value after * must be an iterable, not int + + +Error messages for specific failure modes of unpacking - >>> [*[0, 1] for i in range(10)] + >>> [*x if x else y for x in z] Traceback (most recent call last): ... - SyntaxError: iterable unpacking cannot be used in comprehension + [*x if x else y for x in z] + ^^^^^^^^^^^^^^ + SyntaxError: invalid starred expression. Did you forget to wrap the conditional expression in parentheses? - >>> [*'a' for i in range(10)] + >>> [*x if x else y] Traceback (most recent call last): ... - SyntaxError: iterable unpacking cannot be used in comprehension + [*x if x else y] + ^^^^^^^^^^^^^^ + SyntaxError: invalid starred expression. Did you forget to wrap the conditional expression in parentheses? - >>> [*[] for i in range(10)] + >>> [x if x else *y for x in z] Traceback (most recent call last): ... - SyntaxError: iterable unpacking cannot be used in comprehension + [x if x else *y for x in z] + ^ + SyntaxError: cannot unpack only part of a conditional expression - >>> {**{} for a in [1]} + >>> [x if x else *y] Traceback (most recent call last): ... - SyntaxError: dict unpacking cannot be used in dict comprehension + [x if x else *y] + ^ + SyntaxError: cannot unpack only part of a conditional expression -# Pegen is better here. -# Generator expression in function arguments + >>> {**x if x else y} + Traceback (most recent call last): + ... + {**x if x else y} + ^^^^^^^^^^^^^^^^ + SyntaxError: invalid double starred expression. Did you forget to wrap the conditional expression in parentheses? + >>> {x if x else **y} + Traceback (most recent call last): + ... + {x if x else **y} + ^^ + SyntaxError: cannot use dict unpacking on only part of a conditional expression -# >>> list(*x for x in (range(5) for i in range(3))) -# Traceback (most recent call last): -# ... -# list(*x for x in (range(5) for i in range(3))) -# ^ -# SyntaxError: invalid syntax + >>> [**x for x in [{1: 2}]] + Traceback (most recent call last): + ... + [**x for x in [{1: 2}]] + ^^^ + SyntaxError: cannot use dict unpacking in list comprehension + + >>> (**x for x in [{1:2}]) + Traceback (most recent call last): + ... + (**x for x in [{1:2}]) + ^^^ + SyntaxError: cannot use dict unpacking in generator expression >>> dict(**x for x in [{1:2}]) Traceback (most recent call last): ... dict(**x for x in [{1:2}]) - ^ - SyntaxError: invalid syntax + ^^^ + SyntaxError: cannot use dict unpacking in generator expression + + >>> {*a: b for a, b in {1: 2}.items()} + Traceback (most recent call last): + ... + {*a: b for a, b in {1: 2}.items()} + ^^ + SyntaxError: cannot use a starred expression in a dictionary key + + >>> {**a: b for a, b in {1: 2}.items()} + Traceback (most recent call last): + ... + {**a: b for a, b in {1: 2}.items()} + ^^^ + SyntaxError: cannot use dict unpacking in a dictionary key + + >>> {a: *b for a, b in {1: 2}.items()} + Traceback (most recent call last): + ... + {a: *b for a, b in {1: 2}.items()} + ^^ + SyntaxError: cannot use a starred expression in a dictionary value + + >>> {a: **b for a, b in {1: 2}.items()} + Traceback (most recent call last): + ... + {a: **b for a, b in {1: 2}.items()} + ^^^ + SyntaxError: cannot use dict unpacking in a dictionary value + + +# Generator expression in function arguments + + >>> list(*x for x in (range(5) for i in range(3))) + [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] + + >>> def f(arg): + ... print(type(arg), list(arg), list(arg)) + >>> f(*x for x in [[1,2,3]]) + [1, 2, 3] [] Iterable argument unpacking