1"""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): 8SyntaxError: name 'x' is parameter and global 9 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 30>>> obj.None = 1 31Traceback (most recent call last): 32SyntaxError: invalid syntax 33 34>>> None = 1 35Traceback (most recent call last): 36SyntaxError: 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 46>>> (True := 1) 47Traceback (most recent call last): 48SyntaxError: cannot use assignment expressions with True 49 50>>> 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__ 57 58>>> (__debug__ := 1) 59Traceback (most recent call last): 60SyntaxError: cannot assign to __debug__ 61 62>>> del __debug__ 63Traceback (most recent call last): 64SyntaxError: cannot delete __debug__ 65 66>>> f() = 1 67Traceback (most recent call last): 68SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 69 70>>> yield = 1 71Traceback (most recent call last): 72SyntaxError: assignment to yield expression not possible 73 74>>> del f() 75Traceback (most recent call last): 76SyntaxError: cannot delete function call 77 78>>> a + 1 = 2 79Traceback (most recent call last): 80SyntaxError: cannot assign to expression here. Maybe you meant '==' instead of '='? 81 82>>> (x for x in x) = 1 83Traceback (most recent call last): 84SyntaxError: cannot assign to generator expression 85 86>>> 1 = 1 87Traceback (most recent call last): 88SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 89 90>>> "abc" = 1 91Traceback (most recent call last): 92SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 93 94>>> b"" = 1 95Traceback (most recent call last): 96SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? 97 98>>> ... = 1 99Traceback (most recent call last): 100SyntaxError: cannot assign to ellipsis here. Maybe you meant '==' instead of '='? 101 102>>> `1` = 1 103Traceback (most recent call last): 104SyntaxError: invalid syntax 105 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 111>>> (a, "b", c) = (1, 2, 3) 112Traceback (most recent call last): 113SyntaxError: cannot assign to literal 114 115>>> (a, True, c) = (1, 2, 3) 116Traceback (most recent call last): 117SyntaxError: cannot assign to True 118 119>>> (a, __debug__, c) = (1, 2, 3) 120Traceback (most recent call last): 121SyntaxError: cannot assign to __debug__ 122 123>>> (a, *True, c) = (1, 2, 3) 124Traceback (most recent call last): 125SyntaxError: cannot assign to True 126 127>>> (a, *__debug__, c) = (1, 2, 3) 128Traceback (most recent call last): 129SyntaxError: cannot assign to __debug__ 130 131>>> [a, b, c + 1] = [1, 2, 3] 132Traceback (most recent call last): 133SyntaxError: cannot assign to expression 134 135>>> [a, b[1], c + 1] = [1, 2, 3] 136Traceback (most recent call last): 137SyntaxError: cannot assign to expression 138 139>>> [a, b.c.d, c + 1] = [1, 2, 3] 140Traceback (most recent call last): 141SyntaxError: cannot assign to expression 142 143>>> a if 1 else b = 1 144Traceback (most recent call last): 145SyntaxError: cannot assign to conditional expression 146 147>>> 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 159>>> if True: 160... print("Hello" 161... 162... if 2: 163... print(123)) 164Traceback (most recent call last): 165SyntaxError: invalid syntax 166 167>>> 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 179>>> a, b += 1, 2 180Traceback (most recent call last): 181SyntaxError: 'tuple' is an illegal expression for augmented assignment 182 183>>> (a, b) += 1, 2 184Traceback (most recent call last): 185SyntaxError: 'tuple' is an illegal expression for augmented assignment 186 187>>> [a, b] += 1, 2 188Traceback (most recent call last): 189SyntaxError: 'list' is an illegal expression for augmented assignment 190 191Invalid 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): 208SyntaxError: cannot assign to expression 209 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): 220SyntaxError: cannot assign to expression 221 222>>> for i < (): pass 223Traceback (most recent call last): 224SyntaxError: invalid syntax 225 226>>> for a, b 227Traceback (most recent call last): 228SyntaxError: invalid syntax 229 230>>> 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): 244SyntaxError: cannot assign to expression 245 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 254>>> with a as b 255Traceback (most recent call last): 256SyntaxError: expected ':' 257 258>>> p = p = 259Traceback (most recent call last): 260SyntaxError: invalid syntax 261 262Comprehensions without 'in' keyword: 263 264>>> [x for x if range(1)] 265Traceback (most recent call last): 266SyntaxError: 'in' expected after for-loop variables 267 268>>> tuple(x for x if range(1)) 269Traceback (most recent call last): 270SyntaxError: 'in' expected after for-loop variables 271 272>>> [x for x() in a] 273Traceback (most recent call last): 274SyntaxError: cannot assign to function call 275 276>>> [x for a, b, (c + 1, d()) in y] 277Traceback (most recent call last): 278SyntaxError: cannot assign to expression 279 280>>> [x for a, b, (c + 1, d()) if y] 281Traceback (most recent call last): 282SyntaxError: 'in' expected after for-loop variables 283 284>>> [x for x+1 in y] 285Traceback (most recent call last): 286SyntaxError: cannot assign to expression 287 288>>> [x for x+1, x() in y] 289Traceback (most recent call last): 290SyntaxError: cannot assign to expression 291 292Comprehensions creating tuples without parentheses 293should produce a specialized error message: 294 295>>> [x,y for x,y in range(100)] 296Traceback (most recent call last): 297SyntaxError: did you forget parentheses around the comprehension target? 298 299>>> {x,y for x,y in range(100)} 300Traceback (most recent call last): 301SyntaxError: did you forget parentheses around the comprehension target? 302 303# Missing commas in literals collections should not 304# produce special error messages regarding missing 305# parentheses, but about missing commas instead 306 307>>> [1, 2 3] 308Traceback (most recent call last): 309SyntaxError: invalid syntax. Perhaps you forgot a comma? 310 311>>> {1, 2 3} 312Traceback (most recent call last): 313SyntaxError: invalid syntax. Perhaps you forgot a comma? 314 315>>> {1:2, 2:5 3:12} 316Traceback (most recent call last): 317SyntaxError: invalid syntax. Perhaps you forgot a comma? 318 319>>> (1, 2 3) 320Traceback (most recent call last): 321SyntaxError: invalid syntax. Perhaps you forgot a comma? 322 323# Make sure soft keywords constructs don't raise specialized 324# errors regarding missing commas or other spezialiced errors 325 326>>> match x: 327... y = 3 328Traceback (most recent call last): 329SyntaxError: invalid syntax 330 331>>> match x: 332... case y: 333... 3 $ 3 334Traceback (most recent call last): 335SyntaxError: invalid syntax 336 337>>> match x: 338... case $: 339... ... 340Traceback (most recent call last): 341SyntaxError: invalid syntax 342 343>>> match ...: 344... case {**rest, "key": value}: 345... ... 346Traceback (most recent call last): 347SyntaxError: invalid syntax 348 349>>> match ...: 350... case {**_}: 351... ... 352Traceback (most recent call last): 353SyntaxError: invalid syntax 354 355From compiler_complex_args(): 356 357>>> def f(None=1): 358... pass 359Traceback (most recent call last): 360SyntaxError: invalid syntax 361 362From ast_for_arguments(): 363 364>>> def f(x, y=1, z): 365... pass 366Traceback (most recent call last): 367SyntaxError: parameter without a default follows parameter with a default 368 369>>> def f(x, /, y=1, z): 370... pass 371Traceback (most recent call last): 372SyntaxError: parameter without a default follows parameter with a default 373 374>>> def f(x, None): 375... pass 376Traceback (most recent call last): 377SyntaxError: invalid syntax 378 379>>> def f(*None): 380... pass 381Traceback (most recent call last): 382SyntaxError: invalid syntax 383 384>>> def f(**None): 385... pass 386Traceback (most recent call last): 387SyntaxError: invalid syntax 388 389>>> def foo(/,a,b=,c): 390... pass 391Traceback (most recent call last): 392SyntaxError: at least one argument must precede / 393 394>>> def foo(a,/,/,b,c): 395... pass 396Traceback (most recent call last): 397SyntaxError: / may appear only once 398 399>>> def foo(a,/,a1,/,b,c): 400... pass 401Traceback (most recent call last): 402SyntaxError: / may appear only once 403 404>>> def foo(a=1,/,/,*b,/,c): 405... pass 406Traceback (most recent call last): 407SyntaxError: / may appear only once 408 409>>> def foo(a,/,a1=1,/,b,c): 410... pass 411Traceback (most recent call last): 412SyntaxError: / may appear only once 413 414>>> def foo(a,*b,c,/,d,e): 415... pass 416Traceback (most recent call last): 417SyntaxError: / must be ahead of * 418 419>>> def foo(a=1,*b,c=3,/,d,e): 420... pass 421Traceback (most recent call last): 422SyntaxError: / must be ahead of * 423 424>>> def foo(a,*b=3,c): 425... pass 426Traceback (most recent call last): 427SyntaxError: var-positional argument cannot have default value 428 429>>> def foo(a,*b: int=,c): 430... pass 431Traceback (most recent call last): 432SyntaxError: var-positional argument cannot have default value 433 434>>> def foo(a,**b=3): 435... pass 436Traceback (most recent call last): 437SyntaxError: var-keyword argument cannot have default value 438 439>>> def foo(a,**b: int=3): 440... pass 441Traceback (most recent call last): 442SyntaxError: var-keyword argument cannot have default value 443 444>>> def foo(a,*a, b, **c, d): 445... pass 446Traceback (most recent call last): 447SyntaxError: arguments cannot follow var-keyword argument 448 449>>> def foo(a,*a, b, **c, d=4): 450... pass 451Traceback (most recent call last): 452SyntaxError: arguments cannot follow var-keyword argument 453 454>>> def foo(a,*a, b, **c, *d): 455... pass 456Traceback (most recent call last): 457SyntaxError: arguments cannot follow var-keyword argument 458 459>>> def foo(a,*a, b, **c, **d): 460... pass 461Traceback (most recent call last): 462SyntaxError: arguments cannot follow var-keyword argument 463 464>>> def foo(a=1,/,**b,/,c): 465... pass 466Traceback (most recent call last): 467SyntaxError: arguments cannot follow var-keyword argument 468 469>>> def foo(*b,*d): 470... pass 471Traceback (most recent call last): 472SyntaxError: * argument may appear only once 473 474>>> def foo(a,*b,c,*d,*e,c): 475... pass 476Traceback (most recent call last): 477SyntaxError: * argument may appear only once 478 479>>> def foo(a,b,/,c,*b,c,*d,*e,c): 480... pass 481Traceback (most recent call last): 482SyntaxError: * argument may appear only once 483 484>>> def foo(a,b,/,c,*b,c,*d,**e): 485... pass 486Traceback (most recent call last): 487SyntaxError: * argument may appear only once 488 489>>> def foo(a=1,/*,b,c): 490... pass 491Traceback (most recent call last): 492SyntaxError: expected comma between / and * 493 494>>> def foo(a=1,d=,c): 495... pass 496Traceback (most recent call last): 497SyntaxError: expected default value expression 498 499>>> def foo(a,d=,c): 500... pass 501Traceback (most recent call last): 502SyntaxError: expected default value expression 503 504>>> def foo(a,d: int=,c): 505... pass 506Traceback (most recent call last): 507SyntaxError: expected default value expression 508 509>>> lambda /,a,b,c: None 510Traceback (most recent call last): 511SyntaxError: at least one argument must precede / 512 513>>> lambda a,/,/,b,c: None 514Traceback (most recent call last): 515SyntaxError: / may appear only once 516 517>>> lambda a,/,a1,/,b,c: None 518Traceback (most recent call last): 519SyntaxError: / may appear only once 520 521>>> lambda a=1,/,/,*b,/,c: None 522Traceback (most recent call last): 523SyntaxError: / may appear only once 524 525>>> lambda a,/,a1=1,/,b,c: None 526Traceback (most recent call last): 527SyntaxError: / may appear only once 528 529>>> lambda a,*b,c,/,d,e: None 530Traceback (most recent call last): 531SyntaxError: / must be ahead of * 532 533>>> lambda a=1,*b,c=3,/,d,e: None 534Traceback (most recent call last): 535SyntaxError: / must be ahead of * 536 537>>> lambda a=1,/*,b,c: None 538Traceback (most recent call last): 539SyntaxError: expected comma between / and * 540 541>>> lambda a,*b=3,c: None 542Traceback (most recent call last): 543SyntaxError: var-positional argument cannot have default value 544 545>>> lambda a,**b=3: None 546Traceback (most recent call last): 547SyntaxError: var-keyword argument cannot have default value 548 549>>> lambda a, *a, b, **c, d: None 550Traceback (most recent call last): 551SyntaxError: arguments cannot follow var-keyword argument 552 553>>> lambda a,*a, b, **c, d=4: None 554Traceback (most recent call last): 555SyntaxError: arguments cannot follow var-keyword argument 556 557>>> lambda a,*a, b, **c, *d: None 558Traceback (most recent call last): 559SyntaxError: arguments cannot follow var-keyword argument 560 561>>> lambda a,*a, b, **c, **d: None 562Traceback (most recent call last): 563SyntaxError: arguments cannot follow var-keyword argument 564 565>>> lambda a=1,/,**b,/,c: None 566Traceback (most recent call last): 567SyntaxError: arguments cannot follow var-keyword argument 568 569>>> lambda *b,*d: None 570Traceback (most recent call last): 571SyntaxError: * argument may appear only once 572 573>>> lambda a,*b,c,*d,*e,c: None 574Traceback (most recent call last): 575SyntaxError: * argument may appear only once 576 577>>> lambda a,b,/,c,*b,c,*d,*e,c: None 578Traceback (most recent call last): 579SyntaxError: * argument may appear only once 580 581>>> lambda a,b,/,c,*b,c,*d,**e: None 582Traceback (most recent call last): 583SyntaxError: * argument may appear only once 584 585>>> lambda a=1,d=,c: None 586Traceback (most recent call last): 587SyntaxError: expected default value expression 588 589>>> lambda a,d=,c: None 590Traceback (most recent call last): 591SyntaxError: expected default value expression 592 593>>> lambda a,d=3,c: None 594Traceback (most recent call last): 595SyntaxError: parameter without a default follows parameter with a default 596 597>>> lambda a,/,d=3,c: None 598Traceback (most recent call last): 599SyntaxError: parameter without a default follows parameter with a default 600 601>>> import ast; ast.parse(''' 602... def f( 603... *, # type: int 604... a, # type: int 605... ): 606... pass 607... ''', type_comments=True) 608Traceback (most recent call last): 609SyntaxError: bare * has associated type comment 610 611 612From ast_for_funcdef(): 613 614>>> def None(x): 615... pass 616Traceback (most recent call last): 617SyntaxError: invalid syntax 618 619 620From ast_for_call(): 621 622>>> def f(it, *varargs, **kwargs): 623... return list(it) 624>>> L = range(10) 625>>> f(x for x in L) 626[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 627>>> f(x for x in L, 1) 628Traceback (most recent call last): 629SyntaxError: Generator expression must be parenthesized 630>>> f(x for x in L, y=1) 631Traceback (most recent call last): 632SyntaxError: Generator expression must be parenthesized 633>>> f(x for x in L, *[]) 634Traceback (most recent call last): 635SyntaxError: Generator expression must be parenthesized 636>>> f(x for x in L, **{}) 637Traceback (most recent call last): 638SyntaxError: Generator expression must be parenthesized 639>>> f(L, x for x in L) 640Traceback (most recent call last): 641SyntaxError: Generator expression must be parenthesized 642>>> f(x for x in L, y for y in L) 643Traceback (most recent call last): 644SyntaxError: Generator expression must be parenthesized 645>>> f(x for x in L,) 646Traceback (most recent call last): 647SyntaxError: Generator expression must be parenthesized 648>>> f((x for x in L), 1) 649[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 650>>> class C(x for x in L): 651... pass 652Traceback (most recent call last): 653SyntaxError: invalid syntax 654 655>>> def g(*args, **kwargs): 656... print(args, sorted(kwargs.items())) 657>>> g(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 658... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 659... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 660... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 661... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 662... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 663... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 664... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 665... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 666... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 667... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 668... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 669... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 670... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 671... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 672... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 673... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 674... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 675... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 676... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS 677(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) [] 678 679>>> g(a000=0, a001=1, a002=2, a003=3, a004=4, a005=5, a006=6, a007=7, a008=8, 680... a009=9, a010=10, a011=11, a012=12, a013=13, a014=14, a015=15, a016=16, 681... a017=17, a018=18, a019=19, a020=20, a021=21, a022=22, a023=23, a024=24, 682... a025=25, a026=26, a027=27, a028=28, a029=29, a030=30, a031=31, a032=32, 683... a033=33, a034=34, a035=35, a036=36, a037=37, a038=38, a039=39, a040=40, 684... a041=41, a042=42, a043=43, a044=44, a045=45, a046=46, a047=47, a048=48, 685... a049=49, a050=50, a051=51, a052=52, a053=53, a054=54, a055=55, a056=56, 686... a057=57, a058=58, a059=59, a060=60, a061=61, a062=62, a063=63, a064=64, 687... a065=65, a066=66, a067=67, a068=68, a069=69, a070=70, a071=71, a072=72, 688... a073=73, a074=74, a075=75, a076=76, a077=77, a078=78, a079=79, a080=80, 689... a081=81, a082=82, a083=83, a084=84, a085=85, a086=86, a087=87, a088=88, 690... a089=89, a090=90, a091=91, a092=92, a093=93, a094=94, a095=95, a096=96, 691... a097=97, a098=98, a099=99, a100=100, a101=101, a102=102, a103=103, 692... a104=104, a105=105, a106=106, a107=107, a108=108, a109=109, a110=110, 693... a111=111, a112=112, a113=113, a114=114, a115=115, a116=116, a117=117, 694... a118=118, a119=119, a120=120, a121=121, a122=122, a123=123, a124=124, 695... a125=125, a126=126, a127=127, a128=128, a129=129, a130=130, a131=131, 696... a132=132, a133=133, a134=134, a135=135, a136=136, a137=137, a138=138, 697... a139=139, a140=140, a141=141, a142=142, a143=143, a144=144, a145=145, 698... a146=146, a147=147, a148=148, a149=149, a150=150, a151=151, a152=152, 699... a153=153, a154=154, a155=155, a156=156, a157=157, a158=158, a159=159, 700... a160=160, a161=161, a162=162, a163=163, a164=164, a165=165, a166=166, 701... a167=167, a168=168, a169=169, a170=170, a171=171, a172=172, a173=173, 702... a174=174, a175=175, a176=176, a177=177, a178=178, a179=179, a180=180, 703... a181=181, a182=182, a183=183, a184=184, a185=185, a186=186, a187=187, 704... a188=188, a189=189, a190=190, a191=191, a192=192, a193=193, a194=194, 705... a195=195, a196=196, a197=197, a198=198, a199=199, a200=200, a201=201, 706... a202=202, a203=203, a204=204, a205=205, a206=206, a207=207, a208=208, 707... a209=209, a210=210, a211=211, a212=212, a213=213, a214=214, a215=215, 708... a216=216, a217=217, a218=218, a219=219, a220=220, a221=221, a222=222, 709... a223=223, a224=224, a225=225, a226=226, a227=227, a228=228, a229=229, 710... a230=230, a231=231, a232=232, a233=233, a234=234, a235=235, a236=236, 711... a237=237, a238=238, a239=239, a240=240, a241=241, a242=242, a243=243, 712... a244=244, a245=245, a246=246, a247=247, a248=248, a249=249, a250=250, 713... a251=251, a252=252, a253=253, a254=254, a255=255, a256=256, a257=257, 714... a258=258, a259=259, a260=260, a261=261, a262=262, a263=263, a264=264, 715... a265=265, a266=266, a267=267, a268=268, a269=269, a270=270, a271=271, 716... a272=272, a273=273, a274=274, a275=275, a276=276, a277=277, a278=278, 717... a279=279, a280=280, a281=281, a282=282, a283=283, a284=284, a285=285, 718... a286=286, a287=287, a288=288, a289=289, a290=290, a291=291, a292=292, 719... a293=293, a294=294, a295=295, a296=296, a297=297, a298=298, a299=299) 720... # doctest: +ELLIPSIS 721() [('a000', 0), ('a001', 1), ('a002', 2), ..., ('a298', 298), ('a299', 299)] 722 723>>> class C: 724... def meth(self, *args): 725... return args 726>>> obj = C() 727>>> obj.meth( 728... 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 729... 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 730... 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 731... 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 732... 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 733... 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 734... 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 735... 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 736... 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 737... 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 738... 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 739... 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 740... 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 741... 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 742... 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 743... 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 744... 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 745... 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 746... 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 747... 290, 291, 292, 293, 294, 295, 296, 297, 298, 299) # doctest: +ELLIPSIS 748(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ..., 297, 298, 299) 749 750>>> f(lambda x: x[0] = 3) 751Traceback (most recent call last): 752SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 753 754# Check that this error doesn't trigger for names: 755>>> f(a={x: for x in {}}) 756Traceback (most recent call last): 757SyntaxError: invalid syntax 758 759The grammar accepts any test (basically, any expression) in the 760keyword slot of a call site. Test a few different options. 761 762>>> f(x()=2) 763Traceback (most recent call last): 764SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 765>>> f(a or b=1) 766Traceback (most recent call last): 767SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 768>>> f(x.y=1) 769Traceback (most recent call last): 770SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 771>>> f((x)=2) 772Traceback (most recent call last): 773SyntaxError: expression cannot contain assignment, perhaps you meant "=="? 774>>> f(True=1) 775Traceback (most recent call last): 776SyntaxError: cannot assign to True 777>>> f(False=1) 778Traceback (most recent call last): 779SyntaxError: cannot assign to False 780>>> f(None=1) 781Traceback (most recent call last): 782SyntaxError: cannot assign to None 783>>> f(__debug__=1) 784Traceback (most recent call last): 785SyntaxError: cannot assign to __debug__ 786>>> __debug__: int 787Traceback (most recent call last): 788SyntaxError: cannot assign to __debug__ 789>>> f(a=) 790Traceback (most recent call last): 791SyntaxError: expected argument value expression 792>>> f(a, b, c=) 793Traceback (most recent call last): 794SyntaxError: expected argument value expression 795>>> f(a, b, c=, d) 796Traceback (most recent call last): 797SyntaxError: expected argument value expression 798>>> f(*args=[0]) 799Traceback (most recent call last): 800SyntaxError: cannot assign to iterable argument unpacking 801>>> f(a, b, *args=[0]) 802Traceback (most recent call last): 803SyntaxError: cannot assign to iterable argument unpacking 804>>> f(**kwargs={'a': 1}) 805Traceback (most recent call last): 806SyntaxError: cannot assign to keyword argument unpacking 807>>> f(a, b, *args, **kwargs={'a': 1}) 808Traceback (most recent call last): 809SyntaxError: cannot assign to keyword argument unpacking 810 811 812More set_context(): 813 814>>> (x for x in x) += 1 815Traceback (most recent call last): 816SyntaxError: 'generator expression' is an illegal expression for augmented assignment 817>>> None += 1 818Traceback (most recent call last): 819SyntaxError: 'None' is an illegal expression for augmented assignment 820>>> __debug__ += 1 821Traceback (most recent call last): 822SyntaxError: cannot assign to __debug__ 823>>> f() += 1 824Traceback (most recent call last): 825SyntaxError: 'function call' is an illegal expression for augmented assignment 826 827 828Test continue in finally in weird combinations. 829 830continue in for loop under finally should be ok. 831 832 >>> def test(): 833 ... try: 834 ... pass 835 ... finally: 836 ... for abc in range(10): 837 ... continue 838 ... print(abc) 839 >>> test() 840 9 841 842continue in a finally should be ok. 843 844 >>> def test(): 845 ... for abc in range(10): 846 ... try: 847 ... pass 848 ... finally: 849 ... continue 850 ... print(abc) 851 >>> test() 852 9 853 854 >>> def test(): 855 ... for abc in range(10): 856 ... try: 857 ... pass 858 ... finally: 859 ... try: 860 ... continue 861 ... except: 862 ... pass 863 ... print(abc) 864 >>> test() 865 9 866 867 >>> def test(): 868 ... for abc in range(10): 869 ... try: 870 ... pass 871 ... finally: 872 ... try: 873 ... pass 874 ... except: 875 ... continue 876 ... print(abc) 877 >>> test() 878 9 879 880A continue outside loop should not be allowed. 881 882 >>> def foo(): 883 ... try: 884 ... pass 885 ... finally: 886 ... continue 887 Traceback (most recent call last): 888 ... 889 SyntaxError: 'continue' not properly in loop 890 891There is one test for a break that is not in a loop. The compiler 892uses a single data structure to keep track of try-finally and loops, 893so we need to be sure that a break is actually inside a loop. If it 894isn't, there should be a syntax error. 895 896 >>> try: 897 ... print(1) 898 ... break 899 ... print(2) 900 ... finally: 901 ... print(3) 902 Traceback (most recent call last): 903 ... 904 SyntaxError: 'break' outside loop 905 906Misuse of the nonlocal and global statement can lead to a few unique syntax errors. 907 908 >>> def f(): 909 ... print(x) 910 ... global x 911 Traceback (most recent call last): 912 ... 913 SyntaxError: name 'x' is used prior to global declaration 914 915 >>> def f(): 916 ... x = 1 917 ... global x 918 Traceback (most recent call last): 919 ... 920 SyntaxError: name 'x' is assigned to before global declaration 921 922 >>> def f(x): 923 ... global x 924 Traceback (most recent call last): 925 ... 926 SyntaxError: name 'x' is parameter and global 927 928 >>> def f(): 929 ... x = 1 930 ... def g(): 931 ... print(x) 932 ... nonlocal x 933 Traceback (most recent call last): 934 ... 935 SyntaxError: name 'x' is used prior to nonlocal declaration 936 937 >>> def f(): 938 ... x = 1 939 ... def g(): 940 ... x = 2 941 ... nonlocal x 942 Traceback (most recent call last): 943 ... 944 SyntaxError: name 'x' is assigned to before nonlocal declaration 945 946 >>> def f(x): 947 ... nonlocal x 948 Traceback (most recent call last): 949 ... 950 SyntaxError: name 'x' is parameter and nonlocal 951 952 >>> def f(): 953 ... global x 954 ... nonlocal x 955 Traceback (most recent call last): 956 ... 957 SyntaxError: name 'x' is nonlocal and global 958 959 >>> def f(): 960 ... nonlocal x 961 Traceback (most recent call last): 962 ... 963 SyntaxError: no binding for nonlocal 'x' found 964 965From SF bug #1705365 966 >>> nonlocal x 967 Traceback (most recent call last): 968 ... 969 SyntaxError: nonlocal declaration not allowed at module level 970 971From https://bugs.python.org/issue25973 972 >>> class A: 973 ... def f(self): 974 ... nonlocal __x 975 Traceback (most recent call last): 976 ... 977 SyntaxError: no binding for nonlocal '_A__x' found 978 979 980This tests assignment-context; there was a bug in Python 2.5 where compiling 981a complex 'if' (one with 'elif') would fail to notice an invalid suite, 982leading to spurious errors. 983 984 >>> if 1: 985 ... x() = 1 986 ... elif 1: 987 ... pass 988 Traceback (most recent call last): 989 ... 990 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 991 992 >>> if 1: 993 ... pass 994 ... elif 1: 995 ... x() = 1 996 Traceback (most recent call last): 997 ... 998 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 999 1000 >>> if 1: 1001 ... x() = 1 1002 ... elif 1: 1003 ... pass 1004 ... else: 1005 ... pass 1006 Traceback (most recent call last): 1007 ... 1008 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 1009 1010 >>> if 1: 1011 ... pass 1012 ... elif 1: 1013 ... x() = 1 1014 ... else: 1015 ... pass 1016 Traceback (most recent call last): 1017 ... 1018 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 1019 1020 >>> if 1: 1021 ... pass 1022 ... elif 1: 1023 ... pass 1024 ... else: 1025 ... x() = 1 1026 Traceback (most recent call last): 1027 ... 1028 SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='? 1029 1030Missing ':' before suites: 1031 1032 >>> def f() 1033 ... pass 1034 Traceback (most recent call last): 1035 SyntaxError: expected ':' 1036 1037 >>> def f[T]() 1038 ... pass 1039 Traceback (most recent call last): 1040 SyntaxError: expected ':' 1041 1042 >>> class A 1043 ... pass 1044 Traceback (most recent call last): 1045 SyntaxError: expected ':' 1046 1047 >>> class A[T] 1048 ... pass 1049 Traceback (most recent call last): 1050 SyntaxError: expected ':' 1051 1052 >>> class A[T]() 1053 ... pass 1054 Traceback (most recent call last): 1055 SyntaxError: expected ':' 1056 1057 >>> class R&D: 1058 ... pass 1059 Traceback (most recent call last): 1060 SyntaxError: invalid syntax 1061 1062 >>> if 1 1063 ... pass 1064 ... elif 1: 1065 ... pass 1066 ... else: 1067 ... x() = 1 1068 Traceback (most recent call last): 1069 SyntaxError: expected ':' 1070 1071 >>> if 1: 1072 ... pass 1073 ... elif 1 1074 ... pass 1075 ... else: 1076 ... x() = 1 1077 Traceback (most recent call last): 1078 SyntaxError: expected ':' 1079 1080 >>> if 1: 1081 ... pass 1082 ... elif 1: 1083 ... pass 1084 ... else 1085 ... x() = 1 1086 Traceback (most recent call last): 1087 SyntaxError: expected ':' 1088 1089 >>> for x in range(10) 1090 ... pass 1091 Traceback (most recent call last): 1092 SyntaxError: expected ':' 1093 1094 >>> for x in range 10: 1095 ... pass 1096 Traceback (most recent call last): 1097 SyntaxError: invalid syntax 1098 1099 >>> while True 1100 ... pass 1101 Traceback (most recent call last): 1102 SyntaxError: expected ':' 1103 1104 >>> with blech as something 1105 ... pass 1106 Traceback (most recent call last): 1107 SyntaxError: expected ':' 1108 1109 >>> with blech 1110 ... pass 1111 Traceback (most recent call last): 1112 SyntaxError: expected ':' 1113 1114 >>> with blech, block as something 1115 ... pass 1116 Traceback (most recent call last): 1117 SyntaxError: expected ':' 1118 1119 >>> with blech, block as something, bluch 1120 ... pass 1121 Traceback (most recent call last): 1122 SyntaxError: expected ':' 1123 1124 >>> with (blech as something) 1125 ... pass 1126 Traceback (most recent call last): 1127 SyntaxError: expected ':' 1128 1129 >>> with (blech) 1130 ... pass 1131 Traceback (most recent call last): 1132 SyntaxError: expected ':' 1133 1134 >>> with (blech, block as something) 1135 ... pass 1136 Traceback (most recent call last): 1137 SyntaxError: expected ':' 1138 1139 >>> with (blech, block as something, bluch) 1140 ... pass 1141 Traceback (most recent call last): 1142 SyntaxError: expected ':' 1143 1144 >>> with block ad something: 1145 ... pass 1146 Traceback (most recent call last): 1147 SyntaxError: invalid syntax 1148 1149 >>> try 1150 ... pass 1151 Traceback (most recent call last): 1152 SyntaxError: expected ':' 1153 1154 >>> try: 1155 ... pass 1156 ... except 1157 ... pass 1158 Traceback (most recent call last): 1159 SyntaxError: expected ':' 1160 1161 >>> match x 1162 ... case list(): 1163 ... pass 1164 Traceback (most recent call last): 1165 SyntaxError: expected ':' 1166 1167 >>> match x x: 1168 ... case list(): 1169 ... pass 1170 Traceback (most recent call last): 1171 SyntaxError: invalid syntax 1172 1173 >>> match x: 1174 ... case list() 1175 ... pass 1176 Traceback (most recent call last): 1177 SyntaxError: expected ':' 1178 1179 >>> match x: 1180 ... case [y] if y > 0 1181 ... pass 1182 Traceback (most recent call last): 1183 SyntaxError: expected ':' 1184 1185 >>> if x = 3: 1186 ... pass 1187 Traceback (most recent call last): 1188 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1189 1190 >>> while x = 3: 1191 ... pass 1192 Traceback (most recent call last): 1193 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1194 1195 >>> if x.a = 3: 1196 ... pass 1197 Traceback (most recent call last): 1198 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='? 1199 1200 >>> while x.a = 3: 1201 ... pass 1202 Traceback (most recent call last): 1203 SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='? 1204 1205 1206Missing parens after function definition 1207 1208 >>> def f: 1209 Traceback (most recent call last): 1210 SyntaxError: expected '(' 1211 1212 >>> async def f: 1213 Traceback (most recent call last): 1214 SyntaxError: expected '(' 1215 1216 >>> def f -> int: 1217 Traceback (most recent call last): 1218 SyntaxError: expected '(' 1219 1220 >>> async def f -> int: # type: int 1221 Traceback (most recent call last): 1222 SyntaxError: expected '(' 1223 1224 >>> async def f[T]: 1225 Traceback (most recent call last): 1226 SyntaxError: expected '(' 1227 1228 >>> def f[T] -> str: 1229 Traceback (most recent call last): 1230 SyntaxError: expected '(' 1231 1232Parenthesized arguments in function definitions 1233 1234 >>> def f(x, (y, z), w): 1235 ... pass 1236 Traceback (most recent call last): 1237 SyntaxError: Function parameters cannot be parenthesized 1238 1239 >>> def f((x, y, z, w)): 1240 ... pass 1241 Traceback (most recent call last): 1242 SyntaxError: Function parameters cannot be parenthesized 1243 1244 >>> def f(x, (y, z, w)): 1245 ... pass 1246 Traceback (most recent call last): 1247 SyntaxError: Function parameters cannot be parenthesized 1248 1249 >>> def f((x, y, z), w): 1250 ... pass 1251 Traceback (most recent call last): 1252 SyntaxError: Function parameters cannot be parenthesized 1253 1254 >>> lambda x, (y, z), w: None 1255 Traceback (most recent call last): 1256 SyntaxError: Lambda expression parameters cannot be parenthesized 1257 1258 >>> lambda (x, y, z, w): None 1259 Traceback (most recent call last): 1260 SyntaxError: Lambda expression parameters cannot be parenthesized 1261 1262 >>> lambda x, (y, z, w): None 1263 Traceback (most recent call last): 1264 SyntaxError: Lambda expression parameters cannot be parenthesized 1265 1266 >>> lambda (x, y, z), w: None 1267 Traceback (most recent call last): 1268 SyntaxError: Lambda expression parameters cannot be parenthesized 1269 1270Custom error messages for try blocks that are not followed by except/finally 1271 1272 >>> try: 1273 ... x = 34 1274 ... 1275 Traceback (most recent call last): 1276 SyntaxError: expected 'except' or 'finally' block 1277 1278Custom error message for try block mixing except and except* 1279 1280 >>> try: 1281 ... pass 1282 ... except TypeError: 1283 ... pass 1284 ... except* ValueError: 1285 ... pass 1286 Traceback (most recent call last): 1287 SyntaxError: cannot have both 'except' and 'except*' on the same 'try' 1288 1289 >>> try: 1290 ... pass 1291 ... except* TypeError: 1292 ... pass 1293 ... except ValueError: 1294 ... pass 1295 Traceback (most recent call last): 1296 SyntaxError: cannot have both 'except' and 'except*' on the same 'try' 1297 1298 >>> try: 1299 ... pass 1300 ... except TypeError: 1301 ... pass 1302 ... except TypeError: 1303 ... pass 1304 ... except* ValueError: 1305 ... pass 1306 Traceback (most recent call last): 1307 SyntaxError: cannot have both 'except' and 'except*' on the same 'try' 1308 1309 >>> try: 1310 ... pass 1311 ... except* TypeError: 1312 ... pass 1313 ... except* TypeError: 1314 ... pass 1315 ... except ValueError: 1316 ... pass 1317 Traceback (most recent call last): 1318 SyntaxError: cannot have both 'except' and 'except*' on the same 'try' 1319 1320Ensure that early = are not matched by the parser as invalid comparisons 1321 >>> f(2, 4, x=34); 1 $ 2 1322 Traceback (most recent call last): 1323 SyntaxError: invalid syntax 1324 1325 >>> dict(x=34); x $ y 1326 Traceback (most recent call last): 1327 SyntaxError: invalid syntax 1328 1329 >>> dict(x=34, (x for x in range 10), 1); x $ y 1330 Traceback (most recent call last): 1331 SyntaxError: invalid syntax 1332 1333 >>> dict(x=34, x=1, y=2); x $ y 1334 Traceback (most recent call last): 1335 SyntaxError: invalid syntax 1336 1337Incomplete dictionary literals 1338 1339 >>> {1:2, 3:4, 5} 1340 Traceback (most recent call last): 1341 SyntaxError: ':' expected after dictionary key 1342 1343 >>> {1:2, 3:4, 5:} 1344 Traceback (most recent call last): 1345 SyntaxError: expression expected after dictionary key and ':' 1346 1347 >>> {1: *12+1, 23: 1} 1348 Traceback (most recent call last): 1349 SyntaxError: cannot use a starred expression in a dictionary value 1350 1351 >>> {1: *12+1} 1352 Traceback (most recent call last): 1353 SyntaxError: cannot use a starred expression in a dictionary value 1354 1355 >>> {1: 23, 1: *12+1} 1356 Traceback (most recent call last): 1357 SyntaxError: cannot use a starred expression in a dictionary value 1358 1359 >>> {1:} 1360 Traceback (most recent call last): 1361 SyntaxError: expression expected after dictionary key and ':' 1362 1363 # Ensure that the error is not raised for syntax errors that happen after sets 1364 1365 >>> {1} $ 1366 Traceback (most recent call last): 1367 SyntaxError: invalid syntax 1368 1369 # Ensure that the error is not raised for invalid expressions 1370 1371 >>> {1: 2, 3: foo(,), 4: 5} 1372 Traceback (most recent call last): 1373 SyntaxError: invalid syntax 1374 1375 >>> {1: $, 2: 3} 1376 Traceback (most recent call last): 1377 SyntaxError: invalid syntax 1378 1379Specialized indentation errors: 1380 1381 >>> while condition: 1382 ... pass 1383 Traceback (most recent call last): 1384 IndentationError: expected an indented block after 'while' statement on line 1 1385 1386 >>> for x in range(10): 1387 ... pass 1388 Traceback (most recent call last): 1389 IndentationError: expected an indented block after 'for' statement on line 1 1390 1391 >>> for x in range(10): 1392 ... pass 1393 ... else: 1394 ... pass 1395 Traceback (most recent call last): 1396 IndentationError: expected an indented block after 'else' statement on line 3 1397 1398 >>> async for x in range(10): 1399 ... pass 1400 Traceback (most recent call last): 1401 IndentationError: expected an indented block after 'for' statement on line 1 1402 1403 >>> async for x in range(10): 1404 ... pass 1405 ... else: 1406 ... pass 1407 Traceback (most recent call last): 1408 IndentationError: expected an indented block after 'else' statement on line 3 1409 1410 >>> if something: 1411 ... pass 1412 Traceback (most recent call last): 1413 IndentationError: expected an indented block after 'if' statement on line 1 1414 1415 >>> if something: 1416 ... pass 1417 ... elif something_else: 1418 ... pass 1419 Traceback (most recent call last): 1420 IndentationError: expected an indented block after 'elif' statement on line 3 1421 1422 >>> if something: 1423 ... pass 1424 ... elif something_else: 1425 ... pass 1426 ... else: 1427 ... pass 1428 Traceback (most recent call last): 1429 IndentationError: expected an indented block after 'else' statement on line 5 1430 1431 >>> try: 1432 ... pass 1433 Traceback (most recent call last): 1434 IndentationError: expected an indented block after 'try' statement on line 1 1435 1436 >>> try: 1437 ... something() 1438 ... except: 1439 ... pass 1440 Traceback (most recent call last): 1441 IndentationError: expected an indented block after 'except' statement on line 3 1442 1443 >>> try: 1444 ... something() 1445 ... except A: 1446 ... pass 1447 Traceback (most recent call last): 1448 IndentationError: expected an indented block after 'except' statement on line 3 1449 1450 >>> try: 1451 ... something() 1452 ... except* A: 1453 ... pass 1454 Traceback (most recent call last): 1455 IndentationError: expected an indented block after 'except*' statement on line 3 1456 1457 >>> try: 1458 ... something() 1459 ... except A: 1460 ... pass 1461 ... finally: 1462 ... pass 1463 Traceback (most recent call last): 1464 IndentationError: expected an indented block after 'finally' statement on line 5 1465 1466 >>> try: 1467 ... something() 1468 ... except* A: 1469 ... pass 1470 ... finally: 1471 ... pass 1472 Traceback (most recent call last): 1473 IndentationError: expected an indented block after 'finally' statement on line 5 1474 1475 >>> with A: 1476 ... pass 1477 Traceback (most recent call last): 1478 IndentationError: expected an indented block after 'with' statement on line 1 1479 1480 >>> with A as a, B as b: 1481 ... pass 1482 Traceback (most recent call last): 1483 IndentationError: expected an indented block after 'with' statement on line 1 1484 1485 >>> with (A as a, B as b): 1486 ... pass 1487 Traceback (most recent call last): 1488 IndentationError: expected an indented block after 'with' statement on line 1 1489 1490 >>> async with A: 1491 ... pass 1492 Traceback (most recent call last): 1493 IndentationError: expected an indented block after 'with' statement on line 1 1494 1495 >>> async with A as a, B as b: 1496 ... pass 1497 Traceback (most recent call last): 1498 IndentationError: expected an indented block after 'with' statement on line 1 1499 1500 >>> async with (A as a, B as b): 1501 ... pass 1502 Traceback (most recent call last): 1503 IndentationError: expected an indented block after 'with' statement on line 1 1504 1505 >>> def foo(x, /, y, *, z=2): 1506 ... pass 1507 Traceback (most recent call last): 1508 IndentationError: expected an indented block after function definition on line 1 1509 1510 >>> def foo[T](x, /, y, *, z=2): 1511 ... pass 1512 Traceback (most recent call last): 1513 IndentationError: expected an indented block after function definition on line 1 1514 1515 >>> class Blech(A): 1516 ... pass 1517 Traceback (most recent call last): 1518 IndentationError: expected an indented block after class definition on line 1 1519 1520 >>> class Blech[T](A): 1521 ... pass 1522 Traceback (most recent call last): 1523 IndentationError: expected an indented block after class definition on line 1 1524 1525 >>> match something: 1526 ... pass 1527 Traceback (most recent call last): 1528 IndentationError: expected an indented block after 'match' statement on line 1 1529 1530 >>> match something: 1531 ... case []: 1532 ... pass 1533 Traceback (most recent call last): 1534 IndentationError: expected an indented block after 'case' statement on line 2 1535 1536 >>> match something: 1537 ... case []: 1538 ... ... 1539 ... case {}: 1540 ... pass 1541 Traceback (most recent call last): 1542 IndentationError: expected an indented block after 'case' statement on line 4 1543 1544Make sure that the old "raise X, Y[, Z]" form is gone: 1545 >>> raise X, Y 1546 Traceback (most recent call last): 1547 ... 1548 SyntaxError: invalid syntax 1549 >>> raise X, Y, Z 1550 Traceback (most recent call last): 1551 ... 1552 SyntaxError: invalid syntax 1553 1554Check that an multiple exception types with missing parentheses 1555raise a custom exception 1556 1557 >>> try: 1558 ... pass 1559 ... except A, B: 1560 ... pass 1561 Traceback (most recent call last): 1562 SyntaxError: multiple exception types must be parenthesized 1563 1564 >>> try: 1565 ... pass 1566 ... except A, B, C: 1567 ... pass 1568 Traceback (most recent call last): 1569 SyntaxError: multiple exception types must be parenthesized 1570 1571 >>> try: 1572 ... pass 1573 ... except A, B, C as blech: 1574 ... pass 1575 Traceback (most recent call last): 1576 SyntaxError: multiple exception types must be parenthesized 1577 1578 >>> try: 1579 ... pass 1580 ... except A, B, C as blech: 1581 ... pass 1582 ... finally: 1583 ... pass 1584 Traceback (most recent call last): 1585 SyntaxError: multiple exception types must be parenthesized 1586 1587 1588 >>> try: 1589 ... pass 1590 ... except* A, B: 1591 ... pass 1592 Traceback (most recent call last): 1593 SyntaxError: multiple exception types must be parenthesized 1594 1595 >>> try: 1596 ... pass 1597 ... except* A, B, C: 1598 ... pass 1599 Traceback (most recent call last): 1600 SyntaxError: multiple exception types must be parenthesized 1601 1602 >>> try: 1603 ... pass 1604 ... except* A, B, C as blech: 1605 ... pass 1606 Traceback (most recent call last): 1607 SyntaxError: multiple exception types must be parenthesized 1608 1609 >>> try: 1610 ... pass 1611 ... except* A, B, C as blech: 1612 ... pass 1613 ... finally: 1614 ... pass 1615 Traceback (most recent call last): 1616 SyntaxError: multiple exception types must be parenthesized 1617 1618Custom exception for 'except*' without an exception type 1619 1620 >>> try: 1621 ... pass 1622 ... except* A as a: 1623 ... pass 1624 ... except*: 1625 ... pass 1626 Traceback (most recent call last): 1627 SyntaxError: expected one or more exception types 1628 1629 1630>>> f(a=23, a=234) 1631Traceback (most recent call last): 1632 ... 1633SyntaxError: keyword argument repeated: a 1634 1635>>> {1, 2, 3} = 42 1636Traceback (most recent call last): 1637SyntaxError: cannot assign to set display here. Maybe you meant '==' instead of '='? 1638 1639>>> {1: 2, 3: 4} = 42 1640Traceback (most recent call last): 1641SyntaxError: cannot assign to dict literal here. Maybe you meant '==' instead of '='? 1642 1643>>> f'{x}' = 42 1644Traceback (most recent call last): 1645SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='? 1646 1647>>> f'{x}-{y}' = 42 1648Traceback (most recent call last): 1649SyntaxError: cannot assign to f-string expression here. Maybe you meant '==' instead of '='? 1650 1651>>> (x, y, z=3, d, e) 1652Traceback (most recent call last): 1653SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1654 1655>>> [x, y, z=3, d, e] 1656Traceback (most recent call last): 1657SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1658 1659>>> [z=3] 1660Traceback (most recent call last): 1661SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1662 1663>>> {x, y, z=3, d, e} 1664Traceback (most recent call last): 1665SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1666 1667>>> {z=3} 1668Traceback (most recent call last): 1669SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? 1670 1671>>> from t import x, 1672Traceback (most recent call last): 1673SyntaxError: trailing comma not allowed without surrounding parentheses 1674 1675>>> from t import x,y, 1676Traceback (most recent call last): 1677SyntaxError: trailing comma not allowed without surrounding parentheses 1678 1679>>> import a from b 1680Traceback (most recent call last): 1681SyntaxError: Did you mean to use 'from ... import ...' instead? 1682 1683>>> import a.y.z from b.y.z 1684Traceback (most recent call last): 1685SyntaxError: Did you mean to use 'from ... import ...' instead? 1686 1687>>> import a from b as bar 1688Traceback (most recent call last): 1689SyntaxError: Did you mean to use 'from ... import ...' instead? 1690 1691>>> import a.y.z from b.y.z as bar 1692Traceback (most recent call last): 1693SyntaxError: Did you mean to use 'from ... import ...' instead? 1694 1695>>> import a, b,c from b 1696Traceback (most recent call last): 1697SyntaxError: Did you mean to use 'from ... import ...' instead? 1698 1699>>> import a.y.z, b.y.z, c.y.z from b.y.z 1700Traceback (most recent call last): 1701SyntaxError: Did you mean to use 'from ... import ...' instead? 1702 1703>>> import a,b,c from b as bar 1704Traceback (most recent call last): 1705SyntaxError: Did you mean to use 'from ... import ...' instead? 1706 1707>>> import a.y.z, b.y.z, c.y.z from b.y.z as bar 1708Traceback (most recent call last): 1709SyntaxError: Did you mean to use 'from ... import ...' instead? 1710 1711# Check that we dont raise the "trailing comma" error if there is more 1712# input to the left of the valid part that we parsed. 1713 1714>>> from t import x,y, and 3 1715Traceback (most recent call last): 1716SyntaxError: invalid syntax 1717 1718>>> from i import 1719Traceback (most recent call last): 1720SyntaxError: Expected one or more names after 'import' 1721 1722>>> from .. import 1723Traceback (most recent call last): 1724SyntaxError: Expected one or more names after 'import' 1725 1726>>> import 1727Traceback (most recent call last): 1728SyntaxError: Expected one or more names after 'import' 1729 1730>>> (): int 1731Traceback (most recent call last): 1732SyntaxError: only single target (not tuple) can be annotated 1733>>> []: int 1734Traceback (most recent call last): 1735SyntaxError: only single target (not list) can be annotated 1736>>> (()): int 1737Traceback (most recent call last): 1738SyntaxError: only single target (not tuple) can be annotated 1739>>> ([]): int 1740Traceback (most recent call last): 1741SyntaxError: only single target (not list) can be annotated 1742 1743# 'not' after operators: 1744 1745>>> 3 + not 3 1746Traceback (most recent call last): 1747SyntaxError: 'not' after an operator must be parenthesized 1748 1749>>> 3 * not 3 1750Traceback (most recent call last): 1751SyntaxError: 'not' after an operator must be parenthesized 1752 1753>>> + not 3 1754Traceback (most recent call last): 1755SyntaxError: 'not' after an operator must be parenthesized 1756 1757>>> - not 3 1758Traceback (most recent call last): 1759SyntaxError: 'not' after an operator must be parenthesized 1760 1761>>> ~ not 3 1762Traceback (most recent call last): 1763SyntaxError: 'not' after an operator must be parenthesized 1764 1765>>> 3 + - not 3 1766Traceback (most recent call last): 1767SyntaxError: 'not' after an operator must be parenthesized 1768 1769>>> 3 + not -1 1770Traceback (most recent call last): 1771SyntaxError: 'not' after an operator must be parenthesized 1772 1773# Check that we don't introduce misleading errors 1774>>> not 1 */ 2 1775Traceback (most recent call last): 1776SyntaxError: invalid syntax 1777 1778>>> not 1 + 1779Traceback (most recent call last): 1780SyntaxError: invalid syntax 1781 1782>>> not + 1 + 1783Traceback (most recent call last): 1784SyntaxError: invalid syntax 1785 1786Corner-cases that used to fail to raise the correct error: 1787 1788 >>> def f(*, x=lambda __debug__:0): pass 1789 Traceback (most recent call last): 1790 SyntaxError: cannot assign to __debug__ 1791 1792 >>> def f(*args:(lambda __debug__:0)): pass 1793 Traceback (most recent call last): 1794 SyntaxError: cannot assign to __debug__ 1795 1796 >>> def f(**kwargs:(lambda __debug__:0)): pass 1797 Traceback (most recent call last): 1798 SyntaxError: cannot assign to __debug__ 1799 1800 >>> with (lambda *:0): pass 1801 Traceback (most recent call last): 1802 SyntaxError: named arguments must follow bare * 1803 1804Corner-cases that used to crash: 1805 1806 >>> def f(**__debug__): pass 1807 Traceback (most recent call last): 1808 SyntaxError: cannot assign to __debug__ 1809 1810 >>> def f(*xx, __debug__): pass 1811 Traceback (most recent call last): 1812 SyntaxError: cannot assign to __debug__ 1813 1814 >>> import ä £ 1815 Traceback (most recent call last): 1816 SyntaxError: invalid character '£' (U+00A3) 1817 1818 Invalid pattern matching constructs: 1819 1820 >>> match ...: 1821 ... case 42 as _: 1822 ... ... 1823 Traceback (most recent call last): 1824 SyntaxError: cannot use '_' as a target 1825 1826 >>> match ...: 1827 ... case 42 as 1+2+4: 1828 ... ... 1829 Traceback (most recent call last): 1830 SyntaxError: invalid pattern target 1831 1832 >>> match ...: 1833 ... case Foo(z=1, y=2, x): 1834 ... ... 1835 Traceback (most recent call last): 1836 SyntaxError: positional patterns follow keyword patterns 1837 1838 >>> match ...: 1839 ... case Foo(a, z=1, y=2, x): 1840 ... ... 1841 Traceback (most recent call last): 1842 SyntaxError: positional patterns follow keyword patterns 1843 1844 >>> match ...: 1845 ... case Foo(z=1, x, y=2): 1846 ... ... 1847 Traceback (most recent call last): 1848 SyntaxError: positional patterns follow keyword patterns 1849 1850 >>> match ...: 1851 ... case C(a=b, c, d=e, f, g=h, i, j=k, ...): 1852 ... ... 1853 Traceback (most recent call last): 1854 SyntaxError: positional patterns follow keyword patterns 1855 1856Uses of the star operator which should fail: 1857 1858A[:*b] 1859 1860 >>> A[:*b] 1861 Traceback (most recent call last): 1862 ... 1863 SyntaxError: invalid syntax 1864 >>> A[:(*b)] 1865 Traceback (most recent call last): 1866 ... 1867 SyntaxError: cannot use starred expression here 1868 >>> A[:*b] = 1 1869 Traceback (most recent call last): 1870 ... 1871 SyntaxError: invalid syntax 1872 >>> del A[:*b] 1873 Traceback (most recent call last): 1874 ... 1875 SyntaxError: invalid syntax 1876 1877A[*b:] 1878 1879 >>> A[*b:] 1880 Traceback (most recent call last): 1881 ... 1882 SyntaxError: invalid syntax 1883 >>> A[(*b):] 1884 Traceback (most recent call last): 1885 ... 1886 SyntaxError: cannot use starred expression here 1887 >>> A[*b:] = 1 1888 Traceback (most recent call last): 1889 ... 1890 SyntaxError: invalid syntax 1891 >>> del A[*b:] 1892 Traceback (most recent call last): 1893 ... 1894 SyntaxError: invalid syntax 1895 1896A[*b:*b] 1897 1898 >>> A[*b:*b] 1899 Traceback (most recent call last): 1900 ... 1901 SyntaxError: invalid syntax 1902 >>> A[(*b:*b)] 1903 Traceback (most recent call last): 1904 ... 1905 SyntaxError: invalid syntax 1906 >>> A[*b:*b] = 1 1907 Traceback (most recent call last): 1908 ... 1909 SyntaxError: invalid syntax 1910 >>> del A[*b:*b] 1911 Traceback (most recent call last): 1912 ... 1913 SyntaxError: invalid syntax 1914 1915A[*(1:2)] 1916 1917 >>> A[*(1:2)] 1918 Traceback (most recent call last): 1919 ... 1920 SyntaxError: Invalid star expression 1921 >>> A[*(1:2)] = 1 1922 Traceback (most recent call last): 1923 ... 1924 SyntaxError: Invalid star expression 1925 >>> del A[*(1:2)] 1926 Traceback (most recent call last): 1927 ... 1928 SyntaxError: Invalid star expression 1929 1930A[*:] and A[:*] 1931 1932 >>> A[*:] 1933 Traceback (most recent call last): 1934 ... 1935 SyntaxError: Invalid star expression 1936 >>> A[:*] 1937 Traceback (most recent call last): 1938 ... 1939 SyntaxError: invalid syntax 1940 1941A[*] 1942 1943 >>> A[*] 1944 Traceback (most recent call last): 1945 ... 1946 SyntaxError: Invalid star expression 1947 1948A[**] 1949 1950 >>> A[**] 1951 Traceback (most recent call last): 1952 ... 1953 SyntaxError: invalid syntax 1954 1955A[**b] 1956 1957 >>> A[**b] 1958 Traceback (most recent call last): 1959 ... 1960 SyntaxError: invalid syntax 1961 >>> A[**b] = 1 1962 Traceback (most recent call last): 1963 ... 1964 SyntaxError: invalid syntax 1965 >>> del A[**b] 1966 Traceback (most recent call last): 1967 ... 1968 SyntaxError: invalid syntax 1969 1970def f(x: *b) 1971 1972 >>> def f6(x: *b): pass 1973 Traceback (most recent call last): 1974 ... 1975 SyntaxError: invalid syntax 1976 >>> def f7(x: *b = 1): pass 1977 Traceback (most recent call last): 1978 ... 1979 SyntaxError: invalid syntax 1980 1981**kwargs: *a 1982 1983 >>> def f8(**kwargs: *a): pass 1984 Traceback (most recent call last): 1985 ... 1986 SyntaxError: invalid syntax 1987 1988x: *b 1989 1990 >>> x: *b 1991 Traceback (most recent call last): 1992 ... 1993 SyntaxError: invalid syntax 1994 >>> x: *b = 1 1995 Traceback (most recent call last): 1996 ... 1997 SyntaxError: invalid syntax 1998 1999Invalid bytes literals: 2000 2001 >>> b"Ā" 2002 Traceback (most recent call last): 2003 ... 2004 b"Ā" 2005 ^^^ 2006 SyntaxError: bytes can only contain ASCII literal characters 2007 2008 >>> b"абвгде" 2009 Traceback (most recent call last): 2010 ... 2011 b"абвгде" 2012 ^^^^^^^^ 2013 SyntaxError: bytes can only contain ASCII literal characters 2014 2015 >>> b"abc ъющый" # first 3 letters are ascii 2016 Traceback (most recent call last): 2017 ... 2018 b"abc ъющый" 2019 ^^^^^^^^^^^ 2020 SyntaxError: bytes can only contain ASCII literal characters 2021 2022Invalid expressions in type scopes: 2023 2024 >>> type A[] = int 2025 Traceback (most recent call last): 2026 ... 2027 SyntaxError: Type parameter list cannot be empty 2028 2029 >>> class A[]: ... 2030 Traceback (most recent call last): 2031 ... 2032 SyntaxError: Type parameter list cannot be empty 2033 2034 >>> def some[](): ... 2035 Traceback (most recent call last): 2036 ... 2037 SyntaxError: Type parameter list cannot be empty 2038 2039 >>> def some[]() 2040 Traceback (most recent call last): 2041 ... 2042 SyntaxError: Type parameter list cannot be empty 2043 2044 >>> async def some[]: # type: int 2045 Traceback (most recent call last): 2046 ... 2047 SyntaxError: Type parameter list cannot be empty 2048 2049 >>> def f[T: (x:=3)](): pass 2050 Traceback (most recent call last): 2051 ... 2052 SyntaxError: named expression cannot be used within a TypeVar bound 2053 2054 >>> def f[T: ((x:= 3), int)](): pass 2055 Traceback (most recent call last): 2056 ... 2057 SyntaxError: named expression cannot be used within a TypeVar constraint 2058 2059 >>> def f[T = ((x:=3))](): pass 2060 Traceback (most recent call last): 2061 ... 2062 SyntaxError: named expression cannot be used within a TypeVar default 2063 2064 >>> async def f[T: (x:=3)](): pass 2065 Traceback (most recent call last): 2066 ... 2067 SyntaxError: named expression cannot be used within a TypeVar bound 2068 2069 >>> async def f[T: ((x:= 3), int)](): pass 2070 Traceback (most recent call last): 2071 ... 2072 SyntaxError: named expression cannot be used within a TypeVar constraint 2073 2074 >>> async def f[T = ((x:=3))](): pass 2075 Traceback (most recent call last): 2076 ... 2077 SyntaxError: named expression cannot be used within a TypeVar default 2078 2079 >>> type A[T: (x:=3)] = int 2080 Traceback (most recent call last): 2081 ... 2082 SyntaxError: named expression cannot be used within a TypeVar bound 2083 2084 >>> type A[T: ((x:= 3), int)] = int 2085 Traceback (most recent call last): 2086 ... 2087 SyntaxError: named expression cannot be used within a TypeVar constraint 2088 2089 >>> type A[T = ((x:=3))] = int 2090 Traceback (most recent call last): 2091 ... 2092 SyntaxError: named expression cannot be used within a TypeVar default 2093 2094 >>> def f[T: (yield)](): pass 2095 Traceback (most recent call last): 2096 ... 2097 SyntaxError: yield expression cannot be used within a TypeVar bound 2098 2099 >>> def f[T: (int, (yield))](): pass 2100 Traceback (most recent call last): 2101 ... 2102 SyntaxError: yield expression cannot be used within a TypeVar constraint 2103 2104 >>> def f[T = (yield)](): pass 2105 Traceback (most recent call last): 2106 ... 2107 SyntaxError: yield expression cannot be used within a TypeVar default 2108 2109 >>> def f[*Ts = (yield)](): pass 2110 Traceback (most recent call last): 2111 ... 2112 SyntaxError: yield expression cannot be used within a TypeVarTuple default 2113 2114 >>> def f[**P = [(yield), int]](): pass 2115 Traceback (most recent call last): 2116 ... 2117 SyntaxError: yield expression cannot be used within a ParamSpec default 2118 2119 >>> type A[T: (yield 3)] = int 2120 Traceback (most recent call last): 2121 ... 2122 SyntaxError: yield expression cannot be used within a TypeVar bound 2123 2124 >>> type A[T: (int, (yield 3))] = int 2125 Traceback (most recent call last): 2126 ... 2127 SyntaxError: yield expression cannot be used within a TypeVar constraint 2128 2129 >>> type A[T = (yield 3)] = int 2130 Traceback (most recent call last): 2131 ... 2132 SyntaxError: yield expression cannot be used within a TypeVar default 2133 2134 >>> type A[T: (await 3)] = int 2135 Traceback (most recent call last): 2136 ... 2137 SyntaxError: await expression cannot be used within a TypeVar bound 2138 2139 >>> type A[T: (yield from [])] = int 2140 Traceback (most recent call last): 2141 ... 2142 SyntaxError: yield expression cannot be used within a TypeVar bound 2143 2144 >>> class A[T: (yield 3)]: pass 2145 Traceback (most recent call last): 2146 ... 2147 SyntaxError: yield expression cannot be used within a TypeVar bound 2148 2149 >>> class A[T: (int, (yield 3))]: pass 2150 Traceback (most recent call last): 2151 ... 2152 SyntaxError: yield expression cannot be used within a TypeVar constraint 2153 2154 >>> class A[T = (yield)]: pass 2155 Traceback (most recent call last): 2156 ... 2157 SyntaxError: yield expression cannot be used within a TypeVar default 2158 2159 >>> class A[*Ts = (yield)]: pass 2160 Traceback (most recent call last): 2161 ... 2162 SyntaxError: yield expression cannot be used within a TypeVarTuple default 2163 2164 >>> class A[**P = [(yield), int]]: pass 2165 Traceback (most recent call last): 2166 ... 2167 SyntaxError: yield expression cannot be used within a ParamSpec default 2168 2169 >>> type A = (x := 3) 2170 Traceback (most recent call last): 2171 ... 2172 SyntaxError: named expression cannot be used within a type alias 2173 2174 >>> type A = (yield 3) 2175 Traceback (most recent call last): 2176 ... 2177 SyntaxError: yield expression cannot be used within a type alias 2178 2179 >>> type A = (await 3) 2180 Traceback (most recent call last): 2181 ... 2182 SyntaxError: await expression cannot be used within a type alias 2183 2184 >>> type A = (yield from []) 2185 Traceback (most recent call last): 2186 ... 2187 SyntaxError: yield expression cannot be used within a type alias 2188 2189 >>> class A[T]((x := 3)): ... 2190 Traceback (most recent call last): 2191 ... 2192 SyntaxError: named expression cannot be used within the definition of a generic 2193 2194 >>> class A[T]((yield 3)): ... 2195 Traceback (most recent call last): 2196 ... 2197 SyntaxError: yield expression cannot be used within the definition of a generic 2198 2199 >>> class A[T]((await 3)): ... 2200 Traceback (most recent call last): 2201 ... 2202 SyntaxError: await expression cannot be used within the definition of a generic 2203 2204 >>> class A[T]((yield from [])): ... 2205 Traceback (most recent call last): 2206 ... 2207 SyntaxError: yield expression cannot be used within the definition of a generic 2208 2209 >>> f(**x, *y) 2210 Traceback (most recent call last): 2211 SyntaxError: iterable argument unpacking follows keyword argument unpacking 2212 2213 >>> f(**x, *) 2214 Traceback (most recent call last): 2215 SyntaxError: Invalid star expression 2216 2217 >>> f(x, *:) 2218 Traceback (most recent call last): 2219 SyntaxError: Invalid star expression 2220 2221 >>> f(x, *) 2222 Traceback (most recent call last): 2223 SyntaxError: Invalid star expression 2224 2225 >>> f(x = 5, *) 2226 Traceback (most recent call last): 2227 SyntaxError: Invalid star expression 2228 2229 >>> f(x = 5, *:) 2230 Traceback (most recent call last): 2231 SyntaxError: Invalid star expression 2232""" 2233 2234import re 2235import doctest 2236import textwrap 2237import unittest 2238 2239from test import support 2240 2241class SyntaxTestCase(unittest.TestCase): 2242 2243 def _check_error(self, code, errtext, 2244 filename="<testcase>", mode="exec", subclass=None, 2245 lineno=None, offset=None, end_lineno=None, end_offset=None): 2246 """Check that compiling code raises SyntaxError with errtext. 2247 2248 errtest is a regular expression that must be present in the 2249 test of the exception raised. If subclass is specified it 2250 is the expected subclass of SyntaxError (e.g. IndentationError). 2251 """ 2252 try: 2253 compile(code, filename, mode) 2254 except SyntaxError as err: 2255 if subclass and not isinstance(err, subclass): 2256 self.fail("SyntaxError is not a %s" % subclass.__name__) 2257 mo = re.search(errtext, str(err)) 2258 if mo is None: 2259 self.fail("SyntaxError did not contain %r" % (errtext,)) 2260 self.assertEqual(err.filename, filename) 2261 if lineno is not None: 2262 self.assertEqual(err.lineno, lineno) 2263 if offset is not None: 2264 self.assertEqual(err.offset, offset) 2265 if end_lineno is not None: 2266 self.assertEqual(err.end_lineno, end_lineno) 2267 if end_offset is not None: 2268 self.assertEqual(err.end_offset, end_offset) 2269 2270 else: 2271 self.fail("compile() did not raise SyntaxError") 2272 2273 def test_expression_with_assignment(self): 2274 self._check_error( 2275 "print(end1 + end2 = ' ')", 2276 'expression cannot contain assignment, perhaps you meant "=="?', 2277 offset=7 2278 ) 2279 2280 def test_curly_brace_after_primary_raises_immediately(self): 2281 self._check_error("f{}", "invalid syntax", mode="single") 2282 2283 def test_assign_call(self): 2284 self._check_error("f() = 1", "assign") 2285 2286 def test_assign_del(self): 2287 self._check_error("del (,)", "invalid syntax") 2288 self._check_error("del 1", "cannot delete literal") 2289 self._check_error("del (1, 2)", "cannot delete literal") 2290 self._check_error("del None", "cannot delete None") 2291 self._check_error("del *x", "cannot delete starred") 2292 self._check_error("del (*x)", "cannot use starred expression") 2293 self._check_error("del (*x,)", "cannot delete starred") 2294 self._check_error("del [*x,]", "cannot delete starred") 2295 self._check_error("del f()", "cannot delete function call") 2296 self._check_error("del f(a, b)", "cannot delete function call") 2297 self._check_error("del o.f()", "cannot delete function call") 2298 self._check_error("del a[0]()", "cannot delete function call") 2299 self._check_error("del x, f()", "cannot delete function call") 2300 self._check_error("del f(), x", "cannot delete function call") 2301 self._check_error("del [a, b, ((c), (d,), e.f())]", "cannot delete function call") 2302 self._check_error("del (a if True else b)", "cannot delete conditional") 2303 self._check_error("del +a", "cannot delete expression") 2304 self._check_error("del a, +b", "cannot delete expression") 2305 self._check_error("del a + b", "cannot delete expression") 2306 self._check_error("del (a + b, c)", "cannot delete expression") 2307 self._check_error("del (c[0], a + b)", "cannot delete expression") 2308 self._check_error("del a.b.c + 2", "cannot delete expression") 2309 self._check_error("del a.b.c[0] + 2", "cannot delete expression") 2310 self._check_error("del (a, b, (c, d.e.f + 2))", "cannot delete expression") 2311 self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "cannot delete expression") 2312 self._check_error("del (a := 5)", "cannot delete named expression") 2313 # We don't have a special message for this, but make sure we don't 2314 # report "cannot delete name" 2315 self._check_error("del a += b", "invalid syntax") 2316 2317 def test_global_param_err_first(self): 2318 source = """if 1: 2319 def error(a): 2320 global a # SyntaxError 2321 def error2(): 2322 b = 1 2323 global b # SyntaxError 2324 """ 2325 self._check_error(source, "parameter and global", lineno=3) 2326 2327 def test_nonlocal_param_err_first(self): 2328 source = """if 1: 2329 def error(a): 2330 nonlocal a # SyntaxError 2331 def error2(): 2332 b = 1 2333 global b # SyntaxError 2334 """ 2335 self._check_error(source, "parameter and nonlocal", lineno=3) 2336 2337 def test_yield_outside_function(self): 2338 self._check_error("if 0: yield", "outside function") 2339 self._check_error("if 0: yield\nelse: x=1", "outside function") 2340 self._check_error("if 1: pass\nelse: yield", "outside function") 2341 self._check_error("while 0: yield", "outside function") 2342 self._check_error("while 0: yield\nelse: x=1", "outside function") 2343 self._check_error("class C:\n if 0: yield", "outside function") 2344 self._check_error("class C:\n if 1: pass\n else: yield", 2345 "outside function") 2346 self._check_error("class C:\n while 0: yield", "outside function") 2347 self._check_error("class C:\n while 0: yield\n else: x = 1", 2348 "outside function") 2349 2350 def test_return_outside_function(self): 2351 self._check_error("if 0: return", "outside function") 2352 self._check_error("if 0: return\nelse: x=1", "outside function") 2353 self._check_error("if 1: pass\nelse: return", "outside function") 2354 self._check_error("while 0: return", "outside function") 2355 self._check_error("class C:\n if 0: return", "outside function") 2356 self._check_error("class C:\n while 0: return", "outside function") 2357 self._check_error("class C:\n while 0: return\n else: x=1", 2358 "outside function") 2359 self._check_error("class C:\n if 0: return\n else: x= 1", 2360 "outside function") 2361 self._check_error("class C:\n if 1: pass\n else: return", 2362 "outside function") 2363 2364 def test_break_outside_loop(self): 2365 msg = "outside loop" 2366 self._check_error("break", msg, lineno=1) 2367 self._check_error("if 0: break", msg, lineno=1) 2368 self._check_error("if 0: break\nelse: x=1", msg, lineno=1) 2369 self._check_error("if 1: pass\nelse: break", msg, lineno=2) 2370 self._check_error("class C:\n if 0: break", msg, lineno=2) 2371 self._check_error("class C:\n if 1: pass\n else: break", 2372 msg, lineno=3) 2373 self._check_error("with object() as obj:\n break", 2374 msg, lineno=2) 2375 2376 def test_continue_outside_loop(self): 2377 msg = "not properly in loop" 2378 self._check_error("if 0: continue", msg, lineno=1) 2379 self._check_error("if 0: continue\nelse: x=1", msg, lineno=1) 2380 self._check_error("if 1: pass\nelse: continue", msg, lineno=2) 2381 self._check_error("class C:\n if 0: continue", msg, lineno=2) 2382 self._check_error("class C:\n if 1: pass\n else: continue", 2383 msg, lineno=3) 2384 self._check_error("with object() as obj:\n continue", 2385 msg, lineno=2) 2386 2387 def test_unexpected_indent(self): 2388 self._check_error("foo()\n bar()\n", "unexpected indent", 2389 subclass=IndentationError) 2390 2391 def test_no_indent(self): 2392 self._check_error("if 1:\nfoo()", "expected an indented block", 2393 subclass=IndentationError) 2394 2395 def test_bad_outdent(self): 2396 self._check_error("if 1:\n foo()\n bar()", 2397 "unindent does not match .* level", 2398 subclass=IndentationError) 2399 2400 def test_kwargs_last(self): 2401 self._check_error("int(base=10, '2')", 2402 "positional argument follows keyword argument") 2403 2404 def test_kwargs_last2(self): 2405 self._check_error("int(**{'base': 10}, '2')", 2406 "positional argument follows " 2407 "keyword argument unpacking") 2408 2409 def test_kwargs_last3(self): 2410 self._check_error("int(**{'base': 10}, *['2'])", 2411 "iterable argument unpacking follows " 2412 "keyword argument unpacking") 2413 2414 def test_generator_in_function_call(self): 2415 self._check_error("foo(x, y for y in range(3) for z in range(2) if z , p)", 2416 "Generator expression must be parenthesized", 2417 lineno=1, end_lineno=1, offset=11, end_offset=53) 2418 2419 def test_except_then_except_star(self): 2420 self._check_error("try: pass\nexcept ValueError: pass\nexcept* TypeError: pass", 2421 r"cannot have both 'except' and 'except\*' on the same 'try'", 2422 lineno=3, end_lineno=3, offset=1, end_offset=8) 2423 2424 def test_except_star_then_except(self): 2425 self._check_error("try: pass\nexcept* ValueError: pass\nexcept TypeError: pass", 2426 r"cannot have both 'except' and 'except\*' on the same 'try'", 2427 lineno=3, end_lineno=3, offset=1, end_offset=7) 2428 2429 def test_empty_line_after_linecont(self): 2430 # See issue-40847 2431 s = r"""\ 2432pass 2433 \ 2434 2435pass 2436""" 2437 try: 2438 compile(s, '<string>', 'exec') 2439 except SyntaxError: 2440 self.fail("Empty line after a line continuation character is valid.") 2441 2442 # See issue-46091 2443 s1 = r"""\ 2444def fib(n): 2445 \ 2446'''Print a Fibonacci series up to n.''' 2447 \ 2448a, b = 0, 1 2449""" 2450 s2 = r"""\ 2451def fib(n): 2452 '''Print a Fibonacci series up to n.''' 2453 a, b = 0, 1 2454""" 2455 try: 2456 compile(s1, '<string>', 'exec') 2457 compile(s2, '<string>', 'exec') 2458 except SyntaxError: 2459 self.fail("Indented statement over multiple lines is valid") 2460 2461 def test_continuation_bad_indentation(self): 2462 # Check that code that breaks indentation across multiple lines raises a syntax error 2463 2464 code = r"""\ 2465if x: 2466 y = 1 2467 \ 2468 foo = 1 2469 """ 2470 2471 self.assertRaises(IndentationError, exec, code) 2472 2473 @support.cpython_only 2474 def test_nested_named_except_blocks(self): 2475 code = "" 2476 for i in range(12): 2477 code += f"{' '*i}try:\n" 2478 code += f"{' '*(i+1)}raise Exception\n" 2479 code += f"{' '*i}except Exception as e:\n" 2480 code += f"{' '*4*12}pass" 2481 self._check_error(code, "too many statically nested blocks") 2482 2483 @support.cpython_only 2484 def test_with_statement_many_context_managers(self): 2485 # See gh-113297 2486 2487 def get_code(n): 2488 code = textwrap.dedent(""" 2489 def bug(): 2490 with ( 2491 a 2492 """) 2493 for i in range(n): 2494 code += f" as a{i}, a\n" 2495 code += "): yield a" 2496 return code 2497 2498 CO_MAXBLOCKS = 21 # static nesting limit of the compiler 2499 MAX_MANAGERS = CO_MAXBLOCKS - 1 # One for the StopIteration block 2500 2501 for n in range(MAX_MANAGERS): 2502 with self.subTest(f"within range: {n=}"): 2503 compile(get_code(n), "<string>", "exec") 2504 2505 for n in range(MAX_MANAGERS, MAX_MANAGERS + 5): 2506 with self.subTest(f"out of range: {n=}"): 2507 self._check_error(get_code(n), "too many statically nested blocks") 2508 2509 @support.cpython_only 2510 def test_async_with_statement_many_context_managers(self): 2511 # See gh-116767 2512 2513 def get_code(n): 2514 code = [ textwrap.dedent(""" 2515 async def bug(): 2516 async with ( 2517 a 2518 """) ] 2519 for i in range(n): 2520 code.append(f" as a{i}, a\n") 2521 code.append("): yield a") 2522 return "".join(code) 2523 2524 CO_MAXBLOCKS = 21 # static nesting limit of the compiler 2525 MAX_MANAGERS = CO_MAXBLOCKS - 1 # One for the StopIteration block 2526 2527 for n in range(MAX_MANAGERS): 2528 with self.subTest(f"within range: {n=}"): 2529 compile(get_code(n), "<string>", "exec") 2530 2531 for n in range(MAX_MANAGERS, MAX_MANAGERS + 5): 2532 with self.subTest(f"out of range: {n=}"): 2533 self._check_error(get_code(n), "too many statically nested blocks") 2534 2535 def test_barry_as_flufl_with_syntax_errors(self): 2536 # The "barry_as_flufl" rule can produce some "bugs-at-a-distance" if 2537 # is reading the wrong token in the presence of syntax errors later 2538 # in the file. See bpo-42214 for more information. 2539 code = """ 2540def func1(): 2541 if a != b: 2542 raise ValueError 2543 2544def func2(): 2545 try 2546 return 1 2547 finally: 2548 pass 2549""" 2550 self._check_error(code, "expected ':'") 2551 2552 def test_invalid_line_continuation_error_position(self): 2553 self._check_error(r"a = 3 \ 4", 2554 "unexpected character after line continuation character", 2555 lineno=1, offset=8) 2556 self._check_error('1,\\#\n2', 2557 "unexpected character after line continuation character", 2558 lineno=1, offset=4) 2559 self._check_error('\nfgdfgf\n1,\\#\n2\n', 2560 "unexpected character after line continuation character", 2561 lineno=3, offset=4) 2562 2563 def test_invalid_line_continuation_left_recursive(self): 2564 # Check bpo-42218: SyntaxErrors following left-recursive rules 2565 # (t_primary_raw in this case) need to be tested explicitly 2566 self._check_error("A.\u018a\\ ", 2567 "unexpected character after line continuation character") 2568 self._check_error("A.\u03bc\\\n", 2569 "unexpected EOF while parsing") 2570 2571 def test_error_parenthesis(self): 2572 for paren in "([{": 2573 self._check_error(paren + "1 + 2", f"\\{paren}' was never closed") 2574 2575 for paren in "([{": 2576 self._check_error(f"a = {paren} 1, 2, 3\nb=3", f"\\{paren}' was never closed") 2577 2578 for paren in ")]}": 2579 self._check_error(paren + "1 + 2", f"unmatched '\\{paren}'") 2580 2581 # Some more complex examples: 2582 code = """\ 2583func( 2584 a=["unclosed], # Need a quote in this comment: " 2585 b=2, 2586) 2587""" 2588 self._check_error(code, "parenthesis '\\)' does not match opening parenthesis '\\['") 2589 2590 self._check_error("match y:\n case e(e=v,v,", " was never closed") 2591 2592 # Examples with dencodings 2593 s = b'# coding=latin\n(aaaaaaaaaaaaaaaaa\naaaaaaaaaaa\xb5' 2594 self._check_error(s, r"'\(' was never closed") 2595 2596 def test_error_string_literal(self): 2597 2598 self._check_error("'blech", r"unterminated string literal \(.*\)$") 2599 self._check_error('"blech', r"unterminated string literal \(.*\)$") 2600 self._check_error( 2601 r'"blech\"', r"unterminated string literal \(.*\); perhaps you escaped the end quote" 2602 ) 2603 self._check_error( 2604 r'r"blech\"', r"unterminated string literal \(.*\); perhaps you escaped the end quote" 2605 ) 2606 self._check_error("'''blech", "unterminated triple-quoted string literal") 2607 self._check_error('"""blech', "unterminated triple-quoted string literal") 2608 2609 def test_invisible_characters(self): 2610 self._check_error('print\x17("Hello")', "invalid non-printable character") 2611 self._check_error(b"with(0,,):\n\x01", "invalid non-printable character") 2612 2613 def test_match_call_does_not_raise_syntax_error(self): 2614 code = """ 2615def match(x): 2616 return 1+1 2617 2618match(34) 2619""" 2620 compile(code, "<string>", "exec") 2621 2622 def test_case_call_does_not_raise_syntax_error(self): 2623 code = """ 2624def case(x): 2625 return 1+1 2626 2627case(34) 2628""" 2629 compile(code, "<string>", "exec") 2630 2631 def test_multiline_compiler_error_points_to_the_end(self): 2632 self._check_error( 2633 "call(\na=1,\na=1\n)", 2634 "keyword argument repeated", 2635 lineno=3 2636 ) 2637 2638 @support.cpython_only 2639 def test_syntax_error_on_deeply_nested_blocks(self): 2640 # This raises a SyntaxError, it used to raise a SystemError. Context 2641 # for this change can be found on issue #27514 2642 2643 # In 2.5 there was a missing exception and an assert was triggered in a 2644 # debug build. The number of blocks must be greater than CO_MAXBLOCKS. 2645 # SF #1565514 2646 2647 source = """ 2648while 1: 2649 while 2: 2650 while 3: 2651 while 4: 2652 while 5: 2653 while 6: 2654 while 8: 2655 while 9: 2656 while 10: 2657 while 11: 2658 while 12: 2659 while 13: 2660 while 14: 2661 while 15: 2662 while 16: 2663 while 17: 2664 while 18: 2665 while 19: 2666 while 20: 2667 while 21: 2668 while 22: 2669 while 23: 2670 break 2671""" 2672 self._check_error(source, "too many statically nested blocks") 2673 2674 @support.cpython_only 2675 def test_error_on_parser_stack_overflow(self): 2676 source = "-" * 100000 + "4" 2677 for mode in ["exec", "eval", "single"]: 2678 with self.subTest(mode=mode): 2679 with self.assertRaisesRegex(MemoryError, r"too complex"): 2680 compile(source, "<string>", mode) 2681 2682 @support.cpython_only 2683 def test_deep_invalid_rule(self): 2684 # Check that a very deep invalid rule in the PEG 2685 # parser doesn't have exponential backtracking. 2686 source = "d{{{{{{{{{{{{{{{{{{{{{{{{{```{{{{{{{ef f():y" 2687 with self.assertRaises(SyntaxError): 2688 compile(source, "<string>", "exec") 2689 2690 2691def load_tests(loader, tests, pattern): 2692 tests.addTest(doctest.DocTestSuite()) 2693 return tests 2694 2695 2696if __name__ == "__main__": 2697 unittest.main() 2698