blob: d5a52ba628a8169e619bfb3aeec38a65a3c48089 [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
Miss Islington (bot)56717622021-08-02 12:05:33 -0700143>>> a = 42 if True
144Traceback (most recent call last):
145SyntaxError: expected 'else' after 'if' expression
146
147>>> a = (42 if True)
148Traceback (most recent call last):
149SyntaxError: expected 'else' after 'if' expression
150
151>>> a = [1, 42 if True, 4]
152Traceback (most recent call last):
153SyntaxError: expected 'else' after 'if' expression
154
Pablo Galindo9f495902020-06-08 02:57:00 +0100155>>> True = True = 3
156Traceback (most recent call last):
157SyntaxError: cannot assign to True
158
159>>> x = y = True = z = 3
160Traceback (most recent call last):
161SyntaxError: cannot assign to True
162
163>>> x = y = yield = 1
164Traceback (most recent call last):
165SyntaxError: assignment to yield expression not possible
166
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300167>>> a, b += 1, 2
168Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100169SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300170
171>>> (a, b) += 1, 2
172Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100173SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300174
175>>> [a, b] += 1, 2
176Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100177SyntaxError: 'list' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300178
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300179Invalid targets in `for` loops and `with` statements should also
180produce a specialized error message
181
182>>> for a() in b: pass
183Traceback (most recent call last):
184SyntaxError: cannot assign to function call
185
186>>> for (a, b()) in b: pass
187Traceback (most recent call last):
188SyntaxError: cannot assign to function call
189
190>>> for [a, b()] in b: pass
191Traceback (most recent call last):
192SyntaxError: cannot assign to function call
193
194>>> for (*a, b, c+1) in b: 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 (x, *(y, z.d())) in b: pass
199Traceback (most recent call last):
200SyntaxError: cannot assign to function call
201
202>>> for a, b() in c: pass
203Traceback (most recent call last):
204SyntaxError: cannot assign to function call
205
206>>> for a, b, (c + 1, d()): pass
207Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100208SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300209
210>>> for i < (): pass
211Traceback (most recent call last):
212SyntaxError: invalid syntax
213
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300214>>> for a, b
215Traceback (most recent call last):
216SyntaxError: invalid syntax
217
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300218>>> with a as b(): pass
219Traceback (most recent call last):
220SyntaxError: cannot assign to function call
221
222>>> with a as (b, c()): pass
223Traceback (most recent call last):
224SyntaxError: cannot assign to function call
225
226>>> with a as [b, c()]: pass
227Traceback (most recent call last):
228SyntaxError: cannot assign to function call
229
230>>> with a as (*b, c, d+1): pass
231Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100232SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300233
234>>> with a as (x, *(y, z.d())): pass
235Traceback (most recent call last):
236SyntaxError: cannot assign to function call
237
238>>> with a as b, c as d(): pass
239Traceback (most recent call last):
240SyntaxError: cannot assign to function call
241
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300242>>> with a as b
243Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000244SyntaxError: expected ':'
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300245
Pablo Galindo9f495902020-06-08 02:57:00 +0100246>>> p = p =
247Traceback (most recent call last):
248SyntaxError: invalid syntax
249
Pablo Galindo835f14f2021-01-31 22:52:56 +0000250Comprehensions creating tuples without parentheses
251should produce a specialized error message:
252
253>>> [x,y for x,y in range(100)]
254Traceback (most recent call last):
255SyntaxError: did you forget parentheses around the comprehension target?
256
257>>> {x,y for x,y in range(100)}
258Traceback (most recent call last):
259SyntaxError: did you forget parentheses around the comprehension target?
260
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000261# Missing commas in literals collections should not
262# produce special error messages regarding missing
Pablo Galindob2802482021-04-15 21:38:45 +0100263# parentheses, but about missing commas instead
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000264
265>>> [1, 2 3]
Pablo Galindo835f14f2021-01-31 22:52:56 +0000266Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100267SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000268
269>>> {1, 2 3}
270Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100271SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000272
273>>> {1:2, 2:5 3:12}
274Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100275SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000276
277>>> (1, 2 3)
278Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100279SyntaxError: invalid syntax. Perhaps you forgot a comma?
280
281# Make sure soft keywords constructs don't raise specialized
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700282# errors regarding missing commas or other spezialiced errors
Pablo Galindob2802482021-04-15 21:38:45 +0100283
284>>> match x:
285... y = 3
286Traceback (most recent call last):
287SyntaxError: invalid syntax
288
289>>> match x:
290... case y:
291... 3 $ 3
292Traceback (most recent call last):
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000293SyntaxError: invalid syntax
Pablo Galindo835f14f2021-01-31 22:52:56 +0000294
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700295>>> match x:
296... case $:
297... ...
298Traceback (most recent call last):
299SyntaxError: invalid syntax
300
301>>> match ...:
302... case {**rest, "key": value}:
303... ...
304Traceback (most recent call last):
305SyntaxError: invalid syntax
306
307>>> match ...:
308... case {**_}:
309... ...
310Traceback (most recent call last):
311SyntaxError: invalid syntax
312
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000313From compiler_complex_args():
314
315>>> def f(None=1):
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
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000320From ast_for_arguments():
321
322>>> def f(x, y=1, z):
323... pass
324Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000325SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000326
327>>> def f(x, None):
328... pass
329Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000330SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000331
332>>> def f(*None):
333... pass
334Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000335SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000336
337>>> def f(**None):
338... pass
339Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000340SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000341
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300342>>> import ast; ast.parse('''
343... def f(
344... *, # type: int
345... a, # type: int
346... ):
347... pass
348... ''', type_comments=True)
349Traceback (most recent call last):
350SyntaxError: bare * has associated type comment
351
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000352
353From ast_for_funcdef():
354
355>>> def None(x):
356... pass
357Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000358SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000359
360
361From ast_for_call():
362
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200363>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000364... return list(it)
365>>> L = range(10)
366>>> f(x for x in L)
367[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
368>>> f(x for x in L, 1)
369Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200370SyntaxError: Generator expression must be parenthesized
371>>> f(x for x in L, y=1)
372Traceback (most recent call last):
373SyntaxError: Generator expression must be parenthesized
374>>> f(x for x in L, *[])
375Traceback (most recent call last):
376SyntaxError: Generator expression must be parenthesized
377>>> f(x for x in L, **{})
378Traceback (most recent call last):
379SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300380>>> f(L, x for x in L)
381Traceback (most recent call last):
382SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400383>>> f(x for x in L, y for y in L)
384Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200385SyntaxError: Generator expression must be parenthesized
386>>> f(x for x in L,)
387Traceback (most recent call last):
388SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000389>>> f((x for x in L), 1)
390[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200391>>> class C(x for x in L):
392... pass
393Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000394SyntaxError: expected ':'
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000395
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200396>>> def g(*args, **kwargs):
397... print(args, sorted(kwargs.items()))
398>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
399... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
400... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
401... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
402... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
403... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
404... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
405... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
406... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
407... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
408... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
409... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
410... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
411... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
412... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
413... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
414... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
415... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
416... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
417... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
418(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000419
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200420>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
421... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
422... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
423... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
424... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
425... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
426... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
427... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
428... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
429... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
430... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
431... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
432... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
433... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
434... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
435... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
436... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
437... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
438... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
439... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
440... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
441... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
442... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
443... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
444... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
445... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
446... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
447... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
448... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
449... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
450... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
451... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
452... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
453... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
454... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
455... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
456... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
457... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
458... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
459... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
460... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
461... # doctest: +ELLIPSIS
462() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000463
Yury Selivanovf2392132016-12-13 19:03:51 -0500464>>> class C:
465... def meth(self, *args):
466... return args
467>>> obj = C()
468>>> obj.meth(
469... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
470... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
471... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
472... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
473... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
474... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
475... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
476... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
477... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
478... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
479... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
480... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
481... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
482... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
483... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
484... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
485... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
486... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
487... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
488... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
489(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
490
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700491>>> f(lambda x: x[0] = 3)
492Traceback (most recent call last):
493SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
494
495# Check that this error doesn't trigger for names:
496>>> f(a={x: for x in {}})
497Traceback (most recent call last):
498SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000499
500The grammar accepts any test (basically, any expression) in the
501keyword slot of a call site. Test a few different options.
502
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700503>>> f(x()=2)
504Traceback (most recent call last):
505SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
506>>> f(a or b=1)
507Traceback (most recent call last):
508SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
509>>> f(x.y=1)
510Traceback (most recent call last):
511SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
512>>> f((x)=2)
513Traceback (most recent call last):
514SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
515>>> f(True=2)
516Traceback (most recent call last):
517SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200518>>> f(__debug__=1)
519Traceback (most recent call last):
520SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100521>>> __debug__: int
522Traceback (most recent call last):
523SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000524
525
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000526More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000527
528>>> (x for x in x) += 1
529Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100530SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000531>>> None += 1
532Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100533SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200534>>> __debug__ += 1
535Traceback (most recent call last):
536SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000537>>> f() += 1
538Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100539SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000540
541
542Test continue in finally in weird combinations.
543
Ezio Melotti13925002011-03-16 11:05:33 +0200544continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000545
546 >>> def test():
547 ... try:
548 ... pass
549 ... finally:
550 ... for abc in range(10):
551 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000552 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000553 >>> test()
554 9
555
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200556continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000557
558 >>> def test():
559 ... for abc in range(10):
560 ... try:
561 ... pass
562 ... finally:
563 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200564 ... print(abc)
565 >>> test()
566 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000567
568 >>> def test():
569 ... for abc in range(10):
570 ... try:
571 ... pass
572 ... finally:
573 ... try:
574 ... continue
575 ... except:
576 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200577 ... print(abc)
578 >>> test()
579 9
580
581 >>> def test():
582 ... for abc in range(10):
583 ... try:
584 ... pass
585 ... finally:
586 ... try:
587 ... pass
588 ... except:
589 ... continue
590 ... print(abc)
591 >>> test()
592 9
593
594A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000595
596 >>> def foo():
597 ... try:
598 ... pass
599 ... finally:
600 ... continue
601 Traceback (most recent call last):
602 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200603 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000604
605There is one test for a break that is not in a loop. The compiler
606uses a single data structure to keep track of try-finally and loops,
607so we need to be sure that a break is actually inside a loop. If it
608isn't, there should be a syntax error.
609
610 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000611 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000612 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000613 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000614 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000615 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000616 Traceback (most recent call last):
617 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000618 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000619
Benjamin Petersone09ed542016-07-14 22:00:03 -0700620This raises a SyntaxError, it used to raise a SystemError.
621Context for this change can be found on issue #27514
622
Thomas Wouters89f507f2006-12-13 04:49:30 +0000623In 2.5 there was a missing exception and an assert was triggered in a debug
624build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
625
626 >>> while 1:
627 ... while 2:
628 ... while 3:
629 ... while 4:
630 ... while 5:
631 ... while 6:
632 ... while 8:
633 ... while 9:
634 ... while 10:
635 ... while 11:
636 ... while 12:
637 ... while 13:
638 ... while 14:
639 ... while 15:
640 ... while 16:
641 ... while 17:
642 ... while 18:
643 ... while 19:
644 ... while 20:
645 ... while 21:
646 ... while 22:
647 ... break
648 Traceback (most recent call last):
649 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700650 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000651
Guido van Rossum6cff8742016-09-09 09:36:26 -0700652Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
653
654 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200655 ... print(x)
656 ... global x
657 Traceback (most recent call last):
658 ...
659 SyntaxError: name 'x' is used prior to global declaration
660
661 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700662 ... x = 1
663 ... global x
664 Traceback (most recent call last):
665 ...
666 SyntaxError: name 'x' is assigned to before global declaration
667
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200668 >>> def f(x):
669 ... global x
670 Traceback (most recent call last):
671 ...
672 SyntaxError: name 'x' is parameter and global
673
Guido van Rossum6cff8742016-09-09 09:36:26 -0700674 >>> def f():
675 ... x = 1
676 ... def g():
677 ... print(x)
678 ... nonlocal x
679 Traceback (most recent call last):
680 ...
681 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000682
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300683 >>> def f():
684 ... x = 1
685 ... def g():
686 ... x = 2
687 ... nonlocal x
688 Traceback (most recent call last):
689 ...
690 SyntaxError: name 'x' is assigned to before nonlocal declaration
691
Jeremy Hylton81e95022007-02-27 06:50:52 +0000692 >>> def f(x):
693 ... nonlocal x
694 Traceback (most recent call last):
695 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000697
Jeremy Hylton81e95022007-02-27 06:50:52 +0000698 >>> def f():
699 ... global x
700 ... nonlocal x
701 Traceback (most recent call last):
702 ...
703 SyntaxError: name 'x' is nonlocal and global
704
705 >>> def f():
706 ... nonlocal x
707 Traceback (most recent call last):
708 ...
709 SyntaxError: no binding for nonlocal 'x' found
710
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000711From SF bug #1705365
712 >>> nonlocal x
713 Traceback (most recent call last):
714 ...
715 SyntaxError: nonlocal declaration not allowed at module level
716
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300717From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600718 >>> class A:
719 ... def f(self):
720 ... nonlocal __x
721 Traceback (most recent call last):
722 ...
723 SyntaxError: no binding for nonlocal '_A__x' found
724
Guido van Rossumd8faa362007-04-27 19:54:29 +0000725
726This tests assignment-context; there was a bug in Python 2.5 where compiling
727a complex 'if' (one with 'elif') would fail to notice an invalid suite,
728leading to spurious errors.
729
730 >>> if 1:
731 ... x() = 1
732 ... elif 1:
733 ... pass
734 Traceback (most recent call last):
735 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100736 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000737
738 >>> if 1:
739 ... pass
740 ... elif 1:
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 '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000745
746 >>> if 1:
747 ... x() = 1
748 ... elif 1:
749 ... pass
750 ... else:
751 ... pass
752 Traceback (most recent call last):
753 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100754 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000755
756 >>> if 1:
757 ... pass
758 ... elif 1:
759 ... x() = 1
760 ... else:
761 ... pass
762 Traceback (most recent call last):
763 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100764 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000765
766 >>> if 1:
767 ... pass
768 ... elif 1:
769 ... pass
770 ... else:
771 ... x() = 1
772 Traceback (most recent call last):
773 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100774 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hylton81e95022007-02-27 06:50:52 +0000775
Pablo Galindo58fb1562021-02-02 19:54:22 +0000776 Missing ':' before suites:
777
778 >>> def f()
779 ... pass
780 Traceback (most recent call last):
781 SyntaxError: expected ':'
782
783 >>> class A
784 ... pass
785 Traceback (most recent call last):
786 SyntaxError: expected ':'
787
788 >>> if 1
789 ... pass
790 ... elif 1:
791 ... pass
792 ... else:
793 ... x() = 1
794 Traceback (most recent call last):
795 SyntaxError: expected ':'
796
797 >>> if 1:
798 ... pass
799 ... elif 1
800 ... pass
801 ... else:
802 ... x() = 1
803 Traceback (most recent call last):
804 SyntaxError: expected ':'
805
806 >>> if 1:
807 ... pass
808 ... elif 1:
809 ... pass
810 ... else
811 ... x() = 1
812 Traceback (most recent call last):
813 SyntaxError: expected ':'
814
815 >>> for x in range(10)
816 ... pass
817 Traceback (most recent call last):
818 SyntaxError: expected ':'
819
820 >>> while True
821 ... pass
822 Traceback (most recent call last):
823 SyntaxError: expected ':'
824
825 >>> with blech as something
826 ... pass
827 Traceback (most recent call last):
828 SyntaxError: expected ':'
829
830 >>> with blech
831 ... pass
832 Traceback (most recent call last):
833 SyntaxError: expected ':'
834
835 >>> with blech, block as something
836 ... pass
837 Traceback (most recent call last):
838 SyntaxError: expected ':'
839
840 >>> with blech, block as something, bluch
841 ... pass
842 Traceback (most recent call last):
843 SyntaxError: expected ':'
844
845 >>> with (blech as something)
846 ... pass
847 Traceback (most recent call last):
848 SyntaxError: expected ':'
849
850 >>> with (blech)
851 ... pass
852 Traceback (most recent call last):
853 SyntaxError: expected ':'
854
855 >>> with (blech, block as something)
856 ... pass
857 Traceback (most recent call last):
858 SyntaxError: expected ':'
859
860 >>> with (blech, block as something, bluch)
861 ... pass
862 Traceback (most recent call last):
863 SyntaxError: expected ':'
864
865 >>> try
866 ... pass
867 Traceback (most recent call last):
868 SyntaxError: expected ':'
869
870 >>> try:
871 ... pass
872 ... except
873 ... pass
874 Traceback (most recent call last):
875 SyntaxError: expected ':'
876
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000877 >>> match x
878 ... case list():
879 ... pass
880 Traceback (most recent call last):
881 SyntaxError: expected ':'
882
883 >>> match x:
884 ... case list()
885 ... pass
886 Traceback (most recent call last):
887 SyntaxError: expected ':'
888
889 >>> match x:
890 ... case [y] if y > 0
891 ... pass
892 Traceback (most recent call last):
893 SyntaxError: expected ':'
894
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100895 >>> if x = 3:
896 ... pass
897 Traceback (most recent call last):
898 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
899
900 >>> while x = 3:
901 ... pass
902 Traceback (most recent call last):
903 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
904
905 >>> if x.a = 3:
906 ... pass
907 Traceback (most recent call last):
908 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
909
910 >>> while x.a = 3:
911 ... pass
912 Traceback (most recent call last):
913 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
914
Pablo Galindoe53f72a2021-06-04 00:11:43 +0100915Custom error messages for try blocks that are not followed by except/finally
916
917 >>> try:
918 ... x = 34
919 ...
920 Traceback (most recent call last):
921 SyntaxError: expected 'except' or 'finally' block
922
Pablo Galindod9151cb2021-04-13 02:32:33 +0100923Ensure that early = are not matched by the parser as invalid comparisons
Pablo Galindob2802482021-04-15 21:38:45 +0100924 >>> f(2, 4, x=34); 1 $ 2
Pablo Galindod9151cb2021-04-13 02:32:33 +0100925 Traceback (most recent call last):
926 SyntaxError: invalid syntax
927
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100928 >>> dict(x=34); x $ y
929 Traceback (most recent call last):
930 SyntaxError: invalid syntax
931
932 >>> dict(x=34, (x for x in range 10), 1); x $ y
933 Traceback (most recent call last):
934 SyntaxError: invalid syntax
935
936 >>> dict(x=34, x=1, y=2); x $ y
937 Traceback (most recent call last):
938 SyntaxError: invalid syntax
939
Pablo Galindoda743502021-04-15 14:06:39 +0100940Incomplete dictionary literals
941
942 >>> {1:2, 3:4, 5}
943 Traceback (most recent call last):
944 SyntaxError: ':' expected after dictionary key
945
946 >>> {1:2, 3:4, 5:}
947 Traceback (most recent call last):
948 SyntaxError: expression expected after dictionary key and ':'
949
950 >>> {1: *12+1, 23: 1}
951 Traceback (most recent call last):
952 SyntaxError: cannot use a starred expression in a dictionary value
953
954 >>> {1: *12+1}
955 Traceback (most recent call last):
956 SyntaxError: cannot use a starred expression in a dictionary value
957
958 >>> {1: 23, 1: *12+1}
959 Traceback (most recent call last):
960 SyntaxError: cannot use a starred expression in a dictionary value
961
962 >>> {1:}
963 Traceback (most recent call last):
964 SyntaxError: expression expected after dictionary key and ':'
965
966 # Ensure that the error is not raise for syntax errors that happen after sets
967
968 >>> {1} $
969 Traceback (most recent call last):
970 SyntaxError: invalid syntax
971
Pablo Galindo56c95df2021-04-21 15:28:21 +0100972Specialized indentation errors:
973
974 >>> while condition:
975 ... pass
976 Traceback (most recent call last):
977 IndentationError: expected an indented block after 'while' statement on line 1
978
979 >>> 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 >>> 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 >>> async for x in range(10):
992 ... pass
993 Traceback (most recent call last):
994 IndentationError: expected an indented block after 'for' statement on line 1
995
996 >>> async for x in range(10):
997 ... pass
998 ... else:
999 ... pass
1000 Traceback (most recent call last):
1001 IndentationError: expected an indented block after 'else' statement on line 3
1002
1003 >>> if something:
1004 ... pass
1005 Traceback (most recent call last):
1006 IndentationError: expected an indented block after 'if' statement on line 1
1007
1008 >>> if something:
1009 ... pass
1010 ... elif something_else:
1011 ... pass
1012 Traceback (most recent call last):
1013 IndentationError: expected an indented block after 'elif' statement on line 3
1014
1015 >>> if something:
1016 ... pass
1017 ... elif something_else:
1018 ... pass
1019 ... else:
1020 ... pass
1021 Traceback (most recent call last):
1022 IndentationError: expected an indented block after 'else' statement on line 5
1023
1024 >>> try:
1025 ... pass
1026 Traceback (most recent call last):
1027 IndentationError: expected an indented block after 'try' statement on line 1
1028
1029 >>> try:
1030 ... something()
1031 ... except A:
1032 ... pass
1033 Traceback (most recent call last):
1034 IndentationError: expected an indented block after 'except' statement on line 3
1035
1036 >>> try:
1037 ... something()
1038 ... except A:
1039 ... pass
1040 ... finally:
1041 ... pass
1042 Traceback (most recent call last):
1043 IndentationError: expected an indented block after 'finally' statement on line 5
1044
1045 >>> with A:
1046 ... pass
1047 Traceback (most recent call last):
1048 IndentationError: expected an indented block after 'with' statement on line 1
1049
1050 >>> with A as a, B as b:
1051 ... pass
1052 Traceback (most recent call last):
1053 IndentationError: expected an indented block after 'with' statement on line 1
1054
1055 >>> with (A as a, B as b):
1056 ... pass
1057 Traceback (most recent call last):
1058 IndentationError: expected an indented block after 'with' statement on line 1
1059
1060 >>> async with A:
1061 ... pass
1062 Traceback (most recent call last):
1063 IndentationError: expected an indented block after 'with' statement on line 1
1064
1065 >>> async with A as a, B as b:
1066 ... pass
1067 Traceback (most recent call last):
1068 IndentationError: expected an indented block after 'with' statement on line 1
1069
1070 >>> async with (A as a, B as b):
1071 ... pass
1072 Traceback (most recent call last):
1073 IndentationError: expected an indented block after 'with' statement on line 1
1074
1075 >>> def foo(x, /, y, *, z=2):
1076 ... pass
1077 Traceback (most recent call last):
1078 IndentationError: expected an indented block after function definition on line 1
1079
1080 >>> class Blech(A):
1081 ... pass
1082 Traceback (most recent call last):
1083 IndentationError: expected an indented block after class definition on line 1
1084
1085 >>> match something:
1086 ... pass
1087 Traceback (most recent call last):
1088 IndentationError: expected an indented block after 'match' statement on line 1
1089
1090 >>> match something:
1091 ... case []:
1092 ... pass
1093 Traceback (most recent call last):
1094 IndentationError: expected an indented block after 'case' statement on line 2
1095
1096 >>> match something:
1097 ... case []:
1098 ... ...
1099 ... case {}:
1100 ... pass
1101 Traceback (most recent call last):
1102 IndentationError: expected an indented block after 'case' statement on line 4
1103
Collin Winter828f04a2007-08-31 00:04:24 +00001104Make sure that the old "raise X, Y[, Z]" form is gone:
1105 >>> raise X, Y
1106 Traceback (most recent call last):
1107 ...
1108 SyntaxError: invalid syntax
1109 >>> raise X, Y, Z
1110 Traceback (most recent call last):
1111 ...
1112 SyntaxError: invalid syntax
1113
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001114Check that an multiple exception types with missing parentheses
Pablo Galindo206cbda2021-02-07 18:42:21 +00001115raise a custom exception
1116
1117 >>> try:
1118 ... pass
1119 ... except A, B:
1120 ... pass
1121 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001122 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001123
1124 >>> try:
1125 ... pass
1126 ... except A, B, C:
1127 ... pass
1128 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001129 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001130
1131 >>> try:
1132 ... pass
1133 ... except A, B, C as blech:
1134 ... pass
1135 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001136 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001137
1138 >>> try:
1139 ... pass
1140 ... except A, B, C as blech:
1141 ... pass
1142 ... finally:
1143 ... pass
1144 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001145 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001146
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001147
1148>>> f(a=23, a=234)
1149Traceback (most recent call last):
1150 ...
Pablo Galindo254ec782020-04-03 20:37:13 +01001151SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001152
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001153>>> {1, 2, 3} = 42
1154Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001155SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001156
1157>>> {1: 2, 3: 4} = 42
1158Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001159SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001160
1161>>> f'{x}' = 42
1162Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001163SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001164
1165>>> f'{x}-{y}' = 42
1166Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001167SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001168
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -07001169>>> (x, y, z=3, d, e)
1170Traceback (most recent call last):
1171SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1172
1173>>> [x, y, z=3, d, e]
1174Traceback (most recent call last):
1175SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1176
1177>>> [z=3]
1178Traceback (most recent call last):
1179SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1180
1181>>> {x, y, z=3, d, e}
1182Traceback (most recent call last):
1183SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1184
1185>>> {z=3}
1186Traceback (most recent call last):
1187SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1188
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +03001189>>> from t import x,
1190Traceback (most recent call last):
1191SyntaxError: trailing comma not allowed without surrounding parentheses
1192
1193>>> from t import x,y,
1194Traceback (most recent call last):
1195SyntaxError: trailing comma not allowed without surrounding parentheses
1196
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +03001197>>> (): int
1198Traceback (most recent call last):
1199SyntaxError: only single target (not tuple) can be annotated
1200>>> []: int
1201Traceback (most recent call last):
1202SyntaxError: only single target (not list) can be annotated
1203>>> (()): int
1204Traceback (most recent call last):
1205SyntaxError: only single target (not tuple) can be annotated
1206>>> ([]): int
1207Traceback (most recent call last):
1208SyntaxError: only single target (not list) can be annotated
1209
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001210Corner-cases that used to fail to raise the correct error:
1211
1212 >>> def f(*, x=lambda __debug__:0): pass
1213 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001214 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001215
Pablo Galindob0544ba2021-04-21 12:41:19 +01001216 >>> def f(*args:(lambda __debug__:0)): pass
1217 Traceback (most recent call last):
1218 SyntaxError: cannot assign to __debug__
1219
1220 >>> def f(**kwargs:(lambda __debug__:0)): pass
1221 Traceback (most recent call last):
1222 SyntaxError: cannot assign to __debug__
1223
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +03001224 >>> with (lambda *:0): pass
1225 Traceback (most recent call last):
1226 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001227
1228Corner-cases that used to crash:
1229
1230 >>> def f(**__debug__): pass
1231 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001232 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001233
1234 >>> def f(*xx, __debug__): pass
1235 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001236 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001237
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +03001238 >>> import ä £
1239 Traceback (most recent call last):
1240 SyntaxError: invalid character '£' (U+00A3)
Pablo Galindoa8c418d2021-06-18 22:15:57 +01001241
1242 Invalid pattern matching constructs:
1243
1244 >>> match ...:
1245 ... case 42 as _:
1246 ... ...
1247 Traceback (most recent call last):
1248 SyntaxError: cannot use '_' as a target
1249
1250 >>> match ...:
1251 ... case 42 as 1+2+4:
1252 ... ...
1253 Traceback (most recent call last):
1254 SyntaxError: invalid pattern target
Miss Islington (bot)11f1a302021-06-24 08:34:28 -07001255
1256 >>> match ...:
1257 ... case Foo(z=1, y=2, x):
1258 ... ...
1259 Traceback (most recent call last):
1260 SyntaxError: positional patterns follow keyword patterns
1261
1262 >>> match ...:
1263 ... case Foo(a, z=1, y=2, x):
1264 ... ...
1265 Traceback (most recent call last):
1266 SyntaxError: positional patterns follow keyword patterns
1267
1268 >>> match ...:
1269 ... case Foo(z=1, x, y=2):
1270 ... ...
1271 Traceback (most recent call last):
1272 SyntaxError: positional patterns follow keyword patterns
1273
1274 >>> match ...:
1275 ... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
1276 ... ...
1277 Traceback (most recent call last):
1278 SyntaxError: positional patterns follow keyword patterns
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001279"""
1280
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001281import re
1282import unittest
1283
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001284from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001285
1286class SyntaxTestCase(unittest.TestCase):
1287
1288 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001289 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001290 """Check that compiling code raises SyntaxError with errtext.
1291
1292 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001293 test of the exception raised. If subclass is specified it
1294 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001295 """
1296 try:
1297 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +00001298 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001299 if subclass and not isinstance(err, subclass):
1300 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001301 mo = re.search(errtext, str(err))
1302 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -07001303 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001304 self.assertEqual(err.filename, filename)
1305 if lineno is not None:
1306 self.assertEqual(err.lineno, lineno)
1307 if offset is not None:
1308 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001309 else:
1310 self.fail("compile() did not raise SyntaxError")
1311
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001312 def test_expression_with_assignment(self):
1313 self._check_error(
1314 "print(end1 + end2 = ' ')",
Pablo Galindo30ed93b2021-04-13 17:51:21 +01001315 'expression cannot contain assignment, perhaps you meant "=="?',
Pablo Galindoa77aac42021-04-23 14:27:05 +01001316 offset=7
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001317 )
1318
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001319 def test_curly_brace_after_primary_raises_immediately(self):
Pablo Galindo Salgadob977f852021-07-27 18:52:32 +01001320 self._check_error("f{}", "invalid syntax", mode="single")
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001321
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001322 def test_assign_call(self):
1323 self._check_error("f() = 1", "assign")
1324
1325 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -07001326 self._check_error("del (,)", "invalid syntax")
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001327 self._check_error("del 1", "cannot delete literal")
1328 self._check_error("del (1, 2)", "cannot delete literal")
1329 self._check_error("del None", "cannot delete None")
1330 self._check_error("del *x", "cannot delete starred")
1331 self._check_error("del (*x)", "cannot use starred expression")
1332 self._check_error("del (*x,)", "cannot delete starred")
1333 self._check_error("del [*x,]", "cannot delete starred")
1334 self._check_error("del f()", "cannot delete function call")
1335 self._check_error("del f(a, b)", "cannot delete function call")
1336 self._check_error("del o.f()", "cannot delete function call")
1337 self._check_error("del a[0]()", "cannot delete function call")
1338 self._check_error("del x, f()", "cannot delete function call")
1339 self._check_error("del f(), x", "cannot delete function call")
1340 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1341 self._check_error("del (a if True else b)", "cannot delete conditional")
1342 self._check_error("del +a", "cannot delete expression")
1343 self._check_error("del a, +b", "cannot delete expression")
1344 self._check_error("del a + b", "cannot delete expression")
1345 self._check_error("del (a + b, c)", "cannot delete expression")
1346 self._check_error("del (c[0], a + b)", "cannot delete expression")
1347 self._check_error("del a.b.c + 2", "cannot delete expression")
1348 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1349 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1350 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1351 self._check_error("del (a := 5)", "cannot delete named expression")
Shantanu27c0d9b2020-05-11 14:53:58 -07001352 # We don't have a special message for this, but make sure we don't
1353 # report "cannot delete name"
1354 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001355
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001356 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001357 source = """if 1:
1358 def error(a):
1359 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001360 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001361 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001362 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001363 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001364 self._check_error(source, "parameter and global", lineno=3)
1365
1366 def test_nonlocal_param_err_first(self):
1367 source = """if 1:
1368 def error(a):
1369 nonlocal a # SyntaxError
1370 def error2():
1371 b = 1
1372 global b # SyntaxError
1373 """
1374 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +00001375
Neal Norwitzfcf44352005-11-27 20:37:43 +00001376 def test_break_outside_loop(self):
1377 self._check_error("break", "outside loop")
1378
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001379 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001380 self._check_error("if 0: yield", "outside function")
1381 self._check_error("if 0: yield\nelse: x=1", "outside function")
1382 self._check_error("if 1: pass\nelse: yield", "outside function")
1383 self._check_error("while 0: yield", "outside function")
1384 self._check_error("while 0: yield\nelse: x=1", "outside function")
1385 self._check_error("class C:\n if 0: yield", "outside function")
1386 self._check_error("class C:\n if 1: pass\n else: yield",
1387 "outside function")
1388 self._check_error("class C:\n while 0: yield", "outside function")
1389 self._check_error("class C:\n while 0: yield\n else: x = 1",
1390 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001391
1392 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001393 self._check_error("if 0: return", "outside function")
1394 self._check_error("if 0: return\nelse: x=1", "outside function")
1395 self._check_error("if 1: pass\nelse: return", "outside function")
1396 self._check_error("while 0: return", "outside function")
1397 self._check_error("class C:\n if 0: return", "outside function")
1398 self._check_error("class C:\n while 0: return", "outside function")
1399 self._check_error("class C:\n while 0: return\n else: x=1",
1400 "outside function")
1401 self._check_error("class C:\n if 0: return\n else: x= 1",
1402 "outside function")
1403 self._check_error("class C:\n if 1: pass\n else: return",
1404 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001405
1406 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001407 self._check_error("if 0: break", "outside loop")
1408 self._check_error("if 0: break\nelse: x=1", "outside loop")
1409 self._check_error("if 1: pass\nelse: break", "outside loop")
1410 self._check_error("class C:\n if 0: break", "outside loop")
1411 self._check_error("class C:\n if 1: pass\n else: break",
1412 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001413
1414 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001415 self._check_error("if 0: continue", "not properly in loop")
1416 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1417 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1418 self._check_error("class C:\n if 0: continue", "not properly in loop")
1419 self._check_error("class C:\n if 1: pass\n else: continue",
1420 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001421
Thomas Wouters477c8d52006-05-27 19:21:47 +00001422 def test_unexpected_indent(self):
1423 self._check_error("foo()\n bar()\n", "unexpected indent",
1424 subclass=IndentationError)
1425
1426 def test_no_indent(self):
1427 self._check_error("if 1:\nfoo()", "expected an indented block",
1428 subclass=IndentationError)
1429
1430 def test_bad_outdent(self):
1431 self._check_error("if 1:\n foo()\n bar()",
1432 "unindent does not match .* level",
1433 subclass=IndentationError)
1434
1435 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001436 self._check_error("int(base=10, '2')",
1437 "positional argument follows keyword argument")
1438
1439 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001440 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001441 "positional argument follows "
1442 "keyword argument unpacking")
1443
1444 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001445 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001446 "iterable argument unpacking follows "
1447 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001448
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001449 def test_empty_line_after_linecont(self):
1450 # See issue-40847
1451 s = r"""\
1452pass
1453 \
1454
1455pass
1456"""
1457 try:
1458 compile(s, '<string>', 'exec')
1459 except SyntaxError:
1460 self.fail("Empty line after a line continuation character is valid.")
1461
Mark Shannon02d126a2020-09-25 14:04:19 +01001462 @support.cpython_only
1463 def test_nested_named_except_blocks(self):
1464 code = ""
1465 for i in range(12):
1466 code += f"{' '*i}try:\n"
1467 code += f"{' '*(i+1)}raise Exception\n"
1468 code += f"{' '*i}except Exception as e:\n"
1469 code += f"{' '*4*12}pass"
1470 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001471
Pablo Galindo06f8c332020-10-30 23:48:42 +00001472 def test_barry_as_flufl_with_syntax_errors(self):
1473 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
1474 # is reading the wrong token in the presence of syntax errors later
1475 # in the file. See bpo-42214 for more information.
1476 code = """
1477def func1():
1478 if a != b:
1479 raise ValueError
1480
1481def func2():
1482 try
1483 return 1
1484 finally:
1485 pass
1486"""
Pablo Galindo58fb1562021-02-02 19:54:22 +00001487 self._check_error(code, "expected ':'")
Pablo Galindo06f8c332020-10-30 23:48:42 +00001488
Pablo Galindo96eeff52021-03-22 17:28:11 +00001489 def test_invalid_line_continuation_error_position(self):
1490 self._check_error(r"a = 3 \ 4",
1491 "unexpected character after line continuation character",
1492 lineno=1, offset=9)
1493
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +02001494 def test_invalid_line_continuation_left_recursive(self):
1495 # Check bpo-42218: SyntaxErrors following left-recursive rules
1496 # (t_primary_raw in this case) need to be tested explicitly
1497 self._check_error("A.\u018a\\ ",
1498 "unexpected character after line continuation character")
1499 self._check_error("A.\u03bc\\\n",
1500 "unexpected EOF while parsing")
1501
Pablo Galindod6d63712021-01-19 23:59:33 +00001502 def test_error_parenthesis(self):
1503 for paren in "([{":
1504 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
1505
1506 for paren in ")]}":
1507 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
1508
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001509 def test_match_call_does_not_raise_syntax_error(self):
1510 code = """
1511def match(x):
1512 return 1+1
1513
1514match(34)
1515"""
1516 compile(code, "<string>", "exec")
1517
1518 def test_case_call_does_not_raise_syntax_error(self):
1519 code = """
1520def case(x):
1521 return 1+1
1522
1523case(34)
1524"""
1525 compile(code, "<string>", "exec")
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -07001526
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07001527 def test_multiline_compiler_error_points_to_the_end(self):
1528 self._check_error(
1529 "call(\na=1,\na=1\n)",
1530 "keyword argument repeated",
1531 lineno=3
1532 )
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001533
Pablo Galindod6d63712021-01-19 23:59:33 +00001534
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001535def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001536 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001537 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001538 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001539
1540if __name__ == "__main__":
1541 test_main()