blob: 3b1128e9af38956e8a4b1aad3f38216ebc44f9b5 [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
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
270# errors regarding missing commas
271
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
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000283From compiler_complex_args():
284
285>>> def f(None=1):
286... pass
287Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000288SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000289
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000290From ast_for_arguments():
291
292>>> def f(x, y=1, z):
293... pass
294Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000295SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000296
297>>> def f(x, None):
298... pass
299Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000300SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000301
302>>> def f(*None):
303... pass
304Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000305SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000306
307>>> def f(**None):
308... pass
309Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000310SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000311
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300312>>> import ast; ast.parse('''
313... def f(
314... *, # type: int
315... a, # type: int
316... ):
317... pass
318... ''', type_comments=True)
319Traceback (most recent call last):
320SyntaxError: bare * has associated type comment
321
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000322
323From ast_for_funcdef():
324
325>>> def None(x):
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
330
331From ast_for_call():
332
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200333>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334... return list(it)
335>>> L = range(10)
336>>> f(x for x in L)
337[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
338>>> f(x for x in L, 1)
339Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200340SyntaxError: Generator expression must be parenthesized
341>>> f(x for x in L, y=1)
342Traceback (most recent call last):
343SyntaxError: Generator expression must be parenthesized
344>>> f(x for x in L, *[])
345Traceback (most recent call last):
346SyntaxError: Generator expression must be parenthesized
347>>> f(x for x in L, **{})
348Traceback (most recent call last):
349SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300350>>> f(L, x for x in L)
351Traceback (most recent call last):
352SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400353>>> f(x for x in L, y for y in L)
354Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200355SyntaxError: Generator expression must be parenthesized
356>>> f(x for x in L,)
357Traceback (most recent call last):
358SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000359>>> f((x for x in L), 1)
360[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200361>>> class C(x for x in L):
362... pass
363Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000364SyntaxError: expected ':'
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000365
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200366>>> def g(*args, **kwargs):
367... print(args, sorted(kwargs.items()))
368>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
369... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
370... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
371... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
372... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
373... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
374... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
375... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
376... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
377... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
378... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
379... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
380... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
381... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
382... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
383... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
384... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
385... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
386... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
387... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
388(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000389
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200390>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
391... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
392... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
393... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
394... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
395... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
396... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
397... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
398... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
399... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
400... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
401... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
402... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
403... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
404... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
405... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
406... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
407... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
408... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
409... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
410... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
411... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
412... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
413... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
414... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
415... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
416... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
417... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
418... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
419... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
420... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
421... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
422... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
423... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
424... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
425... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
426... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
427... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
428... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
429... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
430... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
431... # doctest: +ELLIPSIS
432() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000433
Yury Selivanovf2392132016-12-13 19:03:51 -0500434>>> class C:
435... def meth(self, *args):
436... return args
437>>> obj = C()
438>>> obj.meth(
439... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
440... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
441... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
442... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
443... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
444... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
445... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
446... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
447... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
448... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
449... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
450... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
451... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
452... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
453... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
454... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
455... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
456... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
457... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
458... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
459(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
460
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700461>>> f(lambda x: x[0] = 3)
462Traceback (most recent call last):
463SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
464
465# Check that this error doesn't trigger for names:
466>>> f(a={x: for x in {}})
467Traceback (most recent call last):
468SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000469
470The grammar accepts any test (basically, any expression) in the
471keyword slot of a call site. Test a few different options.
472
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700473>>> f(x()=2)
474Traceback (most recent call last):
475SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
476>>> f(a or b=1)
477Traceback (most recent call last):
478SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
479>>> f(x.y=1)
480Traceback (most recent call last):
481SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
482>>> f((x)=2)
483Traceback (most recent call last):
484SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
485>>> f(True=2)
486Traceback (most recent call last):
487SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200488>>> f(__debug__=1)
489Traceback (most recent call last):
490SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100491>>> __debug__: int
492Traceback (most recent call last):
493SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000494
495
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000496More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000497
498>>> (x for x in x) += 1
499Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100500SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000501>>> None += 1
502Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100503SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200504>>> __debug__ += 1
505Traceback (most recent call last):
506SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000507>>> f() += 1
508Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100509SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000510
511
512Test continue in finally in weird combinations.
513
Ezio Melotti13925002011-03-16 11:05:33 +0200514continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000515
516 >>> def test():
517 ... try:
518 ... pass
519 ... finally:
520 ... for abc in range(10):
521 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000522 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000523 >>> test()
524 9
525
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200526continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000527
528 >>> def test():
529 ... for abc in range(10):
530 ... try:
531 ... pass
532 ... finally:
533 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200534 ... print(abc)
535 >>> test()
536 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000537
538 >>> def test():
539 ... for abc in range(10):
540 ... try:
541 ... pass
542 ... finally:
543 ... try:
544 ... continue
545 ... except:
546 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200547 ... print(abc)
548 >>> test()
549 9
550
551 >>> def test():
552 ... for abc in range(10):
553 ... try:
554 ... pass
555 ... finally:
556 ... try:
557 ... pass
558 ... except:
559 ... continue
560 ... print(abc)
561 >>> test()
562 9
563
564A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565
566 >>> def foo():
567 ... try:
568 ... pass
569 ... finally:
570 ... continue
571 Traceback (most recent call last):
572 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200573 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000574
575There is one test for a break that is not in a loop. The compiler
576uses a single data structure to keep track of try-finally and loops,
577so we need to be sure that a break is actually inside a loop. If it
578isn't, there should be a syntax error.
579
580 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000581 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000582 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000583 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000584 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000585 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000586 Traceback (most recent call last):
587 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000588 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000589
Benjamin Petersone09ed542016-07-14 22:00:03 -0700590This raises a SyntaxError, it used to raise a SystemError.
591Context for this change can be found on issue #27514
592
Thomas Wouters89f507f2006-12-13 04:49:30 +0000593In 2.5 there was a missing exception and an assert was triggered in a debug
594build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
595
596 >>> while 1:
597 ... while 2:
598 ... while 3:
599 ... while 4:
600 ... while 5:
601 ... while 6:
602 ... while 8:
603 ... while 9:
604 ... while 10:
605 ... while 11:
606 ... while 12:
607 ... while 13:
608 ... while 14:
609 ... while 15:
610 ... while 16:
611 ... while 17:
612 ... while 18:
613 ... while 19:
614 ... while 20:
615 ... while 21:
616 ... while 22:
617 ... break
618 Traceback (most recent call last):
619 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700620 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000621
Guido van Rossum6cff8742016-09-09 09:36:26 -0700622Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
623
624 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200625 ... print(x)
626 ... global x
627 Traceback (most recent call last):
628 ...
629 SyntaxError: name 'x' is used prior to global declaration
630
631 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700632 ... x = 1
633 ... global x
634 Traceback (most recent call last):
635 ...
636 SyntaxError: name 'x' is assigned to before global declaration
637
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200638 >>> def f(x):
639 ... global x
640 Traceback (most recent call last):
641 ...
642 SyntaxError: name 'x' is parameter and global
643
Guido van Rossum6cff8742016-09-09 09:36:26 -0700644 >>> def f():
645 ... x = 1
646 ... def g():
647 ... print(x)
648 ... nonlocal x
649 Traceback (most recent call last):
650 ...
651 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000652
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300653 >>> def f():
654 ... x = 1
655 ... def g():
656 ... x = 2
657 ... nonlocal x
658 Traceback (most recent call last):
659 ...
660 SyntaxError: name 'x' is assigned to before nonlocal declaration
661
Jeremy Hylton81e95022007-02-27 06:50:52 +0000662 >>> def f(x):
663 ... nonlocal x
664 Traceback (most recent call last):
665 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000666 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000667
Jeremy Hylton81e95022007-02-27 06:50:52 +0000668 >>> def f():
669 ... global x
670 ... nonlocal x
671 Traceback (most recent call last):
672 ...
673 SyntaxError: name 'x' is nonlocal and global
674
675 >>> def f():
676 ... nonlocal x
677 Traceback (most recent call last):
678 ...
679 SyntaxError: no binding for nonlocal 'x' found
680
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000681From SF bug #1705365
682 >>> nonlocal x
683 Traceback (most recent call last):
684 ...
685 SyntaxError: nonlocal declaration not allowed at module level
686
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300687From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600688 >>> class A:
689 ... def f(self):
690 ... nonlocal __x
691 Traceback (most recent call last):
692 ...
693 SyntaxError: no binding for nonlocal '_A__x' found
694
Guido van Rossumd8faa362007-04-27 19:54:29 +0000695
696This tests assignment-context; there was a bug in Python 2.5 where compiling
697a complex 'if' (one with 'elif') would fail to notice an invalid suite,
698leading to spurious errors.
699
700 >>> if 1:
701 ... x() = 1
702 ... elif 1:
703 ... pass
704 Traceback (most recent call last):
705 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100706 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000707
708 >>> if 1:
709 ... pass
710 ... elif 1:
711 ... x() = 1
712 Traceback (most recent call last):
713 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100714 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000715
716 >>> if 1:
717 ... x() = 1
718 ... elif 1:
719 ... pass
720 ... else:
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 ... else:
731 ... pass
732 Traceback (most recent call last):
733 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100734 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000735
736 >>> if 1:
737 ... pass
738 ... elif 1:
739 ... pass
740 ... else:
741 ... x() = 1
742 Traceback (most recent call last):
743 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100744 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hylton81e95022007-02-27 06:50:52 +0000745
Pablo Galindo58fb1562021-02-02 19:54:22 +0000746 Missing ':' before suites:
747
748 >>> def f()
749 ... pass
750 Traceback (most recent call last):
751 SyntaxError: expected ':'
752
753 >>> class A
754 ... pass
755 Traceback (most recent call last):
756 SyntaxError: expected ':'
757
758 >>> if 1
759 ... pass
760 ... elif 1:
761 ... pass
762 ... else:
763 ... x() = 1
764 Traceback (most recent call last):
765 SyntaxError: expected ':'
766
767 >>> if 1:
768 ... pass
769 ... elif 1
770 ... pass
771 ... else:
772 ... x() = 1
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 >>> for x in range(10)
786 ... pass
787 Traceback (most recent call last):
788 SyntaxError: expected ':'
789
790 >>> while True
791 ... pass
792 Traceback (most recent call last):
793 SyntaxError: expected ':'
794
795 >>> with blech as something
796 ... pass
797 Traceback (most recent call last):
798 SyntaxError: expected ':'
799
800 >>> with blech
801 ... pass
802 Traceback (most recent call last):
803 SyntaxError: expected ':'
804
805 >>> with blech, block as something
806 ... pass
807 Traceback (most recent call last):
808 SyntaxError: expected ':'
809
810 >>> with blech, block as something, bluch
811 ... pass
812 Traceback (most recent call last):
813 SyntaxError: expected ':'
814
815 >>> with (blech as something)
816 ... pass
817 Traceback (most recent call last):
818 SyntaxError: expected ':'
819
820 >>> with (blech)
821 ... pass
822 Traceback (most recent call last):
823 SyntaxError: expected ':'
824
825 >>> with (blech, block as something)
826 ... pass
827 Traceback (most recent call last):
828 SyntaxError: expected ':'
829
830 >>> with (blech, block as something, bluch)
831 ... pass
832 Traceback (most recent call last):
833 SyntaxError: expected ':'
834
835 >>> try
836 ... pass
837 Traceback (most recent call last):
838 SyntaxError: expected ':'
839
840 >>> try:
841 ... pass
842 ... except
843 ... pass
844 Traceback (most recent call last):
845 SyntaxError: expected ':'
846
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000847 >>> match x
848 ... case list():
849 ... pass
850 Traceback (most recent call last):
851 SyntaxError: expected ':'
852
853 >>> match x:
854 ... case list()
855 ... pass
856 Traceback (most recent call last):
857 SyntaxError: expected ':'
858
859 >>> match x:
860 ... case [y] if y > 0
861 ... pass
862 Traceback (most recent call last):
863 SyntaxError: expected ':'
864
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100865 >>> if x = 3:
866 ... pass
867 Traceback (most recent call last):
868 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
869
870 >>> while x = 3:
871 ... pass
872 Traceback (most recent call last):
873 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
874
875 >>> if x.a = 3:
876 ... pass
877 Traceback (most recent call last):
878 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
879
880 >>> while x.a = 3:
881 ... pass
882 Traceback (most recent call last):
883 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
884
Pablo Galindod9151cb2021-04-13 02:32:33 +0100885Ensure that early = are not matched by the parser as invalid comparisons
Pablo Galindob2802482021-04-15 21:38:45 +0100886 >>> f(2, 4, x=34); 1 $ 2
Pablo Galindod9151cb2021-04-13 02:32:33 +0100887 Traceback (most recent call last):
888 SyntaxError: invalid syntax
889
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100890 >>> dict(x=34); x $ y
891 Traceback (most recent call last):
892 SyntaxError: invalid syntax
893
894 >>> dict(x=34, (x for x in range 10), 1); x $ y
895 Traceback (most recent call last):
896 SyntaxError: invalid syntax
897
898 >>> dict(x=34, x=1, y=2); x $ y
899 Traceback (most recent call last):
900 SyntaxError: invalid syntax
901
Pablo Galindoda743502021-04-15 14:06:39 +0100902Incomplete dictionary literals
903
904 >>> {1:2, 3:4, 5}
905 Traceback (most recent call last):
906 SyntaxError: ':' expected after dictionary key
907
908 >>> {1:2, 3:4, 5:}
909 Traceback (most recent call last):
910 SyntaxError: expression expected after dictionary key and ':'
911
912 >>> {1: *12+1, 23: 1}
913 Traceback (most recent call last):
914 SyntaxError: cannot use a starred expression in a dictionary value
915
916 >>> {1: *12+1}
917 Traceback (most recent call last):
918 SyntaxError: cannot use a starred expression in a dictionary value
919
920 >>> {1: 23, 1: *12+1}
921 Traceback (most recent call last):
922 SyntaxError: cannot use a starred expression in a dictionary value
923
924 >>> {1:}
925 Traceback (most recent call last):
926 SyntaxError: expression expected after dictionary key and ':'
927
928 # Ensure that the error is not raise for syntax errors that happen after sets
929
930 >>> {1} $
931 Traceback (most recent call last):
932 SyntaxError: invalid syntax
933
Pablo Galindo56c95df2021-04-21 15:28:21 +0100934Specialized indentation errors:
935
936 >>> while condition:
937 ... pass
938 Traceback (most recent call last):
939 IndentationError: expected an indented block after 'while' statement on line 1
940
941 >>> for x in range(10):
942 ... pass
943 Traceback (most recent call last):
944 IndentationError: expected an indented block after 'for' statement on line 1
945
946 >>> for x in range(10):
947 ... pass
948 ... else:
949 ... pass
950 Traceback (most recent call last):
951 IndentationError: expected an indented block after 'else' statement on line 3
952
953 >>> async for x in range(10):
954 ... pass
955 Traceback (most recent call last):
956 IndentationError: expected an indented block after 'for' statement on line 1
957
958 >>> async for x in range(10):
959 ... pass
960 ... else:
961 ... pass
962 Traceback (most recent call last):
963 IndentationError: expected an indented block after 'else' statement on line 3
964
965 >>> if something:
966 ... pass
967 Traceback (most recent call last):
968 IndentationError: expected an indented block after 'if' statement on line 1
969
970 >>> if something:
971 ... pass
972 ... elif something_else:
973 ... pass
974 Traceback (most recent call last):
975 IndentationError: expected an indented block after 'elif' statement on line 3
976
977 >>> if something:
978 ... pass
979 ... elif something_else:
980 ... pass
981 ... else:
982 ... pass
983 Traceback (most recent call last):
984 IndentationError: expected an indented block after 'else' statement on line 5
985
986 >>> try:
987 ... pass
988 Traceback (most recent call last):
989 IndentationError: expected an indented block after 'try' statement on line 1
990
991 >>> try:
992 ... something()
993 ... except A:
994 ... pass
995 Traceback (most recent call last):
996 IndentationError: expected an indented block after 'except' statement on line 3
997
998 >>> try:
999 ... something()
1000 ... except A:
1001 ... pass
1002 ... finally:
1003 ... pass
1004 Traceback (most recent call last):
1005 IndentationError: expected an indented block after 'finally' statement on line 5
1006
1007 >>> with A:
1008 ... pass
1009 Traceback (most recent call last):
1010 IndentationError: expected an indented block after 'with' statement on line 1
1011
1012 >>> with A as a, B as b:
1013 ... pass
1014 Traceback (most recent call last):
1015 IndentationError: expected an indented block after 'with' statement on line 1
1016
1017 >>> with (A as a, B as b):
1018 ... pass
1019 Traceback (most recent call last):
1020 IndentationError: expected an indented block after 'with' statement on line 1
1021
1022 >>> async with A:
1023 ... pass
1024 Traceback (most recent call last):
1025 IndentationError: expected an indented block after 'with' statement on line 1
1026
1027 >>> async with A as a, B as b:
1028 ... pass
1029 Traceback (most recent call last):
1030 IndentationError: expected an indented block after 'with' statement on line 1
1031
1032 >>> async with (A as a, B as b):
1033 ... pass
1034 Traceback (most recent call last):
1035 IndentationError: expected an indented block after 'with' statement on line 1
1036
1037 >>> def foo(x, /, y, *, z=2):
1038 ... pass
1039 Traceback (most recent call last):
1040 IndentationError: expected an indented block after function definition on line 1
1041
1042 >>> class Blech(A):
1043 ... pass
1044 Traceback (most recent call last):
1045 IndentationError: expected an indented block after class definition on line 1
1046
1047 >>> match something:
1048 ... pass
1049 Traceback (most recent call last):
1050 IndentationError: expected an indented block after 'match' statement on line 1
1051
1052 >>> match something:
1053 ... case []:
1054 ... pass
1055 Traceback (most recent call last):
1056 IndentationError: expected an indented block after 'case' statement on line 2
1057
1058 >>> match something:
1059 ... case []:
1060 ... ...
1061 ... case {}:
1062 ... pass
1063 Traceback (most recent call last):
1064 IndentationError: expected an indented block after 'case' statement on line 4
1065
Collin Winter828f04a2007-08-31 00:04:24 +00001066Make sure that the old "raise X, Y[, Z]" form is gone:
1067 >>> raise X, Y
1068 Traceback (most recent call last):
1069 ...
1070 SyntaxError: invalid syntax
1071 >>> raise X, Y, Z
1072 Traceback (most recent call last):
1073 ...
1074 SyntaxError: invalid syntax
1075
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001076Check that an multiple exception types with missing parentheses
Pablo Galindo206cbda2021-02-07 18:42:21 +00001077raise a custom exception
1078
1079 >>> try:
1080 ... pass
1081 ... except A, B:
1082 ... pass
1083 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001084 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001085
1086 >>> try:
1087 ... pass
1088 ... except A, B, C:
1089 ... pass
1090 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001091 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001092
1093 >>> try:
1094 ... pass
1095 ... except A, B, C as blech:
1096 ... pass
1097 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001098 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001099
1100 >>> try:
1101 ... pass
1102 ... except A, B, C as blech:
1103 ... pass
1104 ... finally:
1105 ... pass
1106 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001107 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001108
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001109
1110>>> f(a=23, a=234)
1111Traceback (most recent call last):
1112 ...
Pablo Galindo254ec782020-04-03 20:37:13 +01001113SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001114
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001115>>> {1, 2, 3} = 42
1116Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001117SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001118
1119>>> {1: 2, 3: 4} = 42
1120Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001121SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001122
1123>>> f'{x}' = 42
1124Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001125SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001126
1127>>> f'{x}-{y}' = 42
1128Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001129SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001130
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +03001131>>> from t import x,
1132Traceback (most recent call last):
1133SyntaxError: trailing comma not allowed without surrounding parentheses
1134
1135>>> from t import x,y,
1136Traceback (most recent call last):
1137SyntaxError: trailing comma not allowed without surrounding parentheses
1138
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +03001139>>> (): int
1140Traceback (most recent call last):
1141SyntaxError: only single target (not tuple) can be annotated
1142>>> []: int
1143Traceback (most recent call last):
1144SyntaxError: only single target (not list) can be annotated
1145>>> (()): int
1146Traceback (most recent call last):
1147SyntaxError: only single target (not tuple) can be annotated
1148>>> ([]): int
1149Traceback (most recent call last):
1150SyntaxError: only single target (not list) can be annotated
1151
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001152Corner-cases that used to fail to raise the correct error:
1153
1154 >>> def f(*, x=lambda __debug__:0): pass
1155 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001156 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001157
Pablo Galindob0544ba2021-04-21 12:41:19 +01001158 >>> def f(*args:(lambda __debug__:0)): pass
1159 Traceback (most recent call last):
1160 SyntaxError: cannot assign to __debug__
1161
1162 >>> def f(**kwargs:(lambda __debug__:0)): pass
1163 Traceback (most recent call last):
1164 SyntaxError: cannot assign to __debug__
1165
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +03001166 >>> with (lambda *:0): pass
1167 Traceback (most recent call last):
1168 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001169
1170Corner-cases that used to crash:
1171
1172 >>> def f(**__debug__): pass
1173 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001174 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001175
1176 >>> def f(*xx, __debug__): pass
1177 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001178 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001179
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +03001180 >>> import ä £
1181 Traceback (most recent call last):
1182 SyntaxError: invalid character '£' (U+00A3)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001183"""
1184
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001185import re
1186import unittest
1187
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001188from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001189
1190class SyntaxTestCase(unittest.TestCase):
1191
1192 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001193 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001194 """Check that compiling code raises SyntaxError with errtext.
1195
1196 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001197 test of the exception raised. If subclass is specified it
1198 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001199 """
1200 try:
1201 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +00001202 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001203 if subclass and not isinstance(err, subclass):
1204 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001205 mo = re.search(errtext, str(err))
1206 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -07001207 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001208 self.assertEqual(err.filename, filename)
1209 if lineno is not None:
1210 self.assertEqual(err.lineno, lineno)
1211 if offset is not None:
1212 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001213 else:
1214 self.fail("compile() did not raise SyntaxError")
1215
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001216 def test_expression_with_assignment(self):
1217 self._check_error(
1218 "print(end1 + end2 = ' ')",
Pablo Galindo30ed93b2021-04-13 17:51:21 +01001219 'expression cannot contain assignment, perhaps you meant "=="?',
Pablo Galindoa77aac42021-04-23 14:27:05 +01001220 offset=7
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001221 )
1222
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001223 def test_curly_brace_after_primary_raises_immediately(self):
1224 self._check_error("f{", "invalid syntax", mode="single")
1225
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001226 def test_assign_call(self):
1227 self._check_error("f() = 1", "assign")
1228
1229 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -07001230 self._check_error("del (,)", "invalid syntax")
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001231 self._check_error("del 1", "cannot delete literal")
1232 self._check_error("del (1, 2)", "cannot delete literal")
1233 self._check_error("del None", "cannot delete None")
1234 self._check_error("del *x", "cannot delete starred")
1235 self._check_error("del (*x)", "cannot use starred expression")
1236 self._check_error("del (*x,)", "cannot delete starred")
1237 self._check_error("del [*x,]", "cannot delete starred")
1238 self._check_error("del f()", "cannot delete function call")
1239 self._check_error("del f(a, b)", "cannot delete function call")
1240 self._check_error("del o.f()", "cannot delete function call")
1241 self._check_error("del a[0]()", "cannot delete function call")
1242 self._check_error("del x, f()", "cannot delete function call")
1243 self._check_error("del f(), x", "cannot delete function call")
1244 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1245 self._check_error("del (a if True else b)", "cannot delete conditional")
1246 self._check_error("del +a", "cannot delete expression")
1247 self._check_error("del a, +b", "cannot delete expression")
1248 self._check_error("del a + b", "cannot delete expression")
1249 self._check_error("del (a + b, c)", "cannot delete expression")
1250 self._check_error("del (c[0], a + b)", "cannot delete expression")
1251 self._check_error("del a.b.c + 2", "cannot delete expression")
1252 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1253 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1254 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1255 self._check_error("del (a := 5)", "cannot delete named expression")
Shantanu27c0d9b2020-05-11 14:53:58 -07001256 # We don't have a special message for this, but make sure we don't
1257 # report "cannot delete name"
1258 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001259
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001260 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001261 source = """if 1:
1262 def error(a):
1263 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001264 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001265 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001266 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001267 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001268 self._check_error(source, "parameter and global", lineno=3)
1269
1270 def test_nonlocal_param_err_first(self):
1271 source = """if 1:
1272 def error(a):
1273 nonlocal a # SyntaxError
1274 def error2():
1275 b = 1
1276 global b # SyntaxError
1277 """
1278 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +00001279
Neal Norwitzfcf44352005-11-27 20:37:43 +00001280 def test_break_outside_loop(self):
1281 self._check_error("break", "outside loop")
1282
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001283 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001284 self._check_error("if 0: yield", "outside function")
1285 self._check_error("if 0: yield\nelse: x=1", "outside function")
1286 self._check_error("if 1: pass\nelse: yield", "outside function")
1287 self._check_error("while 0: yield", "outside function")
1288 self._check_error("while 0: yield\nelse: x=1", "outside function")
1289 self._check_error("class C:\n if 0: yield", "outside function")
1290 self._check_error("class C:\n if 1: pass\n else: yield",
1291 "outside function")
1292 self._check_error("class C:\n while 0: yield", "outside function")
1293 self._check_error("class C:\n while 0: yield\n else: x = 1",
1294 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001295
1296 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001297 self._check_error("if 0: return", "outside function")
1298 self._check_error("if 0: return\nelse: x=1", "outside function")
1299 self._check_error("if 1: pass\nelse: return", "outside function")
1300 self._check_error("while 0: return", "outside function")
1301 self._check_error("class C:\n if 0: return", "outside function")
1302 self._check_error("class C:\n while 0: return", "outside function")
1303 self._check_error("class C:\n while 0: return\n else: x=1",
1304 "outside function")
1305 self._check_error("class C:\n if 0: return\n else: x= 1",
1306 "outside function")
1307 self._check_error("class C:\n if 1: pass\n else: return",
1308 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001309
1310 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001311 self._check_error("if 0: break", "outside loop")
1312 self._check_error("if 0: break\nelse: x=1", "outside loop")
1313 self._check_error("if 1: pass\nelse: break", "outside loop")
1314 self._check_error("class C:\n if 0: break", "outside loop")
1315 self._check_error("class C:\n if 1: pass\n else: break",
1316 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001317
1318 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001319 self._check_error("if 0: continue", "not properly in loop")
1320 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1321 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1322 self._check_error("class C:\n if 0: continue", "not properly in loop")
1323 self._check_error("class C:\n if 1: pass\n else: continue",
1324 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001325
Thomas Wouters477c8d52006-05-27 19:21:47 +00001326 def test_unexpected_indent(self):
1327 self._check_error("foo()\n bar()\n", "unexpected indent",
1328 subclass=IndentationError)
1329
1330 def test_no_indent(self):
1331 self._check_error("if 1:\nfoo()", "expected an indented block",
1332 subclass=IndentationError)
1333
1334 def test_bad_outdent(self):
1335 self._check_error("if 1:\n foo()\n bar()",
1336 "unindent does not match .* level",
1337 subclass=IndentationError)
1338
1339 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001340 self._check_error("int(base=10, '2')",
1341 "positional argument follows keyword argument")
1342
1343 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001344 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001345 "positional argument follows "
1346 "keyword argument unpacking")
1347
1348 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001349 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001350 "iterable argument unpacking follows "
1351 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001352
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001353 def test_empty_line_after_linecont(self):
1354 # See issue-40847
1355 s = r"""\
1356pass
1357 \
1358
1359pass
1360"""
1361 try:
1362 compile(s, '<string>', 'exec')
1363 except SyntaxError:
1364 self.fail("Empty line after a line continuation character is valid.")
1365
Mark Shannon02d126a2020-09-25 14:04:19 +01001366 @support.cpython_only
1367 def test_nested_named_except_blocks(self):
1368 code = ""
1369 for i in range(12):
1370 code += f"{' '*i}try:\n"
1371 code += f"{' '*(i+1)}raise Exception\n"
1372 code += f"{' '*i}except Exception as e:\n"
1373 code += f"{' '*4*12}pass"
1374 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001375
Pablo Galindo06f8c332020-10-30 23:48:42 +00001376 def test_barry_as_flufl_with_syntax_errors(self):
1377 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
1378 # is reading the wrong token in the presence of syntax errors later
1379 # in the file. See bpo-42214 for more information.
1380 code = """
1381def func1():
1382 if a != b:
1383 raise ValueError
1384
1385def func2():
1386 try
1387 return 1
1388 finally:
1389 pass
1390"""
Pablo Galindo58fb1562021-02-02 19:54:22 +00001391 self._check_error(code, "expected ':'")
Pablo Galindo06f8c332020-10-30 23:48:42 +00001392
Pablo Galindo96eeff52021-03-22 17:28:11 +00001393 def test_invalid_line_continuation_error_position(self):
1394 self._check_error(r"a = 3 \ 4",
1395 "unexpected character after line continuation character",
1396 lineno=1, offset=9)
1397
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +02001398 def test_invalid_line_continuation_left_recursive(self):
1399 # Check bpo-42218: SyntaxErrors following left-recursive rules
1400 # (t_primary_raw in this case) need to be tested explicitly
1401 self._check_error("A.\u018a\\ ",
1402 "unexpected character after line continuation character")
1403 self._check_error("A.\u03bc\\\n",
1404 "unexpected EOF while parsing")
1405
Pablo Galindod6d63712021-01-19 23:59:33 +00001406 def test_error_parenthesis(self):
1407 for paren in "([{":
1408 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
1409
1410 for paren in ")]}":
1411 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
1412
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001413 def test_match_call_does_not_raise_syntax_error(self):
1414 code = """
1415def match(x):
1416 return 1+1
1417
1418match(34)
1419"""
1420 compile(code, "<string>", "exec")
1421
1422 def test_case_call_does_not_raise_syntax_error(self):
1423 code = """
1424def case(x):
1425 return 1+1
1426
1427case(34)
1428"""
1429 compile(code, "<string>", "exec")
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -07001430
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07001431 def test_multiline_compiler_error_points_to_the_end(self):
1432 self._check_error(
1433 "call(\na=1,\na=1\n)",
1434 "keyword argument repeated",
1435 lineno=3
1436 )
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001437
Pablo Galindod6d63712021-01-19 23:59:33 +00001438
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001439def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001440 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001441 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001442 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001443
1444if __name__ == "__main__":
1445 test_main()