File size: 7,714 Bytes
a4edccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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]])
+    <class 'generator'> [1, 2, 3] []
 
 Iterable argument unpacking