blob: c77aafeb363613886d545ff4caccd9bef80f6a1d [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 Galindo Salgadob1bd16c2021-08-05 19:00:19 +0100155>>> if True:
156... print("Hello"
157...
158... if 2:
159... print(123))
160Traceback (most recent call last):
161SyntaxError: invalid syntax
162
Pablo Galindo9f495902020-06-08 02:57:00 +0100163>>> True = True = 3
164Traceback (most recent call last):
165SyntaxError: cannot assign to True
166
167>>> x = y = True = z = 3
168Traceback (most recent call last):
169SyntaxError: cannot assign to True
170
171>>> x = y = yield = 1
172Traceback (most recent call last):
173SyntaxError: assignment to yield expression not possible
174
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300175>>> a, b += 1, 2
176Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100177SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300178
179>>> (a, b) += 1, 2
180Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100181SyntaxError: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300182
183>>> [a, b] += 1, 2
184Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100185SyntaxError: 'list' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300186
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300187Invalid targets in `for` loops and `with` statements should also
188produce a specialized error message
189
190>>> for a() in b: pass
191Traceback (most recent call last):
192SyntaxError: cannot assign to function call
193
194>>> for (a, b()) in b: pass
195Traceback (most recent call last):
196SyntaxError: cannot assign to function call
197
198>>> for [a, b()] in b: pass
199Traceback (most recent call last):
200SyntaxError: cannot assign to function call
201
202>>> for (*a, b, c+1) in b: pass
203Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100204SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300205
206>>> for (x, *(y, z.d())) in b: pass
207Traceback (most recent call last):
208SyntaxError: cannot assign to function call
209
210>>> for a, b() in c: pass
211Traceback (most recent call last):
212SyntaxError: cannot assign to function call
213
214>>> for a, b, (c + 1, d()): pass
215Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100216SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300217
218>>> for i < (): pass
219Traceback (most recent call last):
220SyntaxError: invalid syntax
221
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300222>>> for a, b
223Traceback (most recent call last):
224SyntaxError: invalid syntax
225
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300226>>> with a as b(): pass
227Traceback (most recent call last):
228SyntaxError: cannot assign to function call
229
230>>> with a as (b, c()): pass
231Traceback (most recent call last):
232SyntaxError: cannot assign to function call
233
234>>> with a as [b, c()]: pass
235Traceback (most recent call last):
236SyntaxError: cannot assign to function call
237
238>>> with a as (*b, c, d+1): pass
239Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100240SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300241
242>>> with a as (x, *(y, z.d())): pass
243Traceback (most recent call last):
244SyntaxError: cannot assign to function call
245
246>>> with a as b, c as d(): pass
247Traceback (most recent call last):
248SyntaxError: cannot assign to function call
249
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300250>>> with a as b
251Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000252SyntaxError: expected ':'
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300253
Pablo Galindo9f495902020-06-08 02:57:00 +0100254>>> p = p =
255Traceback (most recent call last):
256SyntaxError: invalid syntax
257
Pablo Galindo835f14f2021-01-31 22:52:56 +0000258Comprehensions creating tuples without parentheses
259should produce a specialized error message:
260
261>>> [x,y for x,y in range(100)]
262Traceback (most recent call last):
263SyntaxError: did you forget parentheses around the comprehension target?
264
265>>> {x,y for x,y in range(100)}
266Traceback (most recent call last):
267SyntaxError: did you forget parentheses around the comprehension target?
268
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000269# Missing commas in literals collections should not
270# produce special error messages regarding missing
Pablo Galindob2802482021-04-15 21:38:45 +0100271# parentheses, but about missing commas instead
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000272
273>>> [1, 2 3]
Pablo Galindo835f14f2021-01-31 22:52:56 +0000274Traceback (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?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000280
281>>> {1:2, 2:5 3:12}
282Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100283SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000284
285>>> (1, 2 3)
286Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100287SyntaxError: invalid syntax. Perhaps you forgot a comma?
288
289# Make sure soft keywords constructs don't raise specialized
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700290# errors regarding missing commas or other spezialiced errors
Pablo Galindob2802482021-04-15 21:38:45 +0100291
292>>> match x:
293... y = 3
294Traceback (most recent call last):
295SyntaxError: invalid syntax
296
297>>> match x:
298... case y:
299... 3 $ 3
300Traceback (most recent call last):
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000301SyntaxError: invalid syntax
Pablo Galindo835f14f2021-01-31 22:52:56 +0000302
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700303>>> match x:
304... case $:
305... ...
306Traceback (most recent call last):
307SyntaxError: invalid syntax
308
309>>> match ...:
310... case {**rest, "key": value}:
311... ...
312Traceback (most recent call last):
313SyntaxError: invalid syntax
314
315>>> match ...:
316... case {**_}:
317... ...
318Traceback (most recent call last):
319SyntaxError: invalid syntax
320
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000321From compiler_complex_args():
322
323>>> def f(None=1):
324... pass
325Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000326SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000327
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000328From ast_for_arguments():
329
330>>> def f(x, y=1, z):
331... pass
332Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000333SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000334
335>>> def f(x, None):
336... pass
337Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000338SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000339
340>>> def f(*None):
341... pass
342Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000343SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000344
345>>> def f(**None):
346... pass
347Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000348SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000349
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300350>>> import ast; ast.parse('''
351... def f(
352... *, # type: int
353... a, # type: int
354... ):
355... pass
356... ''', type_comments=True)
357Traceback (most recent call last):
358SyntaxError: bare * has associated type comment
359
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000360
361From ast_for_funcdef():
362
363>>> def None(x):
364... pass
365Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000366SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000367
368
369From ast_for_call():
370
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200371>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000372... return list(it)
373>>> L = range(10)
374>>> f(x for x in L)
375[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
376>>> f(x for x in L, 1)
377Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200378SyntaxError: Generator expression must be parenthesized
379>>> f(x for x in L, y=1)
380Traceback (most recent call last):
381SyntaxError: Generator expression must be parenthesized
382>>> f(x for x in L, *[])
383Traceback (most recent call last):
384SyntaxError: Generator expression must be parenthesized
385>>> f(x for x in L, **{})
386Traceback (most recent call last):
387SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300388>>> f(L, x for x in L)
389Traceback (most recent call last):
390SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400391>>> f(x for x in L, y for y in L)
392Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200393SyntaxError: Generator expression must be parenthesized
394>>> f(x for x in L,)
395Traceback (most recent call last):
396SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000397>>> f((x for x in L), 1)
398[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200399>>> class C(x for x in L):
400... pass
401Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000402SyntaxError: expected ':'
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000403
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200404>>> def g(*args, **kwargs):
405... print(args, sorted(kwargs.items()))
406>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
407... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
408... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
409... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
410... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
411... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
412... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
413... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
414... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
415... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
416... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
417... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
418... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
419... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
420... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
421... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
422... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
423... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
424... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
425... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
426(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000427
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200428>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
429... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
430... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
431... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
432... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
433... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
434... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
435... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
436... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
437... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
438... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
439... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
440... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
441... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
442... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
443... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
444... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
445... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
446... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
447... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
448... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
449... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
450... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
451... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
452... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
453... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
454... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
455... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
456... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
457... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
458... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
459... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
460... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
461... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
462... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
463... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
464... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
465... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
466... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
467... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
468... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
469... # doctest: +ELLIPSIS
470() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000471
Yury Selivanovf2392132016-12-13 19:03:51 -0500472>>> class C:
473... def meth(self, *args):
474... return args
475>>> obj = C()
476>>> obj.meth(
477... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
478... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
479... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
480... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
481... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
482... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
483... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
484... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
485... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
486... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
487... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
488... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
489... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
490... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
491... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
492... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
493... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
494... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
495... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
496... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
497(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
498
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700499>>> f(lambda x: x[0] = 3)
500Traceback (most recent call last):
501SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
502
503# Check that this error doesn't trigger for names:
504>>> f(a={x: for x in {}})
505Traceback (most recent call last):
506SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000507
508The grammar accepts any test (basically, any expression) in the
509keyword slot of a call site. Test a few different options.
510
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700511>>> f(x()=2)
512Traceback (most recent call last):
513SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
514>>> f(a or b=1)
515Traceback (most recent call last):
516SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
517>>> f(x.y=1)
518Traceback (most recent call last):
519SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
520>>> f((x)=2)
521Traceback (most recent call last):
522SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
523>>> f(True=2)
524Traceback (most recent call last):
525SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200526>>> f(__debug__=1)
527Traceback (most recent call last):
528SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100529>>> __debug__: int
530Traceback (most recent call last):
531SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000532
533
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000534More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000535
536>>> (x for x in x) += 1
537Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100538SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000539>>> None += 1
540Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100541SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200542>>> __debug__ += 1
543Traceback (most recent call last):
544SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000545>>> f() += 1
546Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100547SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000548
549
550Test continue in finally in weird combinations.
551
Ezio Melotti13925002011-03-16 11:05:33 +0200552continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000553
554 >>> def test():
555 ... try:
556 ... pass
557 ... finally:
558 ... for abc in range(10):
559 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000560 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000561 >>> test()
562 9
563
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200564continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565
566 >>> def test():
567 ... for abc in range(10):
568 ... try:
569 ... pass
570 ... finally:
571 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200572 ... print(abc)
573 >>> test()
574 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000575
576 >>> def test():
577 ... for abc in range(10):
578 ... try:
579 ... pass
580 ... finally:
581 ... try:
582 ... continue
583 ... except:
584 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200585 ... print(abc)
586 >>> test()
587 9
588
589 >>> def test():
590 ... for abc in range(10):
591 ... try:
592 ... pass
593 ... finally:
594 ... try:
595 ... pass
596 ... except:
597 ... continue
598 ... print(abc)
599 >>> test()
600 9
601
602A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000603
604 >>> def foo():
605 ... try:
606 ... pass
607 ... finally:
608 ... continue
609 Traceback (most recent call last):
610 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200611 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000612
613There is one test for a break that is not in a loop. The compiler
614uses a single data structure to keep track of try-finally and loops,
615so we need to be sure that a break is actually inside a loop. If it
616isn't, there should be a syntax error.
617
618 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000619 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000620 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000621 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000622 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000623 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000624 Traceback (most recent call last):
625 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000626 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000627
Benjamin Petersone09ed542016-07-14 22:00:03 -0700628This raises a SyntaxError, it used to raise a SystemError.
629Context for this change can be found on issue #27514
630
Thomas Wouters89f507f2006-12-13 04:49:30 +0000631In 2.5 there was a missing exception and an assert was triggered in a debug
632build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
633
634 >>> while 1:
635 ... while 2:
636 ... while 3:
637 ... while 4:
638 ... while 5:
639 ... while 6:
640 ... while 8:
641 ... while 9:
642 ... while 10:
643 ... while 11:
644 ... while 12:
645 ... while 13:
646 ... while 14:
647 ... while 15:
648 ... while 16:
649 ... while 17:
650 ... while 18:
651 ... while 19:
652 ... while 20:
653 ... while 21:
654 ... while 22:
655 ... break
656 Traceback (most recent call last):
657 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700658 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000659
Guido van Rossum6cff8742016-09-09 09:36:26 -0700660Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
661
662 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200663 ... print(x)
664 ... global x
665 Traceback (most recent call last):
666 ...
667 SyntaxError: name 'x' is used prior to global declaration
668
669 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700670 ... x = 1
671 ... global x
672 Traceback (most recent call last):
673 ...
674 SyntaxError: name 'x' is assigned to before global declaration
675
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200676 >>> def f(x):
677 ... global x
678 Traceback (most recent call last):
679 ...
680 SyntaxError: name 'x' is parameter and global
681
Guido van Rossum6cff8742016-09-09 09:36:26 -0700682 >>> def f():
683 ... x = 1
684 ... def g():
685 ... print(x)
686 ... nonlocal x
687 Traceback (most recent call last):
688 ...
689 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000690
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300691 >>> def f():
692 ... x = 1
693 ... def g():
694 ... x = 2
695 ... nonlocal x
696 Traceback (most recent call last):
697 ...
698 SyntaxError: name 'x' is assigned to before nonlocal declaration
699
Jeremy Hylton81e95022007-02-27 06:50:52 +0000700 >>> def f(x):
701 ... nonlocal x
702 Traceback (most recent call last):
703 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000704 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000705
Jeremy Hylton81e95022007-02-27 06:50:52 +0000706 >>> def f():
707 ... global x
708 ... nonlocal x
709 Traceback (most recent call last):
710 ...
711 SyntaxError: name 'x' is nonlocal and global
712
713 >>> def f():
714 ... nonlocal x
715 Traceback (most recent call last):
716 ...
717 SyntaxError: no binding for nonlocal 'x' found
718
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000719From SF bug #1705365
720 >>> nonlocal x
721 Traceback (most recent call last):
722 ...
723 SyntaxError: nonlocal declaration not allowed at module level
724
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300725From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600726 >>> class A:
727 ... def f(self):
728 ... nonlocal __x
729 Traceback (most recent call last):
730 ...
731 SyntaxError: no binding for nonlocal '_A__x' found
732
Guido van Rossumd8faa362007-04-27 19:54:29 +0000733
734This tests assignment-context; there was a bug in Python 2.5 where compiling
735a complex 'if' (one with 'elif') would fail to notice an invalid suite,
736leading to spurious errors.
737
738 >>> if 1:
739 ... x() = 1
740 ... elif 1:
741 ... pass
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 ... pass
748 ... elif 1:
749 ... x() = 1
750 Traceback (most recent call last):
751 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100752 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000753
754 >>> if 1:
755 ... x() = 1
756 ... elif 1:
757 ... pass
758 ... else:
759 ... pass
760 Traceback (most recent call last):
761 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100762 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000763
764 >>> if 1:
765 ... pass
766 ... elif 1:
767 ... x() = 1
768 ... else:
769 ... pass
770 Traceback (most recent call last):
771 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100772 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000773
774 >>> if 1:
775 ... pass
776 ... elif 1:
777 ... pass
778 ... else:
779 ... x() = 1
780 Traceback (most recent call last):
781 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100782 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hylton81e95022007-02-27 06:50:52 +0000783
Pablo Galindo58fb1562021-02-02 19:54:22 +0000784 Missing ':' before suites:
785
786 >>> def f()
787 ... pass
788 Traceback (most recent call last):
789 SyntaxError: expected ':'
790
791 >>> class A
792 ... pass
793 Traceback (most recent call last):
794 SyntaxError: expected ':'
795
796 >>> if 1
797 ... pass
798 ... elif 1:
799 ... pass
800 ... else:
801 ... x() = 1
802 Traceback (most recent call last):
803 SyntaxError: expected ':'
804
805 >>> if 1:
806 ... pass
807 ... elif 1
808 ... pass
809 ... else:
810 ... x() = 1
811 Traceback (most recent call last):
812 SyntaxError: expected ':'
813
814 >>> if 1:
815 ... pass
816 ... elif 1:
817 ... pass
818 ... else
819 ... x() = 1
820 Traceback (most recent call last):
821 SyntaxError: expected ':'
822
823 >>> for x in range(10)
824 ... pass
825 Traceback (most recent call last):
826 SyntaxError: expected ':'
827
828 >>> while True
829 ... pass
830 Traceback (most recent call last):
831 SyntaxError: expected ':'
832
833 >>> with blech as something
834 ... pass
835 Traceback (most recent call last):
836 SyntaxError: expected ':'
837
838 >>> with blech
839 ... pass
840 Traceback (most recent call last):
841 SyntaxError: expected ':'
842
843 >>> with blech, block as something
844 ... pass
845 Traceback (most recent call last):
846 SyntaxError: expected ':'
847
848 >>> with blech, block as something, bluch
849 ... pass
850 Traceback (most recent call last):
851 SyntaxError: expected ':'
852
853 >>> with (blech as something)
854 ... pass
855 Traceback (most recent call last):
856 SyntaxError: expected ':'
857
858 >>> with (blech)
859 ... pass
860 Traceback (most recent call last):
861 SyntaxError: expected ':'
862
863 >>> with (blech, block as something)
864 ... pass
865 Traceback (most recent call last):
866 SyntaxError: expected ':'
867
868 >>> with (blech, block as something, bluch)
869 ... pass
870 Traceback (most recent call last):
871 SyntaxError: expected ':'
872
873 >>> try
874 ... pass
875 Traceback (most recent call last):
876 SyntaxError: expected ':'
877
878 >>> try:
879 ... pass
880 ... except
881 ... pass
882 Traceback (most recent call last):
883 SyntaxError: expected ':'
884
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000885 >>> match x
886 ... case list():
887 ... pass
888 Traceback (most recent call last):
889 SyntaxError: expected ':'
890
891 >>> match x:
892 ... case list()
893 ... pass
894 Traceback (most recent call last):
895 SyntaxError: expected ':'
896
897 >>> match x:
898 ... case [y] if y > 0
899 ... pass
900 Traceback (most recent call last):
901 SyntaxError: expected ':'
902
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100903 >>> if x = 3:
904 ... pass
905 Traceback (most recent call last):
906 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
907
908 >>> while x = 3:
909 ... pass
910 Traceback (most recent call last):
911 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
912
913 >>> if x.a = 3:
914 ... pass
915 Traceback (most recent call last):
916 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
917
918 >>> while x.a = 3:
919 ... pass
920 Traceback (most recent call last):
921 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
922
Pablo Galindoe53f72a2021-06-04 00:11:43 +0100923Custom error messages for try blocks that are not followed by except/finally
924
925 >>> try:
926 ... x = 34
927 ...
928 Traceback (most recent call last):
929 SyntaxError: expected 'except' or 'finally' block
930
Pablo Galindod9151cb2021-04-13 02:32:33 +0100931Ensure that early = are not matched by the parser as invalid comparisons
Pablo Galindob2802482021-04-15 21:38:45 +0100932 >>> f(2, 4, x=34); 1 $ 2
Pablo Galindod9151cb2021-04-13 02:32:33 +0100933 Traceback (most recent call last):
934 SyntaxError: invalid syntax
935
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100936 >>> dict(x=34); x $ y
937 Traceback (most recent call last):
938 SyntaxError: invalid syntax
939
940 >>> dict(x=34, (x for x in range 10), 1); x $ y
941 Traceback (most recent call last):
942 SyntaxError: invalid syntax
943
944 >>> dict(x=34, x=1, y=2); x $ y
945 Traceback (most recent call last):
946 SyntaxError: invalid syntax
947
Pablo Galindoda743502021-04-15 14:06:39 +0100948Incomplete dictionary literals
949
950 >>> {1:2, 3:4, 5}
951 Traceback (most recent call last):
952 SyntaxError: ':' expected after dictionary key
953
954 >>> {1:2, 3:4, 5:}
955 Traceback (most recent call last):
956 SyntaxError: expression expected after dictionary key and ':'
957
958 >>> {1: *12+1, 23: 1}
959 Traceback (most recent call last):
960 SyntaxError: cannot use a starred expression in a dictionary value
961
962 >>> {1: *12+1}
963 Traceback (most recent call last):
964 SyntaxError: cannot use a starred expression in a dictionary value
965
966 >>> {1: 23, 1: *12+1}
967 Traceback (most recent call last):
968 SyntaxError: cannot use a starred expression in a dictionary value
969
970 >>> {1:}
971 Traceback (most recent call last):
972 SyntaxError: expression expected after dictionary key and ':'
973
974 # Ensure that the error is not raise for syntax errors that happen after sets
975
976 >>> {1} $
977 Traceback (most recent call last):
978 SyntaxError: invalid syntax
979
Pablo Galindo56c95df2021-04-21 15:28:21 +0100980Specialized indentation errors:
981
982 >>> while condition:
983 ... pass
984 Traceback (most recent call last):
985 IndentationError: expected an indented block after 'while' statement on line 1
986
987 >>> for x in range(10):
988 ... pass
989 Traceback (most recent call last):
990 IndentationError: expected an indented block after 'for' statement on line 1
991
992 >>> for x in range(10):
993 ... pass
994 ... else:
995 ... pass
996 Traceback (most recent call last):
997 IndentationError: expected an indented block after 'else' statement on line 3
998
999 >>> async for x in range(10):
1000 ... pass
1001 Traceback (most recent call last):
1002 IndentationError: expected an indented block after 'for' statement on line 1
1003
1004 >>> async for x in range(10):
1005 ... pass
1006 ... else:
1007 ... pass
1008 Traceback (most recent call last):
1009 IndentationError: expected an indented block after 'else' statement on line 3
1010
1011 >>> if something:
1012 ... pass
1013 Traceback (most recent call last):
1014 IndentationError: expected an indented block after 'if' statement on line 1
1015
1016 >>> if something:
1017 ... pass
1018 ... elif something_else:
1019 ... pass
1020 Traceback (most recent call last):
1021 IndentationError: expected an indented block after 'elif' statement on line 3
1022
1023 >>> if something:
1024 ... pass
1025 ... elif something_else:
1026 ... pass
1027 ... else:
1028 ... pass
1029 Traceback (most recent call last):
1030 IndentationError: expected an indented block after 'else' statement on line 5
1031
1032 >>> try:
1033 ... pass
1034 Traceback (most recent call last):
1035 IndentationError: expected an indented block after 'try' statement on line 1
1036
1037 >>> try:
1038 ... something()
1039 ... except A:
1040 ... pass
1041 Traceback (most recent call last):
1042 IndentationError: expected an indented block after 'except' statement on line 3
1043
1044 >>> try:
1045 ... something()
1046 ... except A:
1047 ... pass
1048 ... finally:
1049 ... pass
1050 Traceback (most recent call last):
1051 IndentationError: expected an indented block after 'finally' statement on line 5
1052
1053 >>> with A:
1054 ... pass
1055 Traceback (most recent call last):
1056 IndentationError: expected an indented block after 'with' statement on line 1
1057
1058 >>> with A as a, B as b:
1059 ... pass
1060 Traceback (most recent call last):
1061 IndentationError: expected an indented block after 'with' statement on line 1
1062
1063 >>> with (A as a, B as b):
1064 ... pass
1065 Traceback (most recent call last):
1066 IndentationError: expected an indented block after 'with' statement on line 1
1067
1068 >>> async with A:
1069 ... pass
1070 Traceback (most recent call last):
1071 IndentationError: expected an indented block after 'with' statement on line 1
1072
1073 >>> async with A as a, B as b:
1074 ... pass
1075 Traceback (most recent call last):
1076 IndentationError: expected an indented block after 'with' statement on line 1
1077
1078 >>> async with (A as a, B as b):
1079 ... pass
1080 Traceback (most recent call last):
1081 IndentationError: expected an indented block after 'with' statement on line 1
1082
1083 >>> def foo(x, /, y, *, z=2):
1084 ... pass
1085 Traceback (most recent call last):
1086 IndentationError: expected an indented block after function definition on line 1
1087
1088 >>> class Blech(A):
1089 ... pass
1090 Traceback (most recent call last):
1091 IndentationError: expected an indented block after class definition on line 1
1092
1093 >>> match something:
1094 ... pass
1095 Traceback (most recent call last):
1096 IndentationError: expected an indented block after 'match' statement on line 1
1097
1098 >>> match something:
1099 ... case []:
1100 ... pass
1101 Traceback (most recent call last):
1102 IndentationError: expected an indented block after 'case' statement on line 2
1103
1104 >>> match something:
1105 ... case []:
1106 ... ...
1107 ... case {}:
1108 ... pass
1109 Traceback (most recent call last):
1110 IndentationError: expected an indented block after 'case' statement on line 4
1111
Collin Winter828f04a2007-08-31 00:04:24 +00001112Make sure that the old "raise X, Y[, Z]" form is gone:
1113 >>> raise X, Y
1114 Traceback (most recent call last):
1115 ...
1116 SyntaxError: invalid syntax
1117 >>> raise X, Y, Z
1118 Traceback (most recent call last):
1119 ...
1120 SyntaxError: invalid syntax
1121
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001122Check that an multiple exception types with missing parentheses
Pablo Galindo206cbda2021-02-07 18:42:21 +00001123raise a custom exception
1124
1125 >>> try:
1126 ... pass
1127 ... except A, B:
1128 ... pass
1129 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001130 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001131
1132 >>> try:
1133 ... pass
1134 ... except A, B, C:
1135 ... pass
1136 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001137 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001138
1139 >>> try:
1140 ... pass
1141 ... except A, B, C as blech:
1142 ... pass
1143 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001144 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001145
1146 >>> try:
1147 ... pass
1148 ... except A, B, C as blech:
1149 ... pass
1150 ... finally:
1151 ... pass
1152 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001153 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001154
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001155
1156>>> f(a=23, a=234)
1157Traceback (most recent call last):
1158 ...
Pablo Galindo254ec782020-04-03 20:37:13 +01001159SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001160
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001161>>> {1, 2, 3} = 42
1162Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001163SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001164
1165>>> {1: 2, 3: 4} = 42
1166Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001167SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001168
1169>>> f'{x}' = 42
1170Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001171SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001172
1173>>> f'{x}-{y}' = 42
1174Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001175SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001176
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -07001177>>> (x, y, z=3, d, e)
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
1189>>> {x, y, z=3, d, e}
1190Traceback (most recent call last):
1191SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1192
1193>>> {z=3}
1194Traceback (most recent call last):
1195SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1196
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +03001197>>> from t import x,
1198Traceback (most recent call last):
1199SyntaxError: trailing comma not allowed without surrounding parentheses
1200
1201>>> from t import x,y,
1202Traceback (most recent call last):
1203SyntaxError: trailing comma not allowed without surrounding parentheses
1204
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +03001205>>> (): int
1206Traceback (most recent call last):
1207SyntaxError: only single target (not tuple) can be annotated
1208>>> []: int
1209Traceback (most recent call last):
1210SyntaxError: only single target (not list) can be annotated
1211>>> (()): int
1212Traceback (most recent call last):
1213SyntaxError: only single target (not tuple) can be annotated
1214>>> ([]): int
1215Traceback (most recent call last):
1216SyntaxError: only single target (not list) can be annotated
1217
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001218Corner-cases that used to fail to raise the correct error:
1219
1220 >>> def f(*, x=lambda __debug__:0): pass
1221 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001222 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001223
Pablo Galindob0544ba2021-04-21 12:41:19 +01001224 >>> def f(*args:(lambda __debug__:0)): pass
1225 Traceback (most recent call last):
1226 SyntaxError: cannot assign to __debug__
1227
1228 >>> def f(**kwargs:(lambda __debug__:0)): pass
1229 Traceback (most recent call last):
1230 SyntaxError: cannot assign to __debug__
1231
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +03001232 >>> with (lambda *:0): pass
1233 Traceback (most recent call last):
1234 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001235
1236Corner-cases that used to crash:
1237
1238 >>> def f(**__debug__): pass
1239 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001240 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001241
1242 >>> def f(*xx, __debug__): pass
1243 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001244 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001245
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +03001246 >>> import ä £
1247 Traceback (most recent call last):
1248 SyntaxError: invalid character '£' (U+00A3)
Pablo Galindoa8c418d2021-06-18 22:15:57 +01001249
1250 Invalid pattern matching constructs:
1251
1252 >>> match ...:
1253 ... case 42 as _:
1254 ... ...
1255 Traceback (most recent call last):
1256 SyntaxError: cannot use '_' as a target
1257
1258 >>> match ...:
1259 ... case 42 as 1+2+4:
1260 ... ...
1261 Traceback (most recent call last):
1262 SyntaxError: invalid pattern target
Miss Islington (bot)11f1a302021-06-24 08:34:28 -07001263
1264 >>> match ...:
1265 ... case Foo(z=1, y=2, x):
1266 ... ...
1267 Traceback (most recent call last):
1268 SyntaxError: positional patterns follow keyword patterns
1269
1270 >>> match ...:
1271 ... case Foo(a, z=1, y=2, x):
1272 ... ...
1273 Traceback (most recent call last):
1274 SyntaxError: positional patterns follow keyword patterns
1275
1276 >>> match ...:
1277 ... case Foo(z=1, x, y=2):
1278 ... ...
1279 Traceback (most recent call last):
1280 SyntaxError: positional patterns follow keyword patterns
1281
1282 >>> match ...:
1283 ... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
1284 ... ...
1285 Traceback (most recent call last):
1286 SyntaxError: positional patterns follow keyword patterns
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001287"""
1288
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001289import re
1290import unittest
1291
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001292from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001293
1294class SyntaxTestCase(unittest.TestCase):
1295
1296 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001297 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001298 """Check that compiling code raises SyntaxError with errtext.
1299
1300 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001301 test of the exception raised. If subclass is specified it
1302 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001303 """
1304 try:
1305 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +00001306 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001307 if subclass and not isinstance(err, subclass):
1308 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001309 mo = re.search(errtext, str(err))
1310 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -07001311 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001312 self.assertEqual(err.filename, filename)
1313 if lineno is not None:
1314 self.assertEqual(err.lineno, lineno)
1315 if offset is not None:
1316 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001317 else:
1318 self.fail("compile() did not raise SyntaxError")
1319
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001320 def test_expression_with_assignment(self):
1321 self._check_error(
1322 "print(end1 + end2 = ' ')",
Pablo Galindo30ed93b2021-04-13 17:51:21 +01001323 'expression cannot contain assignment, perhaps you meant "=="?',
Pablo Galindoa77aac42021-04-23 14:27:05 +01001324 offset=7
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001325 )
1326
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001327 def test_curly_brace_after_primary_raises_immediately(self):
Pablo Galindo Salgadob977f852021-07-27 18:52:32 +01001328 self._check_error("f{}", "invalid syntax", mode="single")
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001329
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001330 def test_assign_call(self):
1331 self._check_error("f() = 1", "assign")
1332
1333 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -07001334 self._check_error("del (,)", "invalid syntax")
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001335 self._check_error("del 1", "cannot delete literal")
1336 self._check_error("del (1, 2)", "cannot delete literal")
1337 self._check_error("del None", "cannot delete None")
1338 self._check_error("del *x", "cannot delete starred")
1339 self._check_error("del (*x)", "cannot use starred expression")
1340 self._check_error("del (*x,)", "cannot delete starred")
1341 self._check_error("del [*x,]", "cannot delete starred")
1342 self._check_error("del f()", "cannot delete function call")
1343 self._check_error("del f(a, b)", "cannot delete function call")
1344 self._check_error("del o.f()", "cannot delete function call")
1345 self._check_error("del a[0]()", "cannot delete function call")
1346 self._check_error("del x, f()", "cannot delete function call")
1347 self._check_error("del f(), x", "cannot delete function call")
1348 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1349 self._check_error("del (a if True else b)", "cannot delete conditional")
1350 self._check_error("del +a", "cannot delete expression")
1351 self._check_error("del a, +b", "cannot delete expression")
1352 self._check_error("del a + b", "cannot delete expression")
1353 self._check_error("del (a + b, c)", "cannot delete expression")
1354 self._check_error("del (c[0], a + b)", "cannot delete expression")
1355 self._check_error("del a.b.c + 2", "cannot delete expression")
1356 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1357 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1358 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1359 self._check_error("del (a := 5)", "cannot delete named expression")
Shantanu27c0d9b2020-05-11 14:53:58 -07001360 # We don't have a special message for this, but make sure we don't
1361 # report "cannot delete name"
1362 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001363
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001364 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001365 source = """if 1:
1366 def error(a):
1367 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001368 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001369 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001370 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001371 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001372 self._check_error(source, "parameter and global", lineno=3)
1373
1374 def test_nonlocal_param_err_first(self):
1375 source = """if 1:
1376 def error(a):
1377 nonlocal a # SyntaxError
1378 def error2():
1379 b = 1
1380 global b # SyntaxError
1381 """
1382 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +00001383
Neal Norwitzfcf44352005-11-27 20:37:43 +00001384 def test_break_outside_loop(self):
1385 self._check_error("break", "outside loop")
1386
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001387 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001388 self._check_error("if 0: yield", "outside function")
1389 self._check_error("if 0: yield\nelse: x=1", "outside function")
1390 self._check_error("if 1: pass\nelse: yield", "outside function")
1391 self._check_error("while 0: yield", "outside function")
1392 self._check_error("while 0: yield\nelse: x=1", "outside function")
1393 self._check_error("class C:\n if 0: yield", "outside function")
1394 self._check_error("class C:\n if 1: pass\n else: yield",
1395 "outside function")
1396 self._check_error("class C:\n while 0: yield", "outside function")
1397 self._check_error("class C:\n while 0: yield\n else: x = 1",
1398 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001399
1400 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001401 self._check_error("if 0: return", "outside function")
1402 self._check_error("if 0: return\nelse: x=1", "outside function")
1403 self._check_error("if 1: pass\nelse: return", "outside function")
1404 self._check_error("while 0: return", "outside function")
1405 self._check_error("class C:\n if 0: return", "outside function")
1406 self._check_error("class C:\n while 0: return", "outside function")
1407 self._check_error("class C:\n while 0: return\n else: x=1",
1408 "outside function")
1409 self._check_error("class C:\n if 0: return\n else: x= 1",
1410 "outside function")
1411 self._check_error("class C:\n if 1: pass\n else: return",
1412 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001413
1414 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001415 self._check_error("if 0: break", "outside loop")
1416 self._check_error("if 0: break\nelse: x=1", "outside loop")
1417 self._check_error("if 1: pass\nelse: break", "outside loop")
1418 self._check_error("class C:\n if 0: break", "outside loop")
1419 self._check_error("class C:\n if 1: pass\n else: break",
1420 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001421
1422 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001423 self._check_error("if 0: continue", "not properly in loop")
1424 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1425 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1426 self._check_error("class C:\n if 0: continue", "not properly in loop")
1427 self._check_error("class C:\n if 1: pass\n else: continue",
1428 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001429
Thomas Wouters477c8d52006-05-27 19:21:47 +00001430 def test_unexpected_indent(self):
1431 self._check_error("foo()\n bar()\n", "unexpected indent",
1432 subclass=IndentationError)
1433
1434 def test_no_indent(self):
1435 self._check_error("if 1:\nfoo()", "expected an indented block",
1436 subclass=IndentationError)
1437
1438 def test_bad_outdent(self):
1439 self._check_error("if 1:\n foo()\n bar()",
1440 "unindent does not match .* level",
1441 subclass=IndentationError)
1442
1443 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001444 self._check_error("int(base=10, '2')",
1445 "positional argument follows keyword argument")
1446
1447 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001448 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001449 "positional argument follows "
1450 "keyword argument unpacking")
1451
1452 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001453 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001454 "iterable argument unpacking follows "
1455 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001456
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001457 def test_empty_line_after_linecont(self):
1458 # See issue-40847
1459 s = r"""\
1460pass
1461 \
1462
1463pass
1464"""
1465 try:
1466 compile(s, '<string>', 'exec')
1467 except SyntaxError:
1468 self.fail("Empty line after a line continuation character is valid.")
1469
Mark Shannon02d126a2020-09-25 14:04:19 +01001470 @support.cpython_only
1471 def test_nested_named_except_blocks(self):
1472 code = ""
1473 for i in range(12):
1474 code += f"{' '*i}try:\n"
1475 code += f"{' '*(i+1)}raise Exception\n"
1476 code += f"{' '*i}except Exception as e:\n"
1477 code += f"{' '*4*12}pass"
1478 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001479
Pablo Galindo06f8c332020-10-30 23:48:42 +00001480 def test_barry_as_flufl_with_syntax_errors(self):
1481 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
1482 # is reading the wrong token in the presence of syntax errors later
1483 # in the file. See bpo-42214 for more information.
1484 code = """
1485def func1():
1486 if a != b:
1487 raise ValueError
1488
1489def func2():
1490 try
1491 return 1
1492 finally:
1493 pass
1494"""
Pablo Galindo58fb1562021-02-02 19:54:22 +00001495 self._check_error(code, "expected ':'")
Pablo Galindo06f8c332020-10-30 23:48:42 +00001496
Pablo Galindo96eeff52021-03-22 17:28:11 +00001497 def test_invalid_line_continuation_error_position(self):
1498 self._check_error(r"a = 3 \ 4",
1499 "unexpected character after line continuation character",
1500 lineno=1, offset=9)
1501
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +02001502 def test_invalid_line_continuation_left_recursive(self):
1503 # Check bpo-42218: SyntaxErrors following left-recursive rules
1504 # (t_primary_raw in this case) need to be tested explicitly
1505 self._check_error("A.\u018a\\ ",
1506 "unexpected character after line continuation character")
1507 self._check_error("A.\u03bc\\\n",
1508 "unexpected EOF while parsing")
1509
Pablo Galindod6d63712021-01-19 23:59:33 +00001510 def test_error_parenthesis(self):
1511 for paren in "([{":
1512 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
1513
1514 for paren in ")]}":
1515 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
1516
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001517 def test_match_call_does_not_raise_syntax_error(self):
1518 code = """
1519def match(x):
1520 return 1+1
1521
1522match(34)
1523"""
1524 compile(code, "<string>", "exec")
1525
1526 def test_case_call_does_not_raise_syntax_error(self):
1527 code = """
1528def case(x):
1529 return 1+1
1530
1531case(34)
1532"""
1533 compile(code, "<string>", "exec")
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -07001534
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07001535 def test_multiline_compiler_error_points_to_the_end(self):
1536 self._check_error(
1537 "call(\na=1,\na=1\n)",
1538 "keyword argument repeated",
1539 lineno=3
1540 )
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001541
Pablo Galindod6d63712021-01-19 23:59:33 +00001542
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001543def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001544 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001545 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001546 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001547
1548if __name__ == "__main__":
1549 test_main()