blob: e0d0445a83d9d628b21c602c0fb755aa9ff4bbed [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 Galindo3283bf42021-06-03 22:22:28 +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
Pablo Galindob2802482021-04-15 21:38:45 +0100251# parentheses, but about missing commas instead
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000252
253>>> [1, 2 3]
Pablo Galindo835f14f2021-01-31 22:52:56 +0000254Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100255SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000256
257>>> {1, 2 3}
258Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100259SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000260
261>>> {1:2, 2:5 3:12}
262Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100263SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000264
265>>> (1, 2 3)
266Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100267SyntaxError: invalid syntax. Perhaps you forgot a comma?
268
269# Make sure soft keywords constructs don't raise specialized
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700270# errors regarding missing commas or other spezialiced errors
Pablo Galindob2802482021-04-15 21:38:45 +0100271
272>>> match x:
273... y = 3
274Traceback (most recent call last):
275SyntaxError: invalid syntax
276
277>>> match x:
278... case y:
279... 3 $ 3
280Traceback (most recent call last):
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000281SyntaxError: invalid syntax
Pablo Galindo835f14f2021-01-31 22:52:56 +0000282
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700283>>> match x:
284... case $:
285... ...
286Traceback (most recent call last):
287SyntaxError: invalid syntax
288
289>>> match ...:
290... case {**rest, "key": value}:
291... ...
292Traceback (most recent call last):
293SyntaxError: invalid syntax
294
295>>> match ...:
296... case {**_}:
297... ...
298Traceback (most recent call last):
299SyntaxError: invalid syntax
300
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000301From compiler_complex_args():
302
303>>> def f(None=1):
304... pass
305Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000306SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000307
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000308From ast_for_arguments():
309
310>>> def f(x, y=1, z):
311... pass
312Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000313SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000314
315>>> def f(x, None):
316... pass
317Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000318SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000319
320>>> def f(*None):
321... pass
322Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000323SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000324
325>>> def f(**None):
326... pass
327Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000328SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000329
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300330>>> import ast; ast.parse('''
331... def f(
332... *, # type: int
333... a, # type: int
334... ):
335... pass
336... ''', type_comments=True)
337Traceback (most recent call last):
338SyntaxError: bare * has associated type comment
339
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000340
341From ast_for_funcdef():
342
343>>> def None(x):
344... pass
345Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000346SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000347
348
349From ast_for_call():
350
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200351>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000352... return list(it)
353>>> L = range(10)
354>>> f(x for x in L)
355[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
356>>> f(x for x in L, 1)
357Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200358SyntaxError: Generator expression must be parenthesized
359>>> f(x for x in L, y=1)
360Traceback (most recent call last):
361SyntaxError: Generator expression must be parenthesized
362>>> f(x for x in L, *[])
363Traceback (most recent call last):
364SyntaxError: Generator expression must be parenthesized
365>>> f(x for x in L, **{})
366Traceback (most recent call last):
367SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300368>>> f(L, x for x in L)
369Traceback (most recent call last):
370SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400371>>> f(x for x in L, y for y in L)
372Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200373SyntaxError: Generator expression must be parenthesized
374>>> f(x for x in L,)
375Traceback (most recent call last):
376SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000377>>> f((x for x in L), 1)
378[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200379>>> class C(x for x in L):
380... pass
381Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000382SyntaxError: expected ':'
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000383
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200384>>> def g(*args, **kwargs):
385... print(args, sorted(kwargs.items()))
386>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
387... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
388... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
389... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
390... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
391... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
392... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
393... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
394... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
395... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
396... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
397... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
398... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
399... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
400... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
401... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
402... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
403... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
404... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
405... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
406(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000407
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200408>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
409... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
410... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
411... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
412... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
413... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
414... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
415... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
416... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
417... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
418... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
419... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
420... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
421... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
422... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
423... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
424... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
425... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
426... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
427... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
428... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
429... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
430... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
431... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
432... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
433... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
434... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
435... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
436... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
437... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
438... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
439... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
440... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
441... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
442... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
443... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
444... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
445... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
446... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
447... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
448... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
449... # doctest: +ELLIPSIS
450() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000451
Yury Selivanovf2392132016-12-13 19:03:51 -0500452>>> class C:
453... def meth(self, *args):
454... return args
455>>> obj = C()
456>>> obj.meth(
457... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
458... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
459... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
460... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
461... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
462... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
463... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
464... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
465... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
466... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
467... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
468... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
469... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
470... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
471... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
472... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
473... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
474... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
475... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
476... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
477(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
478
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700479>>> f(lambda x: x[0] = 3)
480Traceback (most recent call last):
481SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
482
483# Check that this error doesn't trigger for names:
484>>> f(a={x: for x in {}})
485Traceback (most recent call last):
486SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000487
488The grammar accepts any test (basically, any expression) in the
489keyword slot of a call site. Test a few different options.
490
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700491>>> f(x()=2)
492Traceback (most recent call last):
493SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
494>>> f(a or b=1)
495Traceback (most recent call last):
496SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
497>>> f(x.y=1)
498Traceback (most recent call last):
499SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
500>>> f((x)=2)
501Traceback (most recent call last):
502SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
503>>> f(True=2)
504Traceback (most recent call last):
505SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200506>>> f(__debug__=1)
507Traceback (most recent call last):
508SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100509>>> __debug__: int
510Traceback (most recent call last):
511SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000512
513
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000514More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000515
516>>> (x for x in x) += 1
517Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100518SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000519>>> None += 1
520Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100521SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200522>>> __debug__ += 1
523Traceback (most recent call last):
524SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000525>>> f() += 1
526Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100527SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000528
529
530Test continue in finally in weird combinations.
531
Ezio Melotti13925002011-03-16 11:05:33 +0200532continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000533
534 >>> def test():
535 ... try:
536 ... pass
537 ... finally:
538 ... for abc in range(10):
539 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000540 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000541 >>> test()
542 9
543
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200544continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000545
546 >>> def test():
547 ... for abc in range(10):
548 ... try:
549 ... pass
550 ... finally:
551 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200552 ... print(abc)
553 >>> test()
554 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555
556 >>> def test():
557 ... for abc in range(10):
558 ... try:
559 ... pass
560 ... finally:
561 ... try:
562 ... continue
563 ... except:
564 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200565 ... print(abc)
566 >>> test()
567 9
568
569 >>> def test():
570 ... for abc in range(10):
571 ... try:
572 ... pass
573 ... finally:
574 ... try:
575 ... pass
576 ... except:
577 ... continue
578 ... print(abc)
579 >>> test()
580 9
581
582A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000583
584 >>> def foo():
585 ... try:
586 ... pass
587 ... finally:
588 ... continue
589 Traceback (most recent call last):
590 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200591 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000592
593There is one test for a break that is not in a loop. The compiler
594uses a single data structure to keep track of try-finally and loops,
595so we need to be sure that a break is actually inside a loop. If it
596isn't, there should be a syntax error.
597
598 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000599 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000600 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000601 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000602 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000603 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000604 Traceback (most recent call last):
605 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000606 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000607
Benjamin Petersone09ed542016-07-14 22:00:03 -0700608This raises a SyntaxError, it used to raise a SystemError.
609Context for this change can be found on issue #27514
610
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611In 2.5 there was a missing exception and an assert was triggered in a debug
612build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
613
614 >>> while 1:
615 ... while 2:
616 ... while 3:
617 ... while 4:
618 ... while 5:
619 ... while 6:
620 ... while 8:
621 ... while 9:
622 ... while 10:
623 ... while 11:
624 ... while 12:
625 ... while 13:
626 ... while 14:
627 ... while 15:
628 ... while 16:
629 ... while 17:
630 ... while 18:
631 ... while 19:
632 ... while 20:
633 ... while 21:
634 ... while 22:
635 ... break
636 Traceback (most recent call last):
637 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700638 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000639
Guido van Rossum6cff8742016-09-09 09:36:26 -0700640Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
641
642 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200643 ... print(x)
644 ... global x
645 Traceback (most recent call last):
646 ...
647 SyntaxError: name 'x' is used prior to global declaration
648
649 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700650 ... x = 1
651 ... global x
652 Traceback (most recent call last):
653 ...
654 SyntaxError: name 'x' is assigned to before global declaration
655
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200656 >>> def f(x):
657 ... global x
658 Traceback (most recent call last):
659 ...
660 SyntaxError: name 'x' is parameter and global
661
Guido van Rossum6cff8742016-09-09 09:36:26 -0700662 >>> def f():
663 ... x = 1
664 ... def g():
665 ... print(x)
666 ... nonlocal x
667 Traceback (most recent call last):
668 ...
669 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000670
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300671 >>> def f():
672 ... x = 1
673 ... def g():
674 ... x = 2
675 ... nonlocal x
676 Traceback (most recent call last):
677 ...
678 SyntaxError: name 'x' is assigned to before nonlocal declaration
679
Jeremy Hylton81e95022007-02-27 06:50:52 +0000680 >>> def f(x):
681 ... nonlocal x
682 Traceback (most recent call last):
683 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000684 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000685
Jeremy Hylton81e95022007-02-27 06:50:52 +0000686 >>> def f():
687 ... global x
688 ... nonlocal x
689 Traceback (most recent call last):
690 ...
691 SyntaxError: name 'x' is nonlocal and global
692
693 >>> def f():
694 ... nonlocal x
695 Traceback (most recent call last):
696 ...
697 SyntaxError: no binding for nonlocal 'x' found
698
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000699From SF bug #1705365
700 >>> nonlocal x
701 Traceback (most recent call last):
702 ...
703 SyntaxError: nonlocal declaration not allowed at module level
704
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300705From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600706 >>> class A:
707 ... def f(self):
708 ... nonlocal __x
709 Traceback (most recent call last):
710 ...
711 SyntaxError: no binding for nonlocal '_A__x' found
712
Guido van Rossumd8faa362007-04-27 19:54:29 +0000713
714This tests assignment-context; there was a bug in Python 2.5 where compiling
715a complex 'if' (one with 'elif') would fail to notice an invalid suite,
716leading to spurious errors.
717
718 >>> if 1:
719 ... x() = 1
720 ... elif 1:
721 ... pass
722 Traceback (most recent call last):
723 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100724 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000725
726 >>> if 1:
727 ... pass
728 ... elif 1:
729 ... x() = 1
730 Traceback (most recent call last):
731 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100732 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000733
734 >>> if 1:
735 ... x() = 1
736 ... elif 1:
737 ... pass
738 ... else:
739 ... pass
740 Traceback (most recent call last):
741 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100742 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000743
744 >>> if 1:
745 ... pass
746 ... elif 1:
747 ... x() = 1
748 ... else:
749 ... pass
750 Traceback (most recent call last):
751 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100752 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000753
754 >>> if 1:
755 ... pass
756 ... elif 1:
757 ... pass
758 ... else:
759 ... x() = 1
760 Traceback (most recent call last):
761 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100762 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hylton81e95022007-02-27 06:50:52 +0000763
Pablo Galindo58fb1562021-02-02 19:54:22 +0000764 Missing ':' before suites:
765
766 >>> def f()
767 ... pass
768 Traceback (most recent call last):
769 SyntaxError: expected ':'
770
771 >>> class A
772 ... pass
773 Traceback (most recent call last):
774 SyntaxError: expected ':'
775
776 >>> if 1
777 ... pass
778 ... elif 1:
779 ... pass
780 ... else:
781 ... x() = 1
782 Traceback (most recent call last):
783 SyntaxError: expected ':'
784
785 >>> if 1:
786 ... pass
787 ... elif 1
788 ... pass
789 ... else:
790 ... x() = 1
791 Traceback (most recent call last):
792 SyntaxError: expected ':'
793
794 >>> if 1:
795 ... pass
796 ... elif 1:
797 ... pass
798 ... else
799 ... x() = 1
800 Traceback (most recent call last):
801 SyntaxError: expected ':'
802
803 >>> for x in range(10)
804 ... pass
805 Traceback (most recent call last):
806 SyntaxError: expected ':'
807
808 >>> while True
809 ... pass
810 Traceback (most recent call last):
811 SyntaxError: expected ':'
812
813 >>> with blech as something
814 ... pass
815 Traceback (most recent call last):
816 SyntaxError: expected ':'
817
818 >>> with blech
819 ... pass
820 Traceback (most recent call last):
821 SyntaxError: expected ':'
822
823 >>> with blech, block as something
824 ... pass
825 Traceback (most recent call last):
826 SyntaxError: expected ':'
827
828 >>> with blech, block as something, bluch
829 ... pass
830 Traceback (most recent call last):
831 SyntaxError: expected ':'
832
833 >>> with (blech as something)
834 ... pass
835 Traceback (most recent call last):
836 SyntaxError: expected ':'
837
838 >>> with (blech)
839 ... pass
840 Traceback (most recent call last):
841 SyntaxError: expected ':'
842
843 >>> with (blech, block as something)
844 ... pass
845 Traceback (most recent call last):
846 SyntaxError: expected ':'
847
848 >>> with (blech, block as something, bluch)
849 ... pass
850 Traceback (most recent call last):
851 SyntaxError: expected ':'
852
853 >>> try
854 ... pass
855 Traceback (most recent call last):
856 SyntaxError: expected ':'
857
858 >>> try:
859 ... pass
860 ... except
861 ... pass
862 Traceback (most recent call last):
863 SyntaxError: expected ':'
864
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000865 >>> match x
866 ... case list():
867 ... pass
868 Traceback (most recent call last):
869 SyntaxError: expected ':'
870
871 >>> match x:
872 ... case list()
873 ... pass
874 Traceback (most recent call last):
875 SyntaxError: expected ':'
876
877 >>> match x:
878 ... case [y] if y > 0
879 ... pass
880 Traceback (most recent call last):
881 SyntaxError: expected ':'
882
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100883 >>> if x = 3:
884 ... pass
885 Traceback (most recent call last):
886 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
887
888 >>> while x = 3:
889 ... pass
890 Traceback (most recent call last):
891 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
892
893 >>> if x.a = 3:
894 ... pass
895 Traceback (most recent call last):
896 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
897
898 >>> while x.a = 3:
899 ... pass
900 Traceback (most recent call last):
901 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
902
Pablo Galindoe53f72a2021-06-04 00:11:43 +0100903Custom error messages for try blocks that are not followed by except/finally
904
905 >>> try:
906 ... x = 34
907 ...
908 Traceback (most recent call last):
909 SyntaxError: expected 'except' or 'finally' block
910
Pablo Galindod9151cb2021-04-13 02:32:33 +0100911Ensure that early = are not matched by the parser as invalid comparisons
Pablo Galindob2802482021-04-15 21:38:45 +0100912 >>> f(2, 4, x=34); 1 $ 2
Pablo Galindod9151cb2021-04-13 02:32:33 +0100913 Traceback (most recent call last):
914 SyntaxError: invalid syntax
915
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100916 >>> dict(x=34); x $ y
917 Traceback (most recent call last):
918 SyntaxError: invalid syntax
919
920 >>> dict(x=34, (x for x in range 10), 1); x $ y
921 Traceback (most recent call last):
922 SyntaxError: invalid syntax
923
924 >>> dict(x=34, x=1, y=2); x $ y
925 Traceback (most recent call last):
926 SyntaxError: invalid syntax
927
Pablo Galindoda743502021-04-15 14:06:39 +0100928Incomplete dictionary literals
929
930 >>> {1:2, 3:4, 5}
931 Traceback (most recent call last):
932 SyntaxError: ':' expected after dictionary key
933
934 >>> {1:2, 3:4, 5:}
935 Traceback (most recent call last):
936 SyntaxError: expression expected after dictionary key and ':'
937
938 >>> {1: *12+1, 23: 1}
939 Traceback (most recent call last):
940 SyntaxError: cannot use a starred expression in a dictionary value
941
942 >>> {1: *12+1}
943 Traceback (most recent call last):
944 SyntaxError: cannot use a starred expression in a dictionary value
945
946 >>> {1: 23, 1: *12+1}
947 Traceback (most recent call last):
948 SyntaxError: cannot use a starred expression in a dictionary value
949
950 >>> {1:}
951 Traceback (most recent call last):
952 SyntaxError: expression expected after dictionary key and ':'
953
954 # Ensure that the error is not raise for syntax errors that happen after sets
955
956 >>> {1} $
957 Traceback (most recent call last):
958 SyntaxError: invalid syntax
959
Pablo Galindo56c95df2021-04-21 15:28:21 +0100960Specialized indentation errors:
961
962 >>> while condition:
963 ... pass
964 Traceback (most recent call last):
965 IndentationError: expected an indented block after 'while' statement on line 1
966
967 >>> for x in range(10):
968 ... pass
969 Traceback (most recent call last):
970 IndentationError: expected an indented block after 'for' statement on line 1
971
972 >>> for x in range(10):
973 ... pass
974 ... else:
975 ... pass
976 Traceback (most recent call last):
977 IndentationError: expected an indented block after 'else' statement on line 3
978
979 >>> async for x in range(10):
980 ... pass
981 Traceback (most recent call last):
982 IndentationError: expected an indented block after 'for' statement on line 1
983
984 >>> async for x in range(10):
985 ... pass
986 ... else:
987 ... pass
988 Traceback (most recent call last):
989 IndentationError: expected an indented block after 'else' statement on line 3
990
991 >>> if something:
992 ... pass
993 Traceback (most recent call last):
994 IndentationError: expected an indented block after 'if' statement on line 1
995
996 >>> if something:
997 ... pass
998 ... elif something_else:
999 ... pass
1000 Traceback (most recent call last):
1001 IndentationError: expected an indented block after 'elif' statement on line 3
1002
1003 >>> if something:
1004 ... pass
1005 ... elif something_else:
1006 ... pass
1007 ... else:
1008 ... pass
1009 Traceback (most recent call last):
1010 IndentationError: expected an indented block after 'else' statement on line 5
1011
1012 >>> try:
1013 ... pass
1014 Traceback (most recent call last):
1015 IndentationError: expected an indented block after 'try' statement on line 1
1016
1017 >>> try:
1018 ... something()
1019 ... except A:
1020 ... pass
1021 Traceback (most recent call last):
1022 IndentationError: expected an indented block after 'except' statement on line 3
1023
1024 >>> try:
1025 ... something()
1026 ... except A:
1027 ... pass
1028 ... finally:
1029 ... pass
1030 Traceback (most recent call last):
1031 IndentationError: expected an indented block after 'finally' statement on line 5
1032
1033 >>> with A:
1034 ... pass
1035 Traceback (most recent call last):
1036 IndentationError: expected an indented block after 'with' statement on line 1
1037
1038 >>> with A as a, B as b:
1039 ... pass
1040 Traceback (most recent call last):
1041 IndentationError: expected an indented block after 'with' statement on line 1
1042
1043 >>> with (A as a, B as b):
1044 ... pass
1045 Traceback (most recent call last):
1046 IndentationError: expected an indented block after 'with' statement on line 1
1047
1048 >>> async with A:
1049 ... pass
1050 Traceback (most recent call last):
1051 IndentationError: expected an indented block after 'with' statement on line 1
1052
1053 >>> async with A as a, B as b:
1054 ... pass
1055 Traceback (most recent call last):
1056 IndentationError: expected an indented block after 'with' statement on line 1
1057
1058 >>> async with (A as a, B as b):
1059 ... pass
1060 Traceback (most recent call last):
1061 IndentationError: expected an indented block after 'with' statement on line 1
1062
1063 >>> def foo(x, /, y, *, z=2):
1064 ... pass
1065 Traceback (most recent call last):
1066 IndentationError: expected an indented block after function definition on line 1
1067
1068 >>> class Blech(A):
1069 ... pass
1070 Traceback (most recent call last):
1071 IndentationError: expected an indented block after class definition on line 1
1072
1073 >>> match something:
1074 ... pass
1075 Traceback (most recent call last):
1076 IndentationError: expected an indented block after 'match' statement on line 1
1077
1078 >>> match something:
1079 ... case []:
1080 ... pass
1081 Traceback (most recent call last):
1082 IndentationError: expected an indented block after 'case' statement on line 2
1083
1084 >>> match something:
1085 ... case []:
1086 ... ...
1087 ... case {}:
1088 ... pass
1089 Traceback (most recent call last):
1090 IndentationError: expected an indented block after 'case' statement on line 4
1091
Collin Winter828f04a2007-08-31 00:04:24 +00001092Make sure that the old "raise X, Y[, Z]" form is gone:
1093 >>> raise X, Y
1094 Traceback (most recent call last):
1095 ...
1096 SyntaxError: invalid syntax
1097 >>> raise X, Y, Z
1098 Traceback (most recent call last):
1099 ...
1100 SyntaxError: invalid syntax
1101
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001102Check that an multiple exception types with missing parentheses
Pablo Galindo206cbda2021-02-07 18:42:21 +00001103raise a custom exception
1104
1105 >>> try:
1106 ... pass
1107 ... except A, B:
1108 ... pass
1109 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001110 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001111
1112 >>> try:
1113 ... pass
1114 ... except A, B, C:
1115 ... pass
1116 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001117 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001118
1119 >>> try:
1120 ... pass
1121 ... except A, B, C as blech:
1122 ... pass
1123 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001124 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001125
1126 >>> try:
1127 ... pass
1128 ... except A, B, C as blech:
1129 ... pass
1130 ... finally:
1131 ... pass
1132 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001133 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001134
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001135
1136>>> f(a=23, a=234)
1137Traceback (most recent call last):
1138 ...
Pablo Galindo254ec782020-04-03 20:37:13 +01001139SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001140
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001141>>> {1, 2, 3} = 42
1142Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001143SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001144
1145>>> {1: 2, 3: 4} = 42
1146Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001147SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001148
1149>>> f'{x}' = 42
1150Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001151SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001152
1153>>> f'{x}-{y}' = 42
1154Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001155SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001156
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -07001157>>> (x, y, z=3, d, e)
1158Traceback (most recent call last):
1159SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1160
1161>>> [x, y, z=3, d, e]
1162Traceback (most recent call last):
1163SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1164
1165>>> [z=3]
1166Traceback (most recent call last):
1167SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1168
1169>>> {x, y, z=3, d, e}
1170Traceback (most recent call last):
1171SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1172
1173>>> {z=3}
1174Traceback (most recent call last):
1175SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1176
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +03001177>>> from t import x,
1178Traceback (most recent call last):
1179SyntaxError: trailing comma not allowed without surrounding parentheses
1180
1181>>> from t import x,y,
1182Traceback (most recent call last):
1183SyntaxError: trailing comma not allowed without surrounding parentheses
1184
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +03001185>>> (): int
1186Traceback (most recent call last):
1187SyntaxError: only single target (not tuple) can be annotated
1188>>> []: int
1189Traceback (most recent call last):
1190SyntaxError: only single target (not list) can be annotated
1191>>> (()): int
1192Traceback (most recent call last):
1193SyntaxError: only single target (not tuple) can be annotated
1194>>> ([]): int
1195Traceback (most recent call last):
1196SyntaxError: only single target (not list) can be annotated
1197
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001198Corner-cases that used to fail to raise the correct error:
1199
1200 >>> def f(*, x=lambda __debug__:0): pass
1201 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001202 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001203
Pablo Galindob0544ba2021-04-21 12:41:19 +01001204 >>> def f(*args:(lambda __debug__:0)): pass
1205 Traceback (most recent call last):
1206 SyntaxError: cannot assign to __debug__
1207
1208 >>> def f(**kwargs:(lambda __debug__:0)): pass
1209 Traceback (most recent call last):
1210 SyntaxError: cannot assign to __debug__
1211
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +03001212 >>> with (lambda *:0): pass
1213 Traceback (most recent call last):
1214 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001215
1216Corner-cases that used to crash:
1217
1218 >>> def f(**__debug__): pass
1219 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001220 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001221
1222 >>> def f(*xx, __debug__): pass
1223 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001224 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001225
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +03001226 >>> import ä £
1227 Traceback (most recent call last):
1228 SyntaxError: invalid character '£' (U+00A3)
Pablo Galindoa8c418d2021-06-18 22:15:57 +01001229
1230 Invalid pattern matching constructs:
1231
1232 >>> match ...:
1233 ... case 42 as _:
1234 ... ...
1235 Traceback (most recent call last):
1236 SyntaxError: cannot use '_' as a target
1237
1238 >>> match ...:
1239 ... case 42 as 1+2+4:
1240 ... ...
1241 Traceback (most recent call last):
1242 SyntaxError: invalid pattern target
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001243"""
1244
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001245import re
1246import unittest
1247
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001248from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001249
1250class SyntaxTestCase(unittest.TestCase):
1251
1252 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001253 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001254 """Check that compiling code raises SyntaxError with errtext.
1255
1256 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001257 test of the exception raised. If subclass is specified it
1258 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001259 """
1260 try:
1261 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +00001262 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001263 if subclass and not isinstance(err, subclass):
1264 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001265 mo = re.search(errtext, str(err))
1266 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -07001267 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001268 self.assertEqual(err.filename, filename)
1269 if lineno is not None:
1270 self.assertEqual(err.lineno, lineno)
1271 if offset is not None:
1272 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001273 else:
1274 self.fail("compile() did not raise SyntaxError")
1275
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001276 def test_expression_with_assignment(self):
1277 self._check_error(
1278 "print(end1 + end2 = ' ')",
Pablo Galindo30ed93b2021-04-13 17:51:21 +01001279 'expression cannot contain assignment, perhaps you meant "=="?',
Pablo Galindoa77aac42021-04-23 14:27:05 +01001280 offset=7
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001281 )
1282
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001283 def test_curly_brace_after_primary_raises_immediately(self):
1284 self._check_error("f{", "invalid syntax", mode="single")
1285
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001286 def test_assign_call(self):
1287 self._check_error("f() = 1", "assign")
1288
1289 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -07001290 self._check_error("del (,)", "invalid syntax")
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001291 self._check_error("del 1", "cannot delete literal")
1292 self._check_error("del (1, 2)", "cannot delete literal")
1293 self._check_error("del None", "cannot delete None")
1294 self._check_error("del *x", "cannot delete starred")
1295 self._check_error("del (*x)", "cannot use starred expression")
1296 self._check_error("del (*x,)", "cannot delete starred")
1297 self._check_error("del [*x,]", "cannot delete starred")
1298 self._check_error("del f()", "cannot delete function call")
1299 self._check_error("del f(a, b)", "cannot delete function call")
1300 self._check_error("del o.f()", "cannot delete function call")
1301 self._check_error("del a[0]()", "cannot delete function call")
1302 self._check_error("del x, f()", "cannot delete function call")
1303 self._check_error("del f(), x", "cannot delete function call")
1304 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1305 self._check_error("del (a if True else b)", "cannot delete conditional")
1306 self._check_error("del +a", "cannot delete expression")
1307 self._check_error("del a, +b", "cannot delete expression")
1308 self._check_error("del a + b", "cannot delete expression")
1309 self._check_error("del (a + b, c)", "cannot delete expression")
1310 self._check_error("del (c[0], a + b)", "cannot delete expression")
1311 self._check_error("del a.b.c + 2", "cannot delete expression")
1312 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1313 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1314 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1315 self._check_error("del (a := 5)", "cannot delete named expression")
Shantanu27c0d9b2020-05-11 14:53:58 -07001316 # We don't have a special message for this, but make sure we don't
1317 # report "cannot delete name"
1318 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001319
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001320 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001321 source = """if 1:
1322 def error(a):
1323 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001324 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001325 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001326 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001327 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001328 self._check_error(source, "parameter and global", lineno=3)
1329
1330 def test_nonlocal_param_err_first(self):
1331 source = """if 1:
1332 def error(a):
1333 nonlocal a # SyntaxError
1334 def error2():
1335 b = 1
1336 global b # SyntaxError
1337 """
1338 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +00001339
Neal Norwitzfcf44352005-11-27 20:37:43 +00001340 def test_break_outside_loop(self):
1341 self._check_error("break", "outside loop")
1342
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001343 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001344 self._check_error("if 0: yield", "outside function")
1345 self._check_error("if 0: yield\nelse: x=1", "outside function")
1346 self._check_error("if 1: pass\nelse: yield", "outside function")
1347 self._check_error("while 0: yield", "outside function")
1348 self._check_error("while 0: yield\nelse: x=1", "outside function")
1349 self._check_error("class C:\n if 0: yield", "outside function")
1350 self._check_error("class C:\n if 1: pass\n else: yield",
1351 "outside function")
1352 self._check_error("class C:\n while 0: yield", "outside function")
1353 self._check_error("class C:\n while 0: yield\n else: x = 1",
1354 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001355
1356 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001357 self._check_error("if 0: return", "outside function")
1358 self._check_error("if 0: return\nelse: x=1", "outside function")
1359 self._check_error("if 1: pass\nelse: return", "outside function")
1360 self._check_error("while 0: return", "outside function")
1361 self._check_error("class C:\n if 0: return", "outside function")
1362 self._check_error("class C:\n while 0: return", "outside function")
1363 self._check_error("class C:\n while 0: return\n else: x=1",
1364 "outside function")
1365 self._check_error("class C:\n if 0: return\n else: x= 1",
1366 "outside function")
1367 self._check_error("class C:\n if 1: pass\n else: return",
1368 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001369
1370 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001371 self._check_error("if 0: break", "outside loop")
1372 self._check_error("if 0: break\nelse: x=1", "outside loop")
1373 self._check_error("if 1: pass\nelse: break", "outside loop")
1374 self._check_error("class C:\n if 0: break", "outside loop")
1375 self._check_error("class C:\n if 1: pass\n else: break",
1376 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001377
1378 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001379 self._check_error("if 0: continue", "not properly in loop")
1380 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1381 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1382 self._check_error("class C:\n if 0: continue", "not properly in loop")
1383 self._check_error("class C:\n if 1: pass\n else: continue",
1384 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001385
Thomas Wouters477c8d52006-05-27 19:21:47 +00001386 def test_unexpected_indent(self):
1387 self._check_error("foo()\n bar()\n", "unexpected indent",
1388 subclass=IndentationError)
1389
1390 def test_no_indent(self):
1391 self._check_error("if 1:\nfoo()", "expected an indented block",
1392 subclass=IndentationError)
1393
1394 def test_bad_outdent(self):
1395 self._check_error("if 1:\n foo()\n bar()",
1396 "unindent does not match .* level",
1397 subclass=IndentationError)
1398
1399 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001400 self._check_error("int(base=10, '2')",
1401 "positional argument follows keyword argument")
1402
1403 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001404 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001405 "positional argument follows "
1406 "keyword argument unpacking")
1407
1408 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001409 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001410 "iterable argument unpacking follows "
1411 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001412
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001413 def test_empty_line_after_linecont(self):
1414 # See issue-40847
1415 s = r"""\
1416pass
1417 \
1418
1419pass
1420"""
1421 try:
1422 compile(s, '<string>', 'exec')
1423 except SyntaxError:
1424 self.fail("Empty line after a line continuation character is valid.")
1425
Mark Shannon02d126a2020-09-25 14:04:19 +01001426 @support.cpython_only
1427 def test_nested_named_except_blocks(self):
1428 code = ""
1429 for i in range(12):
1430 code += f"{' '*i}try:\n"
1431 code += f"{' '*(i+1)}raise Exception\n"
1432 code += f"{' '*i}except Exception as e:\n"
1433 code += f"{' '*4*12}pass"
1434 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001435
Pablo Galindo06f8c332020-10-30 23:48:42 +00001436 def test_barry_as_flufl_with_syntax_errors(self):
1437 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
1438 # is reading the wrong token in the presence of syntax errors later
1439 # in the file. See bpo-42214 for more information.
1440 code = """
1441def func1():
1442 if a != b:
1443 raise ValueError
1444
1445def func2():
1446 try
1447 return 1
1448 finally:
1449 pass
1450"""
Pablo Galindo58fb1562021-02-02 19:54:22 +00001451 self._check_error(code, "expected ':'")
Pablo Galindo06f8c332020-10-30 23:48:42 +00001452
Pablo Galindo96eeff52021-03-22 17:28:11 +00001453 def test_invalid_line_continuation_error_position(self):
1454 self._check_error(r"a = 3 \ 4",
1455 "unexpected character after line continuation character",
1456 lineno=1, offset=9)
1457
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +02001458 def test_invalid_line_continuation_left_recursive(self):
1459 # Check bpo-42218: SyntaxErrors following left-recursive rules
1460 # (t_primary_raw in this case) need to be tested explicitly
1461 self._check_error("A.\u018a\\ ",
1462 "unexpected character after line continuation character")
1463 self._check_error("A.\u03bc\\\n",
1464 "unexpected EOF while parsing")
1465
Pablo Galindod6d63712021-01-19 23:59:33 +00001466 def test_error_parenthesis(self):
1467 for paren in "([{":
1468 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
1469
1470 for paren in ")]}":
1471 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
1472
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001473 def test_match_call_does_not_raise_syntax_error(self):
1474 code = """
1475def match(x):
1476 return 1+1
1477
1478match(34)
1479"""
1480 compile(code, "<string>", "exec")
1481
1482 def test_case_call_does_not_raise_syntax_error(self):
1483 code = """
1484def case(x):
1485 return 1+1
1486
1487case(34)
1488"""
1489 compile(code, "<string>", "exec")
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -07001490
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07001491 def test_multiline_compiler_error_points_to_the_end(self):
1492 self._check_error(
1493 "call(\na=1,\na=1\n)",
1494 "keyword argument repeated",
1495 lineno=3
1496 )
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001497
Pablo Galindod6d63712021-01-19 23:59:33 +00001498
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001499def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001500 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001501 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001502 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001503
1504if __name__ == "__main__":
1505 test_main()