blob: 75b778d20e90a9a46ac5bd9b1664ac9a7d8bd77c [file] [log] [blame]
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001"""This module tests SyntaxErrors.
2
3Here's an example of the sort of thing that is tested.
4
5>>> def f(x):
6... global x
7Traceback (most recent call last):
Nick Coghlan650f0d02007-04-15 12:05:43 +00008SyntaxError: name 'x' is parameter and global
Jeremy Hyltonc960f262006-01-27 15:18:39 +00009
10The tests are all raise SyntaxErrors. They were created by checking
11each C call that raises SyntaxError. There are several modules that
12raise these exceptions-- ast.c, compile.c, future.c, pythonrun.c, and
13symtable.c.
14
15The parser itself outlaws a lot of invalid syntax. None of these
16errors are tested here at the moment. We should add some tests; since
17there are infinitely many programs with invalid syntax, we would need
18to be judicious in selecting some.
19
20The compiler generates a synthetic module name for code executed by
21doctest. Since all the code comes from the same module, a suffix like
22[1] is appended to the module name, As a consequence, changing the
23order of tests in this module means renumbering all the errors after
24it. (Maybe we should enable the ellipsis option for these tests.)
25
26In ast.c, syntax errors are raised by calling ast_error().
27
28Errors from set_context():
29
Jeremy Hyltonc960f262006-01-27 15:18:39 +000030>>> obj.None = 1
31Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +000032SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +000033
34>>> None = 1
35Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020036SyntaxError: cannot assign to None
37
38>>> obj.True = 1
39Traceback (most recent call last):
40SyntaxError: invalid syntax
41
42>>> True = 1
43Traceback (most recent call last):
44SyntaxError: cannot assign to True
45
Serhiy Storchakad8b3a982019-03-05 20:42:06 +020046>>> (True := 1)
47Traceback (most recent call last):
Ned Batchelder37143a82019-12-31 21:40:58 -050048SyntaxError: cannot use assignment expressions with True
Serhiy Storchakad8b3a982019-03-05 20:42:06 +020049
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020050>>> obj.__debug__ = 1
51Traceback (most recent call last):
52SyntaxError: cannot assign to __debug__
53
54>>> __debug__ = 1
55Traceback (most recent call last):
56SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +000057
Stéphane Wirtel3ad91672019-02-21 11:11:53 +010058>>> (__debug__ := 1)
59Traceback (most recent call last):
60SyntaxError: cannot assign to __debug__
61
Jeremy Hyltonc960f262006-01-27 15:18:39 +000062>>> f() = 1
63Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010064SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000065
Pablo Galindo9f495902020-06-08 02:57:00 +010066>>> yield = 1
67Traceback (most recent call last):
68SyntaxError: assignment to yield expression not possible
69
Shantanu27c0d9b2020-05-11 14:53:58 -070070>>> del f()
71Traceback (most recent call last):
72SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000073
74>>> a + 1 = 2
75Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010076SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000077
78>>> (x for x in x) = 1
79Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020080SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000081
82>>> 1 = 1
83Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010084SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000085
86>>> "abc" = 1
87Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010088SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000089
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050090>>> b"" = 1
91Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010092SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020093
94>>> ... = 1
95Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010096SyntaxError: cannot assign to Ellipsis here. Maybe you meant '==' instead of '='?
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050097
Jeremy Hyltonc960f262006-01-27 15:18:39 +000098>>> `1` = 1
99Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +0000100SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000101
102If the left-hand side of an assignment is a list or tuple, an illegal
103expression inside that contain should still cause a syntax error.
104This test just checks a couple of cases rather than enumerating all of
105them.
106
Pablo Galindo16ab0702020-05-15 02:04:52 +0100107>>> (a, "b", c) = (1, 2, 3)
108Traceback (most recent call last):
109SyntaxError: cannot assign to literal
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200110
Pablo Galindo16ab0702020-05-15 02:04:52 +0100111>>> (a, True, c) = (1, 2, 3)
112Traceback (most recent call last):
113SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200114
115>>> (a, __debug__, c) = (1, 2, 3)
116Traceback (most recent call last):
117SyntaxError: cannot assign to __debug__
118
Pablo Galindo16ab0702020-05-15 02:04:52 +0100119>>> (a, *True, c) = (1, 2, 3)
120Traceback (most recent call last):
121SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200122
123>>> (a, *__debug__, c) = (1, 2, 3)
124Traceback (most recent call last):
125SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000126
Pablo Galindo16ab0702020-05-15 02:04:52 +0100127>>> [a, b, c + 1] = [1, 2, 3]
128Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100129SyntaxError: cannot assign to expression
Pablo Galindo16ab0702020-05-15 02:04:52 +0100130
131>>> [a, b[1], c + 1] = [1, 2, 3]
132Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100133SyntaxError: cannot assign to expression
Pablo Galindo16ab0702020-05-15 02:04:52 +0100134
135>>> [a, b.c.d, c + 1] = [1, 2, 3]
136Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100137SyntaxError: cannot assign to expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000138
Thomas Wouters477c8d52006-05-27 19:21:47 +0000139>>> a if 1 else b = 1
140Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200141SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000142
Pablo Galindo9f495902020-06-08 02:57:00 +0100143>>> True = True = 3
144Traceback (most recent call last):
145SyntaxError: cannot assign to True
146
147>>> x = y = True = z = 3
148Traceback (most recent call last):
149SyntaxError: cannot assign to True
150
151>>> x = y = yield = 1
152Traceback (most recent call last):
153SyntaxError: assignment to yield expression not possible
154
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300155>>> a, b += 1, 2
156Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100157SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300158
159>>> (a, b) += 1, 2
160Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100161SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300162
163>>> [a, b] += 1, 2
164Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100165SyntaxError: 'list' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300166
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300167Invalid targets in `for` loops and `with` statements should also
168produce a specialized error message
169
170>>> for a() in b: pass
171Traceback (most recent call last):
172SyntaxError: cannot assign to function call
173
174>>> for (a, b()) in b: pass
175Traceback (most recent call last):
176SyntaxError: cannot assign to function call
177
178>>> for [a, b()] in b: pass
179Traceback (most recent call last):
180SyntaxError: cannot assign to function call
181
182>>> for (*a, b, c+1) in b: pass
183Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100184SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300185
186>>> for (x, *(y, z.d())) in b: pass
187Traceback (most recent call last):
188SyntaxError: cannot assign to function call
189
190>>> for a, b() in c: pass
191Traceback (most recent call last):
192SyntaxError: cannot assign to function call
193
194>>> for a, b, (c + 1, d()): pass
195Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100196SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300197
198>>> for i < (): pass
199Traceback (most recent call last):
200SyntaxError: invalid syntax
201
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300202>>> for a, b
203Traceback (most recent call last):
204SyntaxError: invalid syntax
205
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300206>>> with a as b(): pass
207Traceback (most recent call last):
208SyntaxError: cannot assign to function call
209
210>>> with a as (b, c()): pass
211Traceback (most recent call last):
212SyntaxError: cannot assign to function call
213
214>>> with a as [b, c()]: pass
215Traceback (most recent call last):
216SyntaxError: cannot assign to function call
217
218>>> with a as (*b, c, d+1): pass
219Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100220SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300221
222>>> with a as (x, *(y, z.d())): pass
223Traceback (most recent call last):
224SyntaxError: cannot assign to function call
225
226>>> with a as b, c as d(): pass
227Traceback (most recent call last):
228SyntaxError: cannot assign to function call
229
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300230>>> with a as b
231Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000232SyntaxError: expected ':'
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300233
Pablo Galindo9f495902020-06-08 02:57:00 +0100234>>> p = p =
235Traceback (most recent call last):
236SyntaxError: invalid syntax
237
Pablo Galindo835f14f2021-01-31 22:52:56 +0000238Comprehensions creating tuples without parentheses
239should produce a specialized error message:
240
241>>> [x,y for x,y in range(100)]
242Traceback (most recent call last):
243SyntaxError: did you forget parentheses around the comprehension target?
244
245>>> {x,y for x,y in range(100)}
246Traceback (most recent call last):
247SyntaxError: did you forget parentheses around the comprehension target?
248
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000249# Missing commas in literals collections should not
250# produce special error messages regarding missing
251# parentheses
252
253>>> [1, 2 3]
Pablo Galindo835f14f2021-01-31 22:52:56 +0000254Traceback (most recent call last):
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000255SyntaxError: invalid syntax
256
257>>> {1, 2 3}
258Traceback (most recent call last):
259SyntaxError: invalid syntax
260
261>>> {1:2, 2:5 3:12}
262Traceback (most recent call last):
263SyntaxError: invalid syntax
264
265>>> (1, 2 3)
266Traceback (most recent call last):
267SyntaxError: invalid syntax
Pablo Galindo835f14f2021-01-31 22:52:56 +0000268
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000269From compiler_complex_args():
270
271>>> def f(None=1):
272... pass
273Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000274SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000275
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000276From ast_for_arguments():
277
278>>> def f(x, y=1, z):
279... pass
280Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000281SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000282
283>>> def f(x, None):
284... pass
285Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000286SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000287
288>>> def f(*None):
289... pass
290Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000291SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000292
293>>> def f(**None):
294... pass
295Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000296SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000297
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300298>>> import ast; ast.parse('''
299... def f(
300... *, # type: int
301... a, # type: int
302... ):
303... pass
304... ''', type_comments=True)
305Traceback (most recent call last):
306SyntaxError: bare * has associated type comment
307
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000308
309From ast_for_funcdef():
310
311>>> def None(x):
312... pass
313Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000314SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000315
316
317From ast_for_call():
318
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200319>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000320... return list(it)
321>>> L = range(10)
322>>> f(x for x in L)
323[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
324>>> f(x for x in L, 1)
325Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200326SyntaxError: Generator expression must be parenthesized
327>>> f(x for x in L, y=1)
328Traceback (most recent call last):
329SyntaxError: Generator expression must be parenthesized
330>>> f(x for x in L, *[])
331Traceback (most recent call last):
332SyntaxError: Generator expression must be parenthesized
333>>> f(x for x in L, **{})
334Traceback (most recent call last):
335SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300336>>> f(L, x for x in L)
337Traceback (most recent call last):
338SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400339>>> f(x for x in L, y for y in L)
340Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200341SyntaxError: Generator expression must be parenthesized
342>>> f(x for x in L,)
343Traceback (most recent call last):
344SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000345>>> f((x for x in L), 1)
346[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200347>>> class C(x for x in L):
348... pass
349Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000350SyntaxError: expected ':'
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000351
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200352>>> def g(*args, **kwargs):
353... print(args, sorted(kwargs.items()))
354>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
355... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
356... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
357... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
358... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
359... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
360... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
361... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
362... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
363... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
364... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
365... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
366... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
367... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
368... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
369... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
370... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
371... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
372... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
373... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
374(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000375
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200376>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
377... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
378... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
379... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
380... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
381... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
382... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
383... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
384... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
385... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
386... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
387... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
388... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
389... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
390... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
391... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
392... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
393... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
394... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
395... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
396... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
397... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
398... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
399... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
400... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
401... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
402... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
403... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
404... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
405... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
406... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
407... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
408... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
409... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
410... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
411... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
412... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
413... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
414... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
415... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
416... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
417... # doctest: +ELLIPSIS
418() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000419
Yury Selivanovf2392132016-12-13 19:03:51 -0500420>>> class C:
421... def meth(self, *args):
422... return args
423>>> obj = C()
424>>> obj.meth(
425... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
426... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
427... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
428... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
429... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
430... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
431... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
432... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
433... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
434... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
435... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
436... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
437... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
438... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
439... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
440... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
441... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
442... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
443... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
444... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
445(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
446
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100447# >>> f(lambda x: x[0] = 3)
448# Traceback (most recent call last):
449# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000450
451The grammar accepts any test (basically, any expression) in the
452keyword slot of a call site. Test a few different options.
453
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100454# >>> f(x()=2)
455# Traceback (most recent call last):
456# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
457# >>> f(a or b=1)
458# Traceback (most recent call last):
459# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
460# >>> f(x.y=1)
461# Traceback (most recent call last):
462# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
463# >>> f((x)=2)
464# Traceback (most recent call last):
465# SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
466# >>> f(True=2)
467# Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100468# SyntaxError: cannot assign to True here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200469>>> f(__debug__=1)
470Traceback (most recent call last):
471SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100472>>> __debug__: int
473Traceback (most recent call last):
474SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000475
476
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000477More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000478
479>>> (x for x in x) += 1
480Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100481SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000482>>> None += 1
483Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100484SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200485>>> __debug__ += 1
486Traceback (most recent call last):
487SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000488>>> f() += 1
489Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100490SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000491
492
493Test continue in finally in weird combinations.
494
Ezio Melotti13925002011-03-16 11:05:33 +0200495continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000496
497 >>> def test():
498 ... try:
499 ... pass
500 ... finally:
501 ... for abc in range(10):
502 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000503 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000504 >>> test()
505 9
506
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200507continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000508
509 >>> def test():
510 ... for abc in range(10):
511 ... try:
512 ... pass
513 ... finally:
514 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200515 ... print(abc)
516 >>> test()
517 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000518
519 >>> def test():
520 ... for abc in range(10):
521 ... try:
522 ... pass
523 ... finally:
524 ... try:
525 ... continue
526 ... except:
527 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200528 ... print(abc)
529 >>> test()
530 9
531
532 >>> def test():
533 ... for abc in range(10):
534 ... try:
535 ... pass
536 ... finally:
537 ... try:
538 ... pass
539 ... except:
540 ... continue
541 ... print(abc)
542 >>> test()
543 9
544
545A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000546
547 >>> def foo():
548 ... try:
549 ... pass
550 ... finally:
551 ... continue
552 Traceback (most recent call last):
553 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200554 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555
556There is one test for a break that is not in a loop. The compiler
557uses a single data structure to keep track of try-finally and loops,
558so we need to be sure that a break is actually inside a loop. If it
559isn't, there should be a syntax error.
560
561 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000562 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000563 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000564 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000566 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000567 Traceback (most recent call last):
568 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000569 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000570
Benjamin Petersone09ed542016-07-14 22:00:03 -0700571This raises a SyntaxError, it used to raise a SystemError.
572Context for this change can be found on issue #27514
573
Thomas Wouters89f507f2006-12-13 04:49:30 +0000574In 2.5 there was a missing exception and an assert was triggered in a debug
575build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
576
577 >>> while 1:
578 ... while 2:
579 ... while 3:
580 ... while 4:
581 ... while 5:
582 ... while 6:
583 ... while 8:
584 ... while 9:
585 ... while 10:
586 ... while 11:
587 ... while 12:
588 ... while 13:
589 ... while 14:
590 ... while 15:
591 ... while 16:
592 ... while 17:
593 ... while 18:
594 ... while 19:
595 ... while 20:
596 ... while 21:
597 ... while 22:
598 ... break
599 Traceback (most recent call last):
600 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700601 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000602
Guido van Rossum6cff8742016-09-09 09:36:26 -0700603Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
604
605 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200606 ... print(x)
607 ... global x
608 Traceback (most recent call last):
609 ...
610 SyntaxError: name 'x' is used prior to global declaration
611
612 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700613 ... x = 1
614 ... global x
615 Traceback (most recent call last):
616 ...
617 SyntaxError: name 'x' is assigned to before global declaration
618
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200619 >>> def f(x):
620 ... global x
621 Traceback (most recent call last):
622 ...
623 SyntaxError: name 'x' is parameter and global
624
Guido van Rossum6cff8742016-09-09 09:36:26 -0700625 >>> def f():
626 ... x = 1
627 ... def g():
628 ... print(x)
629 ... nonlocal x
630 Traceback (most recent call last):
631 ...
632 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000633
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300634 >>> def f():
635 ... x = 1
636 ... def g():
637 ... x = 2
638 ... nonlocal x
639 Traceback (most recent call last):
640 ...
641 SyntaxError: name 'x' is assigned to before nonlocal declaration
642
Jeremy Hylton81e95022007-02-27 06:50:52 +0000643 >>> def f(x):
644 ... nonlocal x
645 Traceback (most recent call last):
646 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000647 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000648
Jeremy Hylton81e95022007-02-27 06:50:52 +0000649 >>> def f():
650 ... global x
651 ... nonlocal x
652 Traceback (most recent call last):
653 ...
654 SyntaxError: name 'x' is nonlocal and global
655
656 >>> def f():
657 ... nonlocal x
658 Traceback (most recent call last):
659 ...
660 SyntaxError: no binding for nonlocal 'x' found
661
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000662From SF bug #1705365
663 >>> nonlocal x
664 Traceback (most recent call last):
665 ...
666 SyntaxError: nonlocal declaration not allowed at module level
667
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300668From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600669 >>> class A:
670 ... def f(self):
671 ... nonlocal __x
672 Traceback (most recent call last):
673 ...
674 SyntaxError: no binding for nonlocal '_A__x' found
675
Guido van Rossumd8faa362007-04-27 19:54:29 +0000676
677This tests assignment-context; there was a bug in Python 2.5 where compiling
678a complex 'if' (one with 'elif') would fail to notice an invalid suite,
679leading to spurious errors.
680
681 >>> if 1:
682 ... x() = 1
683 ... elif 1:
684 ... pass
685 Traceback (most recent call last):
686 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100687 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000688
689 >>> if 1:
690 ... pass
691 ... elif 1:
692 ... x() = 1
693 Traceback (most recent call last):
694 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100695 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000696
697 >>> if 1:
698 ... x() = 1
699 ... elif 1:
700 ... pass
701 ... else:
702 ... pass
703 Traceback (most recent call last):
704 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100705 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000706
707 >>> if 1:
708 ... pass
709 ... elif 1:
710 ... x() = 1
711 ... else:
712 ... pass
713 Traceback (most recent call last):
714 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100715 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000716
717 >>> if 1:
718 ... pass
719 ... elif 1:
720 ... pass
721 ... else:
722 ... x() = 1
723 Traceback (most recent call last):
724 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100725 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hylton81e95022007-02-27 06:50:52 +0000726
Pablo Galindo58fb1562021-02-02 19:54:22 +0000727 Missing ':' before suites:
728
729 >>> def f()
730 ... pass
731 Traceback (most recent call last):
732 SyntaxError: expected ':'
733
734 >>> class A
735 ... pass
736 Traceback (most recent call last):
737 SyntaxError: expected ':'
738
739 >>> if 1
740 ... pass
741 ... elif 1:
742 ... pass
743 ... else:
744 ... x() = 1
745 Traceback (most recent call last):
746 SyntaxError: expected ':'
747
748 >>> if 1:
749 ... pass
750 ... elif 1
751 ... pass
752 ... else:
753 ... x() = 1
754 Traceback (most recent call last):
755 SyntaxError: expected ':'
756
757 >>> if 1:
758 ... pass
759 ... elif 1:
760 ... pass
761 ... else
762 ... x() = 1
763 Traceback (most recent call last):
764 SyntaxError: expected ':'
765
766 >>> for x in range(10)
767 ... pass
768 Traceback (most recent call last):
769 SyntaxError: expected ':'
770
771 >>> while True
772 ... pass
773 Traceback (most recent call last):
774 SyntaxError: expected ':'
775
776 >>> with blech as something
777 ... pass
778 Traceback (most recent call last):
779 SyntaxError: expected ':'
780
781 >>> with blech
782 ... pass
783 Traceback (most recent call last):
784 SyntaxError: expected ':'
785
786 >>> with blech, block as something
787 ... pass
788 Traceback (most recent call last):
789 SyntaxError: expected ':'
790
791 >>> with blech, block as something, bluch
792 ... pass
793 Traceback (most recent call last):
794 SyntaxError: expected ':'
795
796 >>> with (blech as something)
797 ... pass
798 Traceback (most recent call last):
799 SyntaxError: expected ':'
800
801 >>> with (blech)
802 ... pass
803 Traceback (most recent call last):
804 SyntaxError: expected ':'
805
806 >>> with (blech, block as something)
807 ... pass
808 Traceback (most recent call last):
809 SyntaxError: expected ':'
810
811 >>> with (blech, block as something, bluch)
812 ... pass
813 Traceback (most recent call last):
814 SyntaxError: expected ':'
815
816 >>> try
817 ... pass
818 Traceback (most recent call last):
819 SyntaxError: expected ':'
820
821 >>> try:
822 ... pass
823 ... except
824 ... pass
825 Traceback (most recent call last):
826 SyntaxError: expected ':'
827
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000828 >>> match x
829 ... case list():
830 ... pass
831 Traceback (most recent call last):
832 SyntaxError: expected ':'
833
834 >>> match x:
835 ... case list()
836 ... pass
837 Traceback (most recent call last):
838 SyntaxError: expected ':'
839
840 >>> match x:
841 ... case [y] if y > 0
842 ... pass
843 Traceback (most recent call last):
844 SyntaxError: expected ':'
845
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100846 >>> if x = 3:
847 ... pass
848 Traceback (most recent call last):
849 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
850
851 >>> while x = 3:
852 ... pass
853 Traceback (most recent call last):
854 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
855
856 >>> if x.a = 3:
857 ... pass
858 Traceback (most recent call last):
859 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
860
861 >>> while x.a = 3:
862 ... pass
863 Traceback (most recent call last):
864 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
865
Pablo Galindod9151cb2021-04-13 02:32:33 +0100866Ensure that early = are not matched by the parser as invalid comparisons
867 >>> f(2, 4, x=34); {1,2 a}
868 Traceback (most recent call last):
869 SyntaxError: invalid syntax
870
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100871 >>> dict(x=34); x $ y
872 Traceback (most recent call last):
873 SyntaxError: invalid syntax
874
875 >>> dict(x=34, (x for x in range 10), 1); x $ y
876 Traceback (most recent call last):
877 SyntaxError: invalid syntax
878
879 >>> dict(x=34, x=1, y=2); x $ y
880 Traceback (most recent call last):
881 SyntaxError: invalid syntax
882
Pablo Galindoda743502021-04-15 14:06:39 +0100883Incomplete dictionary literals
884
885 >>> {1:2, 3:4, 5}
886 Traceback (most recent call last):
887 SyntaxError: ':' expected after dictionary key
888
889 >>> {1:2, 3:4, 5:}
890 Traceback (most recent call last):
891 SyntaxError: expression expected after dictionary key and ':'
892
893 >>> {1: *12+1, 23: 1}
894 Traceback (most recent call last):
895 SyntaxError: cannot use a starred expression in a dictionary value
896
897 >>> {1: *12+1}
898 Traceback (most recent call last):
899 SyntaxError: cannot use a starred expression in a dictionary value
900
901 >>> {1: 23, 1: *12+1}
902 Traceback (most recent call last):
903 SyntaxError: cannot use a starred expression in a dictionary value
904
905 >>> {1:}
906 Traceback (most recent call last):
907 SyntaxError: expression expected after dictionary key and ':'
908
909 # Ensure that the error is not raise for syntax errors that happen after sets
910
911 >>> {1} $
912 Traceback (most recent call last):
913 SyntaxError: invalid syntax
914
Collin Winter828f04a2007-08-31 00:04:24 +0000915Make sure that the old "raise X, Y[, Z]" form is gone:
916 >>> raise X, Y
917 Traceback (most recent call last):
918 ...
919 SyntaxError: invalid syntax
920 >>> raise X, Y, Z
921 Traceback (most recent call last):
922 ...
923 SyntaxError: invalid syntax
924
Pablo Galindo206cbda2021-02-07 18:42:21 +0000925Check that an exception group with missing parentheses
926raise a custom exception
927
928 >>> try:
929 ... pass
930 ... except A, B:
931 ... pass
932 Traceback (most recent call last):
933 SyntaxError: exception group must be parenthesized
934
935 >>> try:
936 ... pass
937 ... except A, B, C:
938 ... pass
939 Traceback (most recent call last):
940 SyntaxError: exception group must be parenthesized
941
942 >>> try:
943 ... pass
944 ... except A, B, C as blech:
945 ... pass
946 Traceback (most recent call last):
947 SyntaxError: exception group must be parenthesized
948
949 >>> try:
950 ... pass
951 ... except A, B, C as blech:
952 ... pass
953 ... finally:
954 ... pass
955 Traceback (most recent call last):
956 SyntaxError: exception group must be parenthesized
957
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000958
959>>> f(a=23, a=234)
960Traceback (most recent call last):
961 ...
Pablo Galindo254ec782020-04-03 20:37:13 +0100962SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +0000963
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000964>>> {1, 2, 3} = 42
965Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100966SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200967
968>>> {1: 2, 3: 4} = 42
969Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100970SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200971
972>>> f'{x}' = 42
973Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100974SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200975
976>>> f'{x}-{y}' = 42
977Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100978SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +0000979
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +0300980>>> from t import x,
981Traceback (most recent call last):
982SyntaxError: trailing comma not allowed without surrounding parentheses
983
984>>> from t import x,y,
985Traceback (most recent call last):
986SyntaxError: trailing comma not allowed without surrounding parentheses
987
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +0300988>>> (): int
989Traceback (most recent call last):
990SyntaxError: only single target (not tuple) can be annotated
991>>> []: int
992Traceback (most recent call last):
993SyntaxError: only single target (not list) can be annotated
994>>> (()): int
995Traceback (most recent call last):
996SyntaxError: only single target (not tuple) can be annotated
997>>> ([]): int
998Traceback (most recent call last):
999SyntaxError: only single target (not list) can be annotated
1000
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001001Corner-cases that used to fail to raise the correct error:
1002
1003 >>> def f(*, x=lambda __debug__:0): pass
1004 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001005 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001006
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +03001007 >>> with (lambda *:0): pass
1008 Traceback (most recent call last):
1009 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001010
1011Corner-cases that used to crash:
1012
1013 >>> def f(**__debug__): pass
1014 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001015 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001016
1017 >>> def f(*xx, __debug__): pass
1018 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001019 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001020
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +03001021 >>> import ä £
1022 Traceback (most recent call last):
1023 SyntaxError: invalid character '£' (U+00A3)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001024"""
1025
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001026import re
1027import unittest
1028
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001029from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001030
1031class SyntaxTestCase(unittest.TestCase):
1032
1033 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001034 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001035 """Check that compiling code raises SyntaxError with errtext.
1036
1037 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001038 test of the exception raised. If subclass is specified it
1039 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001040 """
1041 try:
1042 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +00001043 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001044 if subclass and not isinstance(err, subclass):
1045 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001046 mo = re.search(errtext, str(err))
1047 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -07001048 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001049 self.assertEqual(err.filename, filename)
1050 if lineno is not None:
1051 self.assertEqual(err.lineno, lineno)
1052 if offset is not None:
1053 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001054 else:
1055 self.fail("compile() did not raise SyntaxError")
1056
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001057 def test_expression_with_assignment(self):
1058 self._check_error(
1059 "print(end1 + end2 = ' ')",
Pablo Galindo30ed93b2021-04-13 17:51:21 +01001060 'expression cannot contain assignment, perhaps you meant "=="?',
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001061 offset=19
1062 )
1063
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001064 def test_curly_brace_after_primary_raises_immediately(self):
1065 self._check_error("f{", "invalid syntax", mode="single")
1066
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001067 def test_assign_call(self):
1068 self._check_error("f() = 1", "assign")
1069
1070 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -07001071 self._check_error("del (,)", "invalid syntax")
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001072 self._check_error("del 1", "cannot delete literal")
1073 self._check_error("del (1, 2)", "cannot delete literal")
1074 self._check_error("del None", "cannot delete None")
1075 self._check_error("del *x", "cannot delete starred")
1076 self._check_error("del (*x)", "cannot use starred expression")
1077 self._check_error("del (*x,)", "cannot delete starred")
1078 self._check_error("del [*x,]", "cannot delete starred")
1079 self._check_error("del f()", "cannot delete function call")
1080 self._check_error("del f(a, b)", "cannot delete function call")
1081 self._check_error("del o.f()", "cannot delete function call")
1082 self._check_error("del a[0]()", "cannot delete function call")
1083 self._check_error("del x, f()", "cannot delete function call")
1084 self._check_error("del f(), x", "cannot delete function call")
1085 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1086 self._check_error("del (a if True else b)", "cannot delete conditional")
1087 self._check_error("del +a", "cannot delete expression")
1088 self._check_error("del a, +b", "cannot delete expression")
1089 self._check_error("del a + b", "cannot delete expression")
1090 self._check_error("del (a + b, c)", "cannot delete expression")
1091 self._check_error("del (c[0], a + b)", "cannot delete expression")
1092 self._check_error("del a.b.c + 2", "cannot delete expression")
1093 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1094 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1095 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1096 self._check_error("del (a := 5)", "cannot delete named expression")
Shantanu27c0d9b2020-05-11 14:53:58 -07001097 # We don't have a special message for this, but make sure we don't
1098 # report "cannot delete name"
1099 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001100
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001101 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001102 source = """if 1:
1103 def error(a):
1104 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001105 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001106 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001107 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001108 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001109 self._check_error(source, "parameter and global", lineno=3)
1110
1111 def test_nonlocal_param_err_first(self):
1112 source = """if 1:
1113 def error(a):
1114 nonlocal a # SyntaxError
1115 def error2():
1116 b = 1
1117 global b # SyntaxError
1118 """
1119 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +00001120
Neal Norwitzfcf44352005-11-27 20:37:43 +00001121 def test_break_outside_loop(self):
1122 self._check_error("break", "outside loop")
1123
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001124 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001125 self._check_error("if 0: yield", "outside function")
1126 self._check_error("if 0: yield\nelse: x=1", "outside function")
1127 self._check_error("if 1: pass\nelse: yield", "outside function")
1128 self._check_error("while 0: yield", "outside function")
1129 self._check_error("while 0: yield\nelse: x=1", "outside function")
1130 self._check_error("class C:\n if 0: yield", "outside function")
1131 self._check_error("class C:\n if 1: pass\n else: yield",
1132 "outside function")
1133 self._check_error("class C:\n while 0: yield", "outside function")
1134 self._check_error("class C:\n while 0: yield\n else: x = 1",
1135 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001136
1137 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001138 self._check_error("if 0: return", "outside function")
1139 self._check_error("if 0: return\nelse: x=1", "outside function")
1140 self._check_error("if 1: pass\nelse: return", "outside function")
1141 self._check_error("while 0: return", "outside function")
1142 self._check_error("class C:\n if 0: return", "outside function")
1143 self._check_error("class C:\n while 0: return", "outside function")
1144 self._check_error("class C:\n while 0: return\n else: x=1",
1145 "outside function")
1146 self._check_error("class C:\n if 0: return\n else: x= 1",
1147 "outside function")
1148 self._check_error("class C:\n if 1: pass\n else: return",
1149 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001150
1151 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001152 self._check_error("if 0: break", "outside loop")
1153 self._check_error("if 0: break\nelse: x=1", "outside loop")
1154 self._check_error("if 1: pass\nelse: break", "outside loop")
1155 self._check_error("class C:\n if 0: break", "outside loop")
1156 self._check_error("class C:\n if 1: pass\n else: break",
1157 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001158
1159 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001160 self._check_error("if 0: continue", "not properly in loop")
1161 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1162 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1163 self._check_error("class C:\n if 0: continue", "not properly in loop")
1164 self._check_error("class C:\n if 1: pass\n else: continue",
1165 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001166
Thomas Wouters477c8d52006-05-27 19:21:47 +00001167 def test_unexpected_indent(self):
1168 self._check_error("foo()\n bar()\n", "unexpected indent",
1169 subclass=IndentationError)
1170
1171 def test_no_indent(self):
1172 self._check_error("if 1:\nfoo()", "expected an indented block",
1173 subclass=IndentationError)
1174
1175 def test_bad_outdent(self):
1176 self._check_error("if 1:\n foo()\n bar()",
1177 "unindent does not match .* level",
1178 subclass=IndentationError)
1179
1180 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001181 self._check_error("int(base=10, '2')",
1182 "positional argument follows keyword argument")
1183
1184 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001185 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001186 "positional argument follows "
1187 "keyword argument unpacking")
1188
1189 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001190 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001191 "iterable argument unpacking follows "
1192 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001193
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001194 def test_empty_line_after_linecont(self):
1195 # See issue-40847
1196 s = r"""\
1197pass
1198 \
1199
1200pass
1201"""
1202 try:
1203 compile(s, '<string>', 'exec')
1204 except SyntaxError:
1205 self.fail("Empty line after a line continuation character is valid.")
1206
Mark Shannon02d126a2020-09-25 14:04:19 +01001207 @support.cpython_only
1208 def test_nested_named_except_blocks(self):
1209 code = ""
1210 for i in range(12):
1211 code += f"{' '*i}try:\n"
1212 code += f"{' '*(i+1)}raise Exception\n"
1213 code += f"{' '*i}except Exception as e:\n"
1214 code += f"{' '*4*12}pass"
1215 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001216
Pablo Galindo06f8c332020-10-30 23:48:42 +00001217 def test_barry_as_flufl_with_syntax_errors(self):
1218 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
1219 # is reading the wrong token in the presence of syntax errors later
1220 # in the file. See bpo-42214 for more information.
1221 code = """
1222def func1():
1223 if a != b:
1224 raise ValueError
1225
1226def func2():
1227 try
1228 return 1
1229 finally:
1230 pass
1231"""
Pablo Galindo58fb1562021-02-02 19:54:22 +00001232 self._check_error(code, "expected ':'")
Pablo Galindo06f8c332020-10-30 23:48:42 +00001233
Pablo Galindo96eeff52021-03-22 17:28:11 +00001234 def test_invalid_line_continuation_error_position(self):
1235 self._check_error(r"a = 3 \ 4",
1236 "unexpected character after line continuation character",
1237 lineno=1, offset=9)
1238
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +02001239 def test_invalid_line_continuation_left_recursive(self):
1240 # Check bpo-42218: SyntaxErrors following left-recursive rules
1241 # (t_primary_raw in this case) need to be tested explicitly
1242 self._check_error("A.\u018a\\ ",
1243 "unexpected character after line continuation character")
1244 self._check_error("A.\u03bc\\\n",
1245 "unexpected EOF while parsing")
1246
Pablo Galindod6d63712021-01-19 23:59:33 +00001247 def test_error_parenthesis(self):
1248 for paren in "([{":
1249 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
1250
1251 for paren in ")]}":
1252 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
1253
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001254 def test_match_call_does_not_raise_syntax_error(self):
1255 code = """
1256def match(x):
1257 return 1+1
1258
1259match(34)
1260"""
1261 compile(code, "<string>", "exec")
1262
1263 def test_case_call_does_not_raise_syntax_error(self):
1264 code = """
1265def case(x):
1266 return 1+1
1267
1268case(34)
1269"""
1270 compile(code, "<string>", "exec")
1271
Pablo Galindod6d63712021-01-19 23:59:33 +00001272
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001273def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001274 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001275 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001276 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001277
1278if __name__ == "__main__":
1279 test_main()