blob: 43780ce273ef4ee6e2cb42c665e200c71e417567 [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
Dong-hee Na32c1caa2021-08-26 09:52:21 +000062>>> del __debug__
63Traceback (most recent call last):
64SyntaxError: cannot delete __debug__
65
Jeremy Hyltonc960f262006-01-27 15:18:39 +000066>>> f() = 1
67Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010068SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000069
Pablo Galindo9f495902020-06-08 02:57:00 +010070>>> yield = 1
71Traceback (most recent call last):
72SyntaxError: assignment to yield expression not possible
73
Shantanu27c0d9b2020-05-11 14:53:58 -070074>>> del f()
75Traceback (most recent call last):
76SyntaxError: cannot delete function call
Jeremy Hyltonc960f262006-01-27 15:18:39 +000077
78>>> a + 1 = 2
79Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010080SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000081
82>>> (x for x in x) = 1
83Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020084SyntaxError: cannot assign to generator expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +000085
86>>> 1 = 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
90>>> "abc" = 1
91Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010092SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Jeremy Hyltonc960f262006-01-27 15:18:39 +000093
Benjamin Petersonbd3e3622011-04-12 18:33:28 -050094>>> b"" = 1
95Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +010096SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +020097
98>>> ... = 1
99Traceback (most recent call last):
Pablo Galindo3283bf42021-06-03 22:22:28 +0100100SyntaxError: cannot assign to ellipsis here. Maybe you meant '==' instead of '='?
Benjamin Petersonbd3e3622011-04-12 18:33:28 -0500101
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000102>>> `1` = 1
103Traceback (most recent call last):
Brett Cannon8933cb42006-08-25 04:12:10 +0000104SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000105
106If the left-hand side of an assignment is a list or tuple, an illegal
107expression inside that contain should still cause a syntax error.
108This test just checks a couple of cases rather than enumerating all of
109them.
110
Pablo Galindo16ab0702020-05-15 02:04:52 +0100111>>> (a, "b", c) = (1, 2, 3)
112Traceback (most recent call last):
113SyntaxError: cannot assign to literal
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200114
Pablo Galindo16ab0702020-05-15 02:04:52 +0100115>>> (a, True, c) = (1, 2, 3)
116Traceback (most recent call last):
117SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200118
119>>> (a, __debug__, c) = (1, 2, 3)
120Traceback (most recent call last):
121SyntaxError: cannot assign to __debug__
122
Pablo Galindo16ab0702020-05-15 02:04:52 +0100123>>> (a, *True, c) = (1, 2, 3)
124Traceback (most recent call last):
125SyntaxError: cannot assign to True
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200126
127>>> (a, *__debug__, c) = (1, 2, 3)
128Traceback (most recent call last):
129SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000130
Pablo Galindo16ab0702020-05-15 02:04:52 +0100131>>> [a, b, 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[1], c + 1] = [1, 2, 3]
136Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100137SyntaxError: cannot assign to expression
Pablo Galindo16ab0702020-05-15 02:04:52 +0100138
139>>> [a, b.c.d, c + 1] = [1, 2, 3]
140Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100141SyntaxError: cannot assign to expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000142
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143>>> a if 1 else b = 1
144Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200145SyntaxError: cannot assign to conditional expression
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000146
Miss Islington (bot)56717622021-08-02 12:05:33 -0700147>>> a = 42 if True
148Traceback (most recent call last):
149SyntaxError: expected 'else' after 'if' expression
150
151>>> a = (42 if True)
152Traceback (most recent call last):
153SyntaxError: expected 'else' after 'if' expression
154
155>>> a = [1, 42 if True, 4]
156Traceback (most recent call last):
157SyntaxError: expected 'else' after 'if' expression
158
Pablo Galindo Salgadob1bd16c2021-08-05 19:00:19 +0100159>>> if True:
160... print("Hello"
161...
162... if 2:
163... print(123))
164Traceback (most recent call last):
165SyntaxError: invalid syntax
166
Pablo Galindo9f495902020-06-08 02:57:00 +0100167>>> True = True = 3
168Traceback (most recent call last):
169SyntaxError: cannot assign to True
170
171>>> x = y = True = z = 3
172Traceback (most recent call last):
173SyntaxError: cannot assign to True
174
175>>> x = y = yield = 1
176Traceback (most recent call last):
177SyntaxError: assignment to yield expression not possible
178
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300179>>> 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: 'tuple' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300186
187>>> [a, b] += 1, 2
188Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100189SyntaxError: 'list' is an illegal expression for augmented assignment
Lysandros Nikolaouce21cfc2020-05-14 23:13:50 +0300190
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300191Invalid targets in `for` loops and `with` statements should also
192produce a specialized error message
193
194>>> for a() 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()] in b: pass
203Traceback (most recent call last):
204SyntaxError: cannot assign to function call
205
206>>> for (*a, b, c+1) in b: 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 (x, *(y, z.d())) in b: pass
211Traceback (most recent call last):
212SyntaxError: cannot assign to function call
213
214>>> for a, b() in c: pass
215Traceback (most recent call last):
216SyntaxError: cannot assign to function call
217
218>>> for a, b, (c + 1, d()): pass
219Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100220SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300221
222>>> for i < (): pass
223Traceback (most recent call last):
224SyntaxError: invalid syntax
225
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300226>>> for a, b
227Traceback (most recent call last):
228SyntaxError: invalid syntax
229
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300230>>> with a as b(): 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()]: pass
239Traceback (most recent call last):
240SyntaxError: cannot assign to function call
241
242>>> with a as (*b, c, d+1): pass
243Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100244SyntaxError: cannot assign to expression
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300245
246>>> with a as (x, *(y, z.d())): pass
247Traceback (most recent call last):
248SyntaxError: cannot assign to function call
249
250>>> with a as b, c as d(): pass
251Traceback (most recent call last):
252SyntaxError: cannot assign to function call
253
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300254>>> with a as b
255Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000256SyntaxError: expected ':'
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300257
Pablo Galindo9f495902020-06-08 02:57:00 +0100258>>> p = p =
259Traceback (most recent call last):
260SyntaxError: invalid syntax
261
Pablo Galindo835f14f2021-01-31 22:52:56 +0000262Comprehensions creating tuples without parentheses
263should produce a specialized error message:
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
269>>> {x,y for x,y in range(100)}
270Traceback (most recent call last):
271SyntaxError: did you forget parentheses around the comprehension target?
272
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000273# Missing commas in literals collections should not
274# produce special error messages regarding missing
Pablo Galindob2802482021-04-15 21:38:45 +0100275# parentheses, but about missing commas instead
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000276
277>>> [1, 2 3]
Pablo Galindo835f14f2021-01-31 22:52:56 +0000278Traceback (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 3}
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, 2:5 3:12}
286Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100287SyntaxError: invalid syntax. Perhaps you forgot a comma?
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000288
289>>> (1, 2 3)
290Traceback (most recent call last):
Pablo Galindob2802482021-04-15 21:38:45 +0100291SyntaxError: invalid syntax. Perhaps you forgot a comma?
292
293# Make sure soft keywords constructs don't raise specialized
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700294# errors regarding missing commas or other spezialiced errors
Pablo Galindob2802482021-04-15 21:38:45 +0100295
296>>> match x:
297... y = 3
298Traceback (most recent call last):
299SyntaxError: invalid syntax
300
301>>> match x:
302... case y:
303... 3 $ 3
304Traceback (most recent call last):
Pablo Galindod4e6ed72021-02-03 23:29:26 +0000305SyntaxError: invalid syntax
Pablo Galindo835f14f2021-01-31 22:52:56 +0000306
Miss Islington (bot)f807a4f2021-06-09 14:45:43 -0700307>>> match x:
308... case $:
309... ...
310Traceback (most recent call last):
311SyntaxError: invalid syntax
312
313>>> match ...:
314... case {**rest, "key": value}:
315... ...
316Traceback (most recent call last):
317SyntaxError: invalid syntax
318
319>>> match ...:
320... case {**_}:
321... ...
322Traceback (most recent call last):
323SyntaxError: invalid syntax
324
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000325From compiler_complex_args():
326
327>>> def f(None=1):
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
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000332From ast_for_arguments():
333
334>>> def f(x, y=1, z):
335... pass
336Traceback (most recent call last):
Guido van Rossum33d26892007-08-05 15:29:28 +0000337SyntaxError: non-default argument follows default argument
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000338
339>>> def f(x, None):
340... pass
341Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000342SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000343
344>>> def f(*None):
345... pass
346Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000347SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000348
349>>> def f(**None):
350... pass
351Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000352SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000353
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300354>>> import ast; ast.parse('''
355... def f(
356... *, # type: int
357... a, # type: int
358... ):
359... pass
360... ''', type_comments=True)
361Traceback (most recent call last):
362SyntaxError: bare * has associated type comment
363
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000364
365From ast_for_funcdef():
366
367>>> def None(x):
368... pass
369Traceback (most recent call last):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000370SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000371
372
373From ast_for_call():
374
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200375>>> def f(it, *varargs, **kwargs):
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000376... return list(it)
377>>> L = range(10)
378>>> f(x for x in L)
379[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
380>>> f(x for x in L, 1)
381Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200382SyntaxError: Generator expression must be parenthesized
383>>> f(x for x in L, y=1)
384Traceback (most recent call last):
385SyntaxError: Generator expression must be parenthesized
386>>> f(x for x in L, *[])
387Traceback (most recent call last):
388SyntaxError: Generator expression must be parenthesized
389>>> f(x for x in L, **{})
390Traceback (most recent call last):
391SyntaxError: Generator expression must be parenthesized
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300392>>> f(L, x for x in L)
393Traceback (most recent call last):
394SyntaxError: Generator expression must be parenthesized
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400395>>> f(x for x in L, y for y in L)
396Traceback (most recent call last):
Serhiy Storchaka9165f772017-11-15 08:49:40 +0200397SyntaxError: Generator expression must be parenthesized
398>>> f(x for x in L,)
399Traceback (most recent call last):
400SyntaxError: Generator expression must be parenthesized
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000401>>> f((x for x in L), 1)
402[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200403>>> class C(x for x in L):
404... pass
405Traceback (most recent call last):
Pablo Galindo58fb1562021-02-02 19:54:22 +0000406SyntaxError: expected ':'
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000407
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200408>>> def g(*args, **kwargs):
409... print(args, sorted(kwargs.items()))
410>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
411... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
412... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
413... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
414... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
415... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
416... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
417... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
418... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
419... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
420... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
421... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
422... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
423... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
424... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
425... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
426... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
427... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
428... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
429... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
430(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) []
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000431
Serhiy Storchaka214678e2016-11-28 10:52:05 +0200432>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8,
433... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16,
434... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24,
435... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32,
436... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40,
437... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48,
438... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56,
439... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64,
440... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72,
441... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80,
442... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88,
443... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96,
444... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103,
445... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110,
446... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117,
447... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124,
448... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131,
449... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138,
450... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145,
451... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152,
452... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159,
453... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166,
454... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173,
455... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180,
456... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187,
457... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194,
458... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201,
459... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208,
460... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215,
461... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222,
462... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229,
463... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236,
464... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243,
465... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250,
466... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257,
467... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264,
468... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271,
469... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278,
470... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285,
471... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292,
472... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299)
473... # doctest: +ELLIPSIS
474() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)]
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000475
Yury Selivanovf2392132016-12-13 19:03:51 -0500476>>> class C:
477... def meth(self, *args):
478... return args
479>>> obj = C()
480>>> obj.meth(
481... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
482... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
483... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
484... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
485... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
486... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
487... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
488... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135,
489... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
490... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163,
491... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,
492... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
493... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205,
494... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
495... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233,
496... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,
497... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261,
498... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275,
499... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289,
500... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS
501(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299)
502
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700503>>> f(lambda x: x[0] = 3)
504Traceback (most recent call last):
505SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
506
507# Check that this error doesn't trigger for names:
508>>> f(a={x: for x in {}})
509Traceback (most recent call last):
510SyntaxError: invalid syntax
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000511
512The grammar accepts any test (basically, any expression) in the
513keyword slot of a call site. Test a few different options.
514
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -0700515>>> f(x()=2)
516Traceback (most recent call last):
517SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
518>>> f(a or b=1)
519Traceback (most recent call last):
520SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
521>>> f(x.y=1)
522Traceback (most recent call last):
523SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
524>>> f((x)=2)
525Traceback (most recent call last):
526SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
527>>> f(True=2)
528Traceback (most recent call last):
529SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200530>>> f(__debug__=1)
531Traceback (most recent call last):
532SyntaxError: cannot assign to __debug__
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100533>>> __debug__: int
534Traceback (most recent call last):
535SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000536
537
Benjamin Peterson87c8d872009-06-11 22:54:11 +0000538More set_context():
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000539
540>>> (x for x in x) += 1
541Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100542SyntaxError: 'generator expression' is an illegal expression for augmented assignment
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000543>>> None += 1
544Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100545SyntaxError: 'None' is an illegal expression for augmented assignment
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200546>>> __debug__ += 1
547Traceback (most recent call last):
548SyntaxError: cannot assign to __debug__
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000549>>> f() += 1
550Traceback (most recent call last):
Pablo Galindo16ab0702020-05-15 02:04:52 +0100551SyntaxError: 'function call' is an illegal expression for augmented assignment
Thomas Wouters89f507f2006-12-13 04:49:30 +0000552
553
554Test continue in finally in weird combinations.
555
Ezio Melotti13925002011-03-16 11:05:33 +0200556continue in for loop under finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000557
558 >>> def test():
559 ... try:
560 ... pass
561 ... finally:
562 ... for abc in range(10):
563 ... continue
Guido van Rossum7131f842007-02-09 20:13:25 +0000564 ... print(abc)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565 >>> test()
566 9
567
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200568continue in a finally should be ok.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000569
570 >>> def test():
571 ... for abc in range(10):
572 ... try:
573 ... pass
574 ... finally:
575 ... continue
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200576 ... print(abc)
577 >>> test()
578 9
Thomas Wouters89f507f2006-12-13 04:49:30 +0000579
580 >>> def test():
581 ... for abc in range(10):
582 ... try:
583 ... pass
584 ... finally:
585 ... try:
586 ... continue
587 ... except:
588 ... pass
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200589 ... print(abc)
590 >>> test()
591 9
592
593 >>> def test():
594 ... for abc in range(10):
595 ... try:
596 ... pass
597 ... finally:
598 ... try:
599 ... pass
600 ... except:
601 ... continue
602 ... print(abc)
603 >>> test()
604 9
605
606A continue outside loop should not be allowed.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000607
608 >>> def foo():
609 ... try:
610 ... pass
611 ... finally:
612 ... continue
613 Traceback (most recent call last):
614 ...
Serhiy Storchakafe2bbb12018-03-18 09:56:52 +0200615 SyntaxError: 'continue' not properly in loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000616
617There is one test for a break that is not in a loop. The compiler
618uses a single data structure to keep track of try-finally and loops,
619so we need to be sure that a break is actually inside a loop. If it
620isn't, there should be a syntax error.
621
622 >>> try:
Guido van Rossum7131f842007-02-09 20:13:25 +0000623 ... print(1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000624 ... break
Guido van Rossum7131f842007-02-09 20:13:25 +0000625 ... print(2)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000626 ... finally:
Guido van Rossum7131f842007-02-09 20:13:25 +0000627 ... print(3)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000628 Traceback (most recent call last):
629 ...
Guido van Rossum33d26892007-08-05 15:29:28 +0000630 SyntaxError: 'break' outside loop
Thomas Wouters89f507f2006-12-13 04:49:30 +0000631
Benjamin Petersone09ed542016-07-14 22:00:03 -0700632This raises a SyntaxError, it used to raise a SystemError.
633Context for this change can be found on issue #27514
634
Thomas Wouters89f507f2006-12-13 04:49:30 +0000635In 2.5 there was a missing exception and an assert was triggered in a debug
636build. The number of blocks must be greater than CO_MAXBLOCKS. SF #1565514
637
638 >>> while 1:
639 ... while 2:
640 ... while 3:
641 ... while 4:
642 ... while 5:
643 ... while 6:
644 ... while 8:
645 ... while 9:
646 ... while 10:
647 ... while 11:
648 ... while 12:
649 ... while 13:
650 ... while 14:
651 ... while 15:
652 ... while 16:
653 ... while 17:
654 ... while 18:
655 ... while 19:
656 ... while 20:
657 ... while 21:
658 ... while 22:
659 ... break
660 Traceback (most recent call last):
661 ...
Benjamin Petersone09ed542016-07-14 22:00:03 -0700662 SyntaxError: too many statically nested blocks
Thomas Wouters89f507f2006-12-13 04:49:30 +0000663
Guido van Rossum6cff8742016-09-09 09:36:26 -0700664Misuse of the nonlocal and global statement can lead to a few unique syntax errors.
665
666 >>> def f():
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200667 ... print(x)
668 ... global x
669 Traceback (most recent call last):
670 ...
671 SyntaxError: name 'x' is used prior to global declaration
672
673 >>> def f():
Guido van Rossum6cff8742016-09-09 09:36:26 -0700674 ... x = 1
675 ... global x
676 Traceback (most recent call last):
677 ...
678 SyntaxError: name 'x' is assigned to before global declaration
679
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +0200680 >>> def f(x):
681 ... global x
682 Traceback (most recent call last):
683 ...
684 SyntaxError: name 'x' is parameter and global
685
Guido van Rossum6cff8742016-09-09 09:36:26 -0700686 >>> def f():
687 ... x = 1
688 ... def g():
689 ... print(x)
690 ... nonlocal x
691 Traceback (most recent call last):
692 ...
693 SyntaxError: name 'x' is used prior to nonlocal declaration
Jeremy Hylton81e95022007-02-27 06:50:52 +0000694
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300695 >>> def f():
696 ... x = 1
697 ... def g():
698 ... x = 2
699 ... nonlocal x
700 Traceback (most recent call last):
701 ...
702 SyntaxError: name 'x' is assigned to before nonlocal declaration
703
Jeremy Hylton81e95022007-02-27 06:50:52 +0000704 >>> def f(x):
705 ... nonlocal x
706 Traceback (most recent call last):
707 ...
Nick Coghlan650f0d02007-04-15 12:05:43 +0000708 SyntaxError: name 'x' is parameter and nonlocal
Guido van Rossumd8faa362007-04-27 19:54:29 +0000709
Jeremy Hylton81e95022007-02-27 06:50:52 +0000710 >>> def f():
711 ... global x
712 ... nonlocal x
713 Traceback (most recent call last):
714 ...
715 SyntaxError: name 'x' is nonlocal and global
716
717 >>> def f():
718 ... nonlocal x
719 Traceback (most recent call last):
720 ...
721 SyntaxError: no binding for nonlocal 'x' found
722
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000723From SF bug #1705365
724 >>> nonlocal x
725 Traceback (most recent call last):
726 ...
727 SyntaxError: nonlocal declaration not allowed at module level
728
Serhiy Storchaka3b66ebe2017-10-24 00:27:14 +0300729From https://bugs.python.org/issue25973
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600730 >>> class A:
731 ... def f(self):
732 ... nonlocal __x
733 Traceback (most recent call last):
734 ...
735 SyntaxError: no binding for nonlocal '_A__x' found
736
Guido van Rossumd8faa362007-04-27 19:54:29 +0000737
738This tests assignment-context; there was a bug in Python 2.5 where compiling
739a complex 'if' (one with 'elif') would fail to notice an invalid suite,
740leading to spurious errors.
741
742 >>> if 1:
743 ... x() = 1
744 ... elif 1:
745 ... pass
746 Traceback (most recent call last):
747 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100748 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000749
750 >>> if 1:
751 ... pass
752 ... elif 1:
753 ... x() = 1
754 Traceback (most recent call last):
755 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100756 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000757
758 >>> if 1:
759 ... x() = 1
760 ... elif 1:
761 ... pass
762 ... else:
763 ... pass
764 Traceback (most recent call last):
765 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100766 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000767
768 >>> if 1:
769 ... pass
770 ... elif 1:
771 ... x() = 1
772 ... else:
773 ... pass
774 Traceback (most recent call last):
775 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100776 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Guido van Rossumd8faa362007-04-27 19:54:29 +0000777
778 >>> if 1:
779 ... pass
780 ... elif 1:
781 ... pass
782 ... else:
783 ... x() = 1
784 Traceback (most recent call last):
785 ...
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100786 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
Jeremy Hylton81e95022007-02-27 06:50:52 +0000787
Pablo Galindo58fb1562021-02-02 19:54:22 +0000788 Missing ':' before suites:
789
790 >>> def f()
791 ... pass
792 Traceback (most recent call last):
793 SyntaxError: expected ':'
794
795 >>> class A
796 ... pass
797 Traceback (most recent call last):
798 SyntaxError: expected ':'
799
800 >>> if 1
801 ... pass
802 ... elif 1:
803 ... pass
804 ... else:
805 ... x() = 1
806 Traceback (most recent call last):
807 SyntaxError: expected ':'
808
809 >>> if 1:
810 ... pass
811 ... elif 1
812 ... pass
813 ... else:
814 ... x() = 1
815 Traceback (most recent call last):
816 SyntaxError: expected ':'
817
818 >>> if 1:
819 ... pass
820 ... elif 1:
821 ... pass
822 ... else
823 ... x() = 1
824 Traceback (most recent call last):
825 SyntaxError: expected ':'
826
827 >>> for x in range(10)
828 ... pass
829 Traceback (most recent call last):
830 SyntaxError: expected ':'
831
832 >>> while True
833 ... pass
834 Traceback (most recent call last):
835 SyntaxError: expected ':'
836
837 >>> with blech as something
838 ... pass
839 Traceback (most recent call last):
840 SyntaxError: expected ':'
841
842 >>> with blech
843 ... pass
844 Traceback (most recent call last):
845 SyntaxError: expected ':'
846
847 >>> with blech, block as something
848 ... pass
849 Traceback (most recent call last):
850 SyntaxError: expected ':'
851
852 >>> with blech, block as something, bluch
853 ... pass
854 Traceback (most recent call last):
855 SyntaxError: expected ':'
856
857 >>> with (blech as something)
858 ... pass
859 Traceback (most recent call last):
860 SyntaxError: expected ':'
861
862 >>> with (blech)
863 ... pass
864 Traceback (most recent call last):
865 SyntaxError: expected ':'
866
867 >>> with (blech, block as something)
868 ... pass
869 Traceback (most recent call last):
870 SyntaxError: expected ':'
871
872 >>> with (blech, block as something, bluch)
873 ... pass
874 Traceback (most recent call last):
875 SyntaxError: expected ':'
876
877 >>> try
878 ... pass
879 Traceback (most recent call last):
880 SyntaxError: expected ':'
881
882 >>> try:
883 ... pass
884 ... except
885 ... pass
886 Traceback (most recent call last):
887 SyntaxError: expected ':'
888
Pablo Galindo08fb8ac2021-03-18 01:03:11 +0000889 >>> match x
890 ... case list():
891 ... pass
892 Traceback (most recent call last):
893 SyntaxError: expected ':'
894
895 >>> match x:
896 ... case list()
897 ... pass
898 Traceback (most recent call last):
899 SyntaxError: expected ':'
900
901 >>> match x:
902 ... case [y] if y > 0
903 ... pass
904 Traceback (most recent call last):
905 SyntaxError: expected ':'
906
Pablo Galindob86ed8e2021-04-12 16:59:30 +0100907 >>> if x = 3:
908 ... pass
909 Traceback (most recent call last):
910 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
911
912 >>> while x = 3:
913 ... pass
914 Traceback (most recent call last):
915 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
916
917 >>> if x.a = 3:
918 ... pass
919 Traceback (most recent call last):
920 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
921
922 >>> while x.a = 3:
923 ... pass
924 Traceback (most recent call last):
925 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
926
Pablo Galindoe53f72a2021-06-04 00:11:43 +0100927Custom error messages for try blocks that are not followed by except/finally
928
929 >>> try:
930 ... x = 34
931 ...
932 Traceback (most recent call last):
933 SyntaxError: expected 'except' or 'finally' block
934
Pablo Galindod9151cb2021-04-13 02:32:33 +0100935Ensure that early = are not matched by the parser as invalid comparisons
Pablo Galindob2802482021-04-15 21:38:45 +0100936 >>> f(2, 4, x=34); 1 $ 2
Pablo Galindod9151cb2021-04-13 02:32:33 +0100937 Traceback (most recent call last):
938 SyntaxError: invalid syntax
939
Pablo Galindo30ed93b2021-04-13 17:51:21 +0100940 >>> dict(x=34); x $ y
941 Traceback (most recent call last):
942 SyntaxError: invalid syntax
943
944 >>> dict(x=34, (x for x in range 10), 1); x $ y
945 Traceback (most recent call last):
946 SyntaxError: invalid syntax
947
948 >>> dict(x=34, x=1, y=2); x $ y
949 Traceback (most recent call last):
950 SyntaxError: invalid syntax
951
Pablo Galindoda743502021-04-15 14:06:39 +0100952Incomplete dictionary literals
953
954 >>> {1:2, 3:4, 5}
955 Traceback (most recent call last):
956 SyntaxError: ':' expected after dictionary key
957
958 >>> {1:2, 3:4, 5:}
959 Traceback (most recent call last):
960 SyntaxError: expression expected after dictionary key and ':'
961
962 >>> {1: *12+1, 23: 1}
963 Traceback (most recent call last):
964 SyntaxError: cannot use a starred expression in a dictionary value
965
966 >>> {1: *12+1}
967 Traceback (most recent call last):
968 SyntaxError: cannot use a starred expression in a dictionary value
969
970 >>> {1: 23, 1: *12+1}
971 Traceback (most recent call last):
972 SyntaxError: cannot use a starred expression in a dictionary value
973
974 >>> {1:}
975 Traceback (most recent call last):
976 SyntaxError: expression expected after dictionary key and ':'
977
978 # Ensure that the error is not raise for syntax errors that happen after sets
979
980 >>> {1} $
981 Traceback (most recent call last):
982 SyntaxError: invalid syntax
983
Pablo Galindo56c95df2021-04-21 15:28:21 +0100984Specialized indentation errors:
985
986 >>> while condition:
987 ... pass
988 Traceback (most recent call last):
989 IndentationError: expected an indented block after 'while' statement on line 1
990
991 >>> 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 >>> 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 >>> async for x in range(10):
1004 ... pass
1005 Traceback (most recent call last):
1006 IndentationError: expected an indented block after 'for' statement on line 1
1007
1008 >>> async for x in range(10):
1009 ... pass
1010 ... else:
1011 ... pass
1012 Traceback (most recent call last):
1013 IndentationError: expected an indented block after 'else' statement on line 3
1014
1015 >>> if something:
1016 ... pass
1017 Traceback (most recent call last):
1018 IndentationError: expected an indented block after 'if' statement on line 1
1019
1020 >>> if something:
1021 ... pass
1022 ... elif something_else:
1023 ... pass
1024 Traceback (most recent call last):
1025 IndentationError: expected an indented block after 'elif' statement on line 3
1026
1027 >>> if something:
1028 ... pass
1029 ... elif something_else:
1030 ... pass
1031 ... else:
1032 ... pass
1033 Traceback (most recent call last):
1034 IndentationError: expected an indented block after 'else' statement on line 5
1035
1036 >>> try:
1037 ... pass
1038 Traceback (most recent call last):
1039 IndentationError: expected an indented block after 'try' statement on line 1
1040
1041 >>> try:
1042 ... something()
1043 ... except A:
1044 ... pass
1045 Traceback (most recent call last):
1046 IndentationError: expected an indented block after 'except' statement on line 3
1047
1048 >>> try:
1049 ... something()
1050 ... except A:
1051 ... pass
1052 ... finally:
1053 ... pass
1054 Traceback (most recent call last):
1055 IndentationError: expected an indented block after 'finally' statement on line 5
1056
1057 >>> with A:
1058 ... pass
1059 Traceback (most recent call last):
1060 IndentationError: expected an indented block after 'with' statement on line 1
1061
1062 >>> with A as a, B as b:
1063 ... pass
1064 Traceback (most recent call last):
1065 IndentationError: expected an indented block after 'with' statement on line 1
1066
1067 >>> with (A as a, B as b):
1068 ... pass
1069 Traceback (most recent call last):
1070 IndentationError: expected an indented block after 'with' statement on line 1
1071
1072 >>> async with A:
1073 ... pass
1074 Traceback (most recent call last):
1075 IndentationError: expected an indented block after 'with' statement on line 1
1076
1077 >>> async with A as a, B as b:
1078 ... pass
1079 Traceback (most recent call last):
1080 IndentationError: expected an indented block after 'with' statement on line 1
1081
1082 >>> async with (A as a, B as b):
1083 ... pass
1084 Traceback (most recent call last):
1085 IndentationError: expected an indented block after 'with' statement on line 1
1086
1087 >>> def foo(x, /, y, *, z=2):
1088 ... pass
1089 Traceback (most recent call last):
1090 IndentationError: expected an indented block after function definition on line 1
1091
1092 >>> class Blech(A):
1093 ... pass
1094 Traceback (most recent call last):
1095 IndentationError: expected an indented block after class definition on line 1
1096
1097 >>> match something:
1098 ... pass
1099 Traceback (most recent call last):
1100 IndentationError: expected an indented block after 'match' statement on line 1
1101
1102 >>> match something:
1103 ... case []:
1104 ... pass
1105 Traceback (most recent call last):
1106 IndentationError: expected an indented block after 'case' statement on line 2
1107
1108 >>> match something:
1109 ... case []:
1110 ... ...
1111 ... case {}:
1112 ... pass
1113 Traceback (most recent call last):
1114 IndentationError: expected an indented block after 'case' statement on line 4
1115
Collin Winter828f04a2007-08-31 00:04:24 +00001116Make sure that the old "raise X, Y[, Z]" form is gone:
1117 >>> raise X, Y
1118 Traceback (most recent call last):
1119 ...
1120 SyntaxError: invalid syntax
1121 >>> raise X, Y, Z
1122 Traceback (most recent call last):
1123 ...
1124 SyntaxError: invalid syntax
1125
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001126Check that an multiple exception types with missing parentheses
Pablo Galindo206cbda2021-02-07 18:42:21 +00001127raise a custom exception
1128
1129 >>> try:
1130 ... pass
1131 ... except A, B:
1132 ... pass
1133 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001134 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001135
1136 >>> try:
1137 ... pass
1138 ... except A, B, C:
1139 ... pass
1140 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001141 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001142
1143 >>> try:
1144 ... pass
1145 ... except A, B, C as blech:
1146 ... pass
1147 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001148 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001149
1150 >>> try:
1151 ... pass
1152 ... except A, B, C as blech:
1153 ... pass
1154 ... finally:
1155 ... pass
1156 Traceback (most recent call last):
Miss Islington (bot)9a0e65c2021-05-09 14:13:50 -07001157 SyntaxError: multiple exception types must be parenthesized
Pablo Galindo206cbda2021-02-07 18:42:21 +00001158
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001159
1160>>> f(a=23, a=234)
1161Traceback (most recent call last):
1162 ...
Pablo Galindo254ec782020-04-03 20:37:13 +01001163SyntaxError: keyword argument repeated: a
Benjamin Peterson07a1f942008-07-01 20:03:27 +00001164
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001165>>> {1, 2, 3} = 42
1166Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001167SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001168
1169>>> {1: 2, 3: 4} = 42
1170Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001171SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001172
1173>>> f'{x}' = 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 '='?
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001176
1177>>> f'{x}-{y}' = 42
1178Traceback (most recent call last):
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001179SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='?
Benjamin Peterson1b1a1a42010-06-24 00:17:03 +00001180
Miss Islington (bot)ae1732d2021-05-21 11:20:43 -07001181>>> (x, y, z=3, d, e)
1182Traceback (most recent call last):
1183SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1184
1185>>> [x, y, z=3, d, e]
1186Traceback (most recent call last):
1187SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1188
1189>>> [z=3]
1190Traceback (most recent call last):
1191SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1192
1193>>> {x, y, z=3, d, e}
1194Traceback (most recent call last):
1195SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1196
1197>>> {z=3}
1198Traceback (most recent call last):
1199SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
1200
Batuhan Taskaya72e0aa22020-05-21 23:41:58 +03001201>>> from t import x,
1202Traceback (most recent call last):
1203SyntaxError: trailing comma not allowed without surrounding parentheses
1204
1205>>> from t import x,y,
1206Traceback (most recent call last):
1207SyntaxError: trailing comma not allowed without surrounding parentheses
1208
Miss Islington (bot)846a10f2021-08-18 13:32:01 -07001209# Check that we dont raise the "trailing comma" error if there is more
1210# input to the left of the valid part that we parsed.
1211
1212>>> from t import x,y, and 3
1213Traceback (most recent call last):
1214SyntaxError: invalid syntax
1215
Batuhan Taskayac8f29ad2020-06-27 21:33:08 +03001216>>> (): int
1217Traceback (most recent call last):
1218SyntaxError: only single target (not tuple) can be annotated
1219>>> []: int
1220Traceback (most recent call last):
1221SyntaxError: only single target (not list) can be annotated
1222>>> (()): int
1223Traceback (most recent call last):
1224SyntaxError: only single target (not tuple) can be annotated
1225>>> ([]): int
1226Traceback (most recent call last):
1227SyntaxError: only single target (not list) can be annotated
1228
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001229Corner-cases that used to fail to raise the correct error:
1230
1231 >>> def f(*, x=lambda __debug__:0): pass
1232 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001233 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001234
Pablo Galindob0544ba2021-04-21 12:41:19 +01001235 >>> def f(*args:(lambda __debug__:0)): pass
1236 Traceback (most recent call last):
1237 SyntaxError: cannot assign to __debug__
1238
1239 >>> def f(**kwargs:(lambda __debug__:0)): pass
1240 Traceback (most recent call last):
1241 SyntaxError: cannot assign to __debug__
1242
Lysandros Nikolaoue10e7c72020-05-04 13:58:31 +03001243 >>> with (lambda *:0): pass
1244 Traceback (most recent call last):
1245 SyntaxError: named arguments must follow bare *
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001246
1247Corner-cases that used to crash:
1248
1249 >>> def f(**__debug__): pass
1250 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001251 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001252
1253 >>> def f(*xx, __debug__): pass
1254 Traceback (most recent call last):
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001255 SyntaxError: cannot assign to __debug__
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001256
Lysandros Nikolaou7b7a21b2020-05-18 20:32:03 +03001257 >>> import ä £
1258 Traceback (most recent call last):
1259 SyntaxError: invalid character '£' (U+00A3)
Pablo Galindoa8c418d2021-06-18 22:15:57 +01001260
1261 Invalid pattern matching constructs:
1262
1263 >>> match ...:
1264 ... case 42 as _:
1265 ... ...
1266 Traceback (most recent call last):
1267 SyntaxError: cannot use '_' as a target
1268
1269 >>> match ...:
1270 ... case 42 as 1+2+4:
1271 ... ...
1272 Traceback (most recent call last):
1273 SyntaxError: invalid pattern target
Miss Islington (bot)11f1a302021-06-24 08:34:28 -07001274
1275 >>> match ...:
1276 ... case Foo(z=1, y=2, x):
1277 ... ...
1278 Traceback (most recent call last):
1279 SyntaxError: positional patterns follow keyword patterns
1280
1281 >>> match ...:
1282 ... case Foo(a, z=1, y=2, x):
1283 ... ...
1284 Traceback (most recent call last):
1285 SyntaxError: positional patterns follow keyword patterns
1286
1287 >>> match ...:
1288 ... case Foo(z=1, x, y=2):
1289 ... ...
1290 Traceback (most recent call last):
1291 SyntaxError: positional patterns follow keyword patterns
1292
1293 >>> match ...:
1294 ... case C(a=b, c, d=e, f, g=h, i, j=k, ...):
1295 ... ...
1296 Traceback (most recent call last):
1297 SyntaxError: positional patterns follow keyword patterns
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001298"""
1299
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001300import re
1301import unittest
1302
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001303from test import support
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001304
1305class SyntaxTestCase(unittest.TestCase):
1306
1307 def _check_error(self, code, errtext,
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001308 filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001309 """Check that compiling code raises SyntaxError with errtext.
1310
1311 errtest is a regular expression that must be present in the
Thomas Wouters477c8d52006-05-27 19:21:47 +00001312 test of the exception raised. If subclass is specified it
1313 is the expected subclass of SyntaxError (e.g. IndentationError).
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001314 """
1315 try:
1316 compile(code, filename, mode)
Guido van Rossumb940e112007-01-10 16:19:56 +00001317 except SyntaxError as err:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001318 if subclass and not isinstance(err, subclass):
1319 self.fail("SyntaxError is not a %s" % subclass.__name__)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001320 mo = re.search(errtext, str(err))
1321 if mo is None:
Shantanu27c0d9b2020-05-11 14:53:58 -07001322 self.fail("SyntaxError did not contain %r" % (errtext,))
Serhiy Storchaka8b583392016-12-11 14:39:01 +02001323 self.assertEqual(err.filename, filename)
1324 if lineno is not None:
1325 self.assertEqual(err.lineno, lineno)
1326 if offset is not None:
1327 self.assertEqual(err.offset, offset)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001328 else:
1329 self.fail("compile() did not raise SyntaxError")
1330
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001331 def test_expression_with_assignment(self):
1332 self._check_error(
1333 "print(end1 + end2 = ' ')",
Pablo Galindo30ed93b2021-04-13 17:51:21 +01001334 'expression cannot contain assignment, perhaps you meant "=="?',
Pablo Galindoa77aac42021-04-23 14:27:05 +01001335 offset=7
Pablo Galindo43c4fb62020-12-13 16:46:48 +00001336 )
1337
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001338 def test_curly_brace_after_primary_raises_immediately(self):
Pablo Galindo Salgadob977f852021-07-27 18:52:32 +01001339 self._check_error("f{}", "invalid syntax", mode="single")
Lysandros Nikolaou15acc4e2020-10-27 20:54:20 +02001340
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001341 def test_assign_call(self):
1342 self._check_error("f() = 1", "assign")
1343
1344 def test_assign_del(self):
Shantanu27c0d9b2020-05-11 14:53:58 -07001345 self._check_error("del (,)", "invalid syntax")
Pablo Galindob86ed8e2021-04-12 16:59:30 +01001346 self._check_error("del 1", "cannot delete literal")
1347 self._check_error("del (1, 2)", "cannot delete literal")
1348 self._check_error("del None", "cannot delete None")
1349 self._check_error("del *x", "cannot delete starred")
1350 self._check_error("del (*x)", "cannot use starred expression")
1351 self._check_error("del (*x,)", "cannot delete starred")
1352 self._check_error("del [*x,]", "cannot delete starred")
1353 self._check_error("del f()", "cannot delete function call")
1354 self._check_error("del f(a, b)", "cannot delete function call")
1355 self._check_error("del o.f()", "cannot delete function call")
1356 self._check_error("del a[0]()", "cannot delete function call")
1357 self._check_error("del x, f()", "cannot delete function call")
1358 self._check_error("del f(), x", "cannot delete function call")
1359 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call")
1360 self._check_error("del (a if True else b)", "cannot delete conditional")
1361 self._check_error("del +a", "cannot delete expression")
1362 self._check_error("del a, +b", "cannot delete expression")
1363 self._check_error("del a + b", "cannot delete expression")
1364 self._check_error("del (a + b, c)", "cannot delete expression")
1365 self._check_error("del (c[0], a + b)", "cannot delete expression")
1366 self._check_error("del a.b.c + 2", "cannot delete expression")
1367 self._check_error("del a.b.c[0] + 2", "cannot delete expression")
1368 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression")
1369 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression")
1370 self._check_error("del (a := 5)", "cannot delete named expression")
Shantanu27c0d9b2020-05-11 14:53:58 -07001371 # We don't have a special message for this, but make sure we don't
1372 # report "cannot delete name"
1373 self._check_error("del a += b", "invalid syntax")
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001374
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001375 def test_global_param_err_first(self):
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001376 source = """if 1:
1377 def error(a):
1378 global a # SyntaxError
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001379 def error2():
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001380 b = 1
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001381 global b # SyntaxError
Amaury Forgeot d'Arcfe7b4052010-09-10 19:47:43 +00001382 """
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001383 self._check_error(source, "parameter and global", lineno=3)
1384
1385 def test_nonlocal_param_err_first(self):
1386 source = """if 1:
1387 def error(a):
1388 nonlocal a # SyntaxError
1389 def error2():
1390 b = 1
1391 global b # SyntaxError
1392 """
1393 self._check_error(source, "parameter and nonlocal", lineno=3)
Jeremy Hylton42d90162003-07-15 20:24:27 +00001394
Neal Norwitzfcf44352005-11-27 20:37:43 +00001395 def test_break_outside_loop(self):
1396 self._check_error("break", "outside loop")
1397
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001398 def test_yield_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001399 self._check_error("if 0: yield", "outside function")
1400 self._check_error("if 0: yield\nelse: x=1", "outside function")
1401 self._check_error("if 1: pass\nelse: yield", "outside function")
1402 self._check_error("while 0: yield", "outside function")
1403 self._check_error("while 0: yield\nelse: x=1", "outside function")
1404 self._check_error("class C:\n if 0: yield", "outside function")
1405 self._check_error("class C:\n if 1: pass\n else: yield",
1406 "outside function")
1407 self._check_error("class C:\n while 0: yield", "outside function")
1408 self._check_error("class C:\n while 0: yield\n else: x = 1",
1409 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001410
1411 def test_return_outside_function(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001412 self._check_error("if 0: return", "outside function")
1413 self._check_error("if 0: return\nelse: x=1", "outside function")
1414 self._check_error("if 1: pass\nelse: return", "outside function")
1415 self._check_error("while 0: return", "outside function")
1416 self._check_error("class C:\n if 0: return", "outside function")
1417 self._check_error("class C:\n while 0: return", "outside function")
1418 self._check_error("class C:\n while 0: return\n else: x=1",
1419 "outside function")
1420 self._check_error("class C:\n if 0: return\n else: x= 1",
1421 "outside function")
1422 self._check_error("class C:\n if 1: pass\n else: return",
1423 "outside function")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001424
1425 def test_break_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001426 self._check_error("if 0: break", "outside loop")
1427 self._check_error("if 0: break\nelse: x=1", "outside loop")
1428 self._check_error("if 1: pass\nelse: break", "outside loop")
1429 self._check_error("class C:\n if 0: break", "outside loop")
1430 self._check_error("class C:\n if 1: pass\n else: break",
1431 "outside loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001432
1433 def test_continue_outside_loop(self):
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001434 self._check_error("if 0: continue", "not properly in loop")
1435 self._check_error("if 0: continue\nelse: x=1", "not properly in loop")
1436 self._check_error("if 1: pass\nelse: continue", "not properly in loop")
1437 self._check_error("class C:\n if 0: continue", "not properly in loop")
1438 self._check_error("class C:\n if 1: pass\n else: continue",
1439 "not properly in loop")
Pablo Galindoaf8646c2019-05-17 11:37:08 +01001440
Thomas Wouters477c8d52006-05-27 19:21:47 +00001441 def test_unexpected_indent(self):
1442 self._check_error("foo()\n bar()\n", "unexpected indent",
1443 subclass=IndentationError)
1444
1445 def test_no_indent(self):
1446 self._check_error("if 1:\nfoo()", "expected an indented block",
1447 subclass=IndentationError)
1448
1449 def test_bad_outdent(self):
1450 self._check_error("if 1:\n foo()\n bar()",
1451 "unindent does not match .* level",
1452 subclass=IndentationError)
1453
1454 def test_kwargs_last(self):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001455 self._check_error("int(base=10, '2')",
1456 "positional argument follows keyword argument")
1457
1458 def test_kwargs_last2(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001459 self._check_error("int(**{'base': 10}, '2')",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001460 "positional argument follows "
1461 "keyword argument unpacking")
1462
1463 def test_kwargs_last3(self):
Serhiy Storchaka0cc99c82018-01-04 10:36:35 +02001464 self._check_error("int(**{'base': 10}, *['2'])",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001465 "iterable argument unpacking follows "
1466 "keyword argument unpacking")
Thomas Wouters477c8d52006-05-27 19:21:47 +00001467
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001468 def test_empty_line_after_linecont(self):
1469 # See issue-40847
1470 s = r"""\
1471pass
1472 \
1473
1474pass
1475"""
1476 try:
1477 compile(s, '<string>', 'exec')
1478 except SyntaxError:
1479 self.fail("Empty line after a line continuation character is valid.")
1480
Mark Shannon02d126a2020-09-25 14:04:19 +01001481 @support.cpython_only
1482 def test_nested_named_except_blocks(self):
1483 code = ""
1484 for i in range(12):
1485 code += f"{' '*i}try:\n"
1486 code += f"{' '*(i+1)}raise Exception\n"
1487 code += f"{' '*i}except Exception as e:\n"
1488 code += f"{' '*4*12}pass"
1489 self._check_error(code, "too many statically nested blocks")
Lysandros Nikolaou896f4cf2020-06-11 02:56:08 +03001490
Pablo Galindo06f8c332020-10-30 23:48:42 +00001491 def test_barry_as_flufl_with_syntax_errors(self):
1492 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if
1493 # is reading the wrong token in the presence of syntax errors later
1494 # in the file. See bpo-42214 for more information.
1495 code = """
1496def func1():
1497 if a != b:
1498 raise ValueError
1499
1500def func2():
1501 try
1502 return 1
1503 finally:
1504 pass
1505"""
Pablo Galindo58fb1562021-02-02 19:54:22 +00001506 self._check_error(code, "expected ':'")
Pablo Galindo06f8c332020-10-30 23:48:42 +00001507
Pablo Galindo96eeff52021-03-22 17:28:11 +00001508 def test_invalid_line_continuation_error_position(self):
1509 self._check_error(r"a = 3 \ 4",
1510 "unexpected character after line continuation character",
1511 lineno=1, offset=9)
1512
Lysandros Nikolaou02cdfc92020-10-31 20:31:41 +02001513 def test_invalid_line_continuation_left_recursive(self):
1514 # Check bpo-42218: SyntaxErrors following left-recursive rules
1515 # (t_primary_raw in this case) need to be tested explicitly
1516 self._check_error("A.\u018a\\ ",
1517 "unexpected character after line continuation character")
1518 self._check_error("A.\u03bc\\\n",
1519 "unexpected EOF while parsing")
1520
Pablo Galindod6d63712021-01-19 23:59:33 +00001521 def test_error_parenthesis(self):
1522 for paren in "([{":
1523 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed")
1524
1525 for paren in ")]}":
1526 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'")
1527
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001528 def test_match_call_does_not_raise_syntax_error(self):
1529 code = """
1530def match(x):
1531 return 1+1
1532
1533match(34)
1534"""
1535 compile(code, "<string>", "exec")
1536
1537 def test_case_call_does_not_raise_syntax_error(self):
1538 code = """
1539def case(x):
1540 return 1+1
1541
1542case(34)
1543"""
1544 compile(code, "<string>", "exec")
Miss Islington (bot)ec0699c2021-05-19 11:28:31 -07001545
Miss Islington (bot)13de28f2021-05-07 13:40:09 -07001546 def test_multiline_compiler_error_points_to_the_end(self):
1547 self._check_error(
1548 "call(\na=1,\na=1\n)",
1549 "keyword argument repeated",
1550 lineno=3
1551 )
Pablo Galindo08fb8ac2021-03-18 01:03:11 +00001552
Pablo Galindod6d63712021-01-19 23:59:33 +00001553
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001554def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001555 support.run_unittest(SyntaxTestCase)
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001556 from test import test_syntax
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001557 support.run_doctest(test_syntax, verbosity=True)
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00001558
1559if __name__ == "__main__":
1560 test_main()