1-- Test cases for exporting node types from the type checker. 2-- 3-- Each test case consists of at least two sections. 4-- The first section contains [case NAME] followed by the input code, 5-- while the second section contains [out] followed by the output from the type 6-- checker. 7-- 8-- The first line of input code should be a regexp in comment that describes 9-- the information to dump (prefix with ##). The regexp is matched against 10-- the following items: 11-- 12-- * each name of an expression node 13-- * each type string of a node (e.g. OpExpr) 14-- 15-- Lines starting with "--" in this file will be ignored. 16 17 18-- Expressions 19-- ----------- 20 21 22[case testConstructorCall] 23import typing 24A() 25B() 26class A: pass 27class B: pass 28[out] 29CallExpr(2) : A 30NameExpr(2) : def () -> A 31CallExpr(3) : B 32NameExpr(3) : def () -> B 33 34[case testLiterals] 35import typing 365 372.3 38'foo' 39[builtins fixtures/primitives.pyi] 40[out] 41IntExpr(2) : Literal[5]? 42FloatExpr(3) : builtins.float 43StrExpr(4) : Literal['foo']? 44 45[case testNameExpression] 46 47a = None # type: A 48a # node 49def f(aa: 'A') -> None: 50 b = None # type: B 51 aa # node 52 b # node 53class A: 54 def g(self) -> None: 55 self # node 56class B: pass 57[out] 58NameExpr(3) : A 59NameExpr(6) : A 60NameExpr(7) : B 61NameExpr(10) : A 62 63[case testEllipsis] 64import typing 65... 66[out] 67EllipsisExpr(2) : builtins.ellipsis 68 69[case testMemberAccess] 70## MemberExpr|CallExpr 71 72a = None # type: A 73a.m 74a.f 75a.f() 76class A: 77 m = None # type: A 78 def f(self) -> 'B': pass 79class B: pass 80[out] 81MemberExpr(4) : A 82MemberExpr(5) : def () -> B 83CallExpr(6) : B 84MemberExpr(6) : def () -> B 85 86[case testCastExpression] 87## CastExpr|[a-z] 88from typing import Any, cast 89d = None # type: Any 90b = None # type: B 91class A: pass 92class B(A): pass 93cast(A, d) 94cast(A, b) 95cast(B, b) 96[out] 97CastExpr(7) : A 98NameExpr(7) : Any 99CastExpr(8) : A 100NameExpr(8) : B 101CastExpr(9) : B 102NameExpr(9) : B 103 104[case testArithmeticOps] 105## OpExpr 106import typing 107a = 1 + 2 1081.2 * 3 1092.2 - 3 1101 / 2 111[file builtins.py] 112class object: 113 def __init__(self) -> None: pass 114class function: pass 115class int: 116 def __add__(self, x: int) -> int: pass 117 def __truediv__(self, x: int) -> float: pass 118class float: 119 def __mul__(self, x: int) -> float: pass 120 def __sub__(self, x: int) -> float: pass 121class type: pass 122class str: pass 123[out] 124OpExpr(3) : builtins.int 125OpExpr(4) : builtins.float 126OpExpr(5) : builtins.float 127OpExpr(6) : builtins.float 128 129[case testComparisonOps] 130## ComparisonExpr 131import typing 1321 == object() 1331 == 2 1342 < 3 1351 < 2 < 3 1368 > 3 1374 < 6 > 2 138[file builtins.py] 139class object: 140 def __init__(self) -> None: pass 141class int: 142 def __eq__(self, x: object) -> bool: pass 143 def __lt__(self, x: int) -> bool: pass 144 def __gt__(self, x: int) -> int: pass 145class bool: pass 146class type: pass 147class function: pass 148class str: pass 149[out] 150ComparisonExpr(3) : builtins.bool 151ComparisonExpr(4) : builtins.bool 152ComparisonExpr(5) : builtins.bool 153ComparisonExpr(6) : builtins.bool 154ComparisonExpr(7) : builtins.int 155ComparisonExpr(8) : builtins.object 156 157[case testBooleanOps] 158## OpExpr|UnaryExpr 159import typing 160a = 1 161a and a 162a or a 163not a 164[builtins fixtures/bool.pyi] 165[out] 166OpExpr(4) : builtins.int 167OpExpr(5) : builtins.int 168UnaryExpr(6) : builtins.bool 169 170[case testBooleanOpsOnBools] 171## OpExpr|UnaryExpr 172import typing 173a = bool() 174a and a 175a or a 176not a 177[builtins fixtures/bool.pyi] 178[out] 179OpExpr(4) : builtins.bool 180OpExpr(5) : builtins.bool 181UnaryExpr(6) : builtins.bool 182 183[case testFunctionCall] 184## CallExpr 185from typing import Tuple 186f( 187 A(), 188 B()) 189class A: pass 190class B: pass 191def f(a: A, b: B) -> Tuple[A, B]: pass 192[builtins fixtures/tuple-simple.pyi] 193[out] 194CallExpr(3) : Tuple[A, B] 195CallExpr(4) : A 196CallExpr(5) : B 197 198 199-- Statements 200-- ---------- 201 202 203[case testSimpleAssignment] 204from typing import Any 205a = None # type: A 206b = a # type: Any 207if b: 208 b = a 209 a = b 210 211class A: pass 212[out] 213NameExpr(3) : A 214NameExpr(4) : Any 215NameExpr(5) : A 216NameExpr(5) : Any 217NameExpr(6) : A 218NameExpr(6) : Any 219 220[case testMemberAssignment] 221from typing import Any 222class A: 223 a = None # type: A 224 b = None # type: Any 225 def f(self) -> None: 226 self.b = self.a 227 self.a.a = self.b 228[out] 229MemberExpr(6) : A 230MemberExpr(6) : Any 231NameExpr(6) : A 232NameExpr(6) : A 233MemberExpr(7) : A 234MemberExpr(7) : A 235MemberExpr(7) : A 236NameExpr(7) : A 237NameExpr(7) : A 238 239[case testIf] 240 241a = None # type: bool 242if a: 243 1 244elif not a: 245 1 246[builtins fixtures/bool.pyi] 247[out] 248NameExpr(3) : builtins.bool 249IntExpr(4) : Literal[1]? 250NameExpr(5) : builtins.bool 251UnaryExpr(5) : builtins.bool 252IntExpr(6) : Literal[1]? 253 254[case testWhile] 255 256a = None # type: bool 257while a: 258 a 259[builtins fixtures/bool.pyi] 260[out] 261NameExpr(3) : builtins.bool 262NameExpr(4) : builtins.bool 263 264 265-- Simple type inference 266-- --------------------- 267 268 269[case testInferSingleType] 270import typing 271x = () 272[builtins fixtures/primitives.pyi] 273[out] 274NameExpr(2) : Tuple[] 275TupleExpr(2) : Tuple[] 276 277[case testInferTwoTypes] 278## NameExpr 279import typing 280(s, 281i) = 'x', 1 282[builtins fixtures/primitives.pyi] 283[out] 284NameExpr(3) : builtins.str 285NameExpr(4) : builtins.int 286 287[case testInferSingleLocalVarType] 288import typing 289def f() -> None: 290 x = () 291[builtins fixtures/primitives.pyi] 292[out] 293NameExpr(3) : Tuple[] 294TupleExpr(3) : Tuple[] 295 296 297-- Basic generics 298-- -------------- 299 300 301[case testImplicitBoundTypeVarsForMethod] 302## MemberExpr 303from typing import TypeVar, Generic 304T = TypeVar('T') 305class A(Generic[T]): 306 def f(self) -> T: pass 307class B: pass 308def g() -> None: 309 a = None # type: A[B] 310 f = a.f 311[out] 312MemberExpr(9) : def () -> B 313 314[case testImplicitBoundTypeVarsForSelfMethodReference] 315from typing import TypeVar, Generic 316T = TypeVar('T') 317class A(Generic[T]): 318 def f(self) -> T: 319 return self.f() 320[out] 321CallExpr(5) : T`1 322MemberExpr(5) : def () -> T`1 323NameExpr(5) : A[T`1] 324 325[case testGenericFunctionCallWithTypeApp-skip] 326## CallExpr|TypeApplication|NameExpr 327from typing import Any, TypeVar, Tuple 328T = TypeVar('T') 329class A: pass 330f[A](A()) 331f[Any](A()) 332def f(a: T) -> Tuple[T, T]: pass 333[builtins fixtures/tuple.pyi] 334[out] 335CallExpr(5) : A 336CallExpr(5) : Tuple[A, A] 337NameExpr(5) : def () -> A 338NameExpr(5) : def (a: A) -> Tuple[A, A] 339TypeApplication(5) : def (a: A) -> Tuple[A, A] 340CallExpr(6) : A 341CallExpr(6) : Tuple[Any, Any] 342NameExpr(6) : def () -> A 343NameExpr(6) : def (a: Any) -> Tuple[Any, Any] 344TypeApplication(6) : def (a: Any) -> Tuple[Any, Any] 345 346-- NOTE: Type applications are not supported for generic methods, so the 347-- following test cases are commented out. 348 349--[case testGenericMethodCallWithTypeApp] 350--## CallExpr|MemberExpr|TypeApplication 351--from typing import Any, TypeVar, Tuple 352--T = TypeVar('T') 353--class A: 354-- def f(self, a: T) -> Tuple[T, T]: pass 355--a.f[A](a) 356--a.f[Any](a) 357--a = None # type: A 358--[builtins fixtures/tuple.py] 359--[out] 360--CallExpr(2) : Tuple[A, A] 361--MemberExpr(2) : def (A a) -> Tuple[A, A] 362--TypeApplication(2) : def (A a) -> Tuple[A, A] 363--CallExpr(3) : Tuple[Any, Any] 364--MemberExpr(3) : def (any a) -> Tuple[Any, Any] 365--TypeApplication(3) : def (any a) -> Tuple[Any, Any] 366 367--[case testGenericMethodCallInGenericTypeWithTypeApp] 368--## CallExpr|MemberExpr|TypeApplication 369--from typing import Any, TypeVar, Generic, Tuple 370--T = TypeVar('T') 371--S = TypeVar('S') 372--class B: pass 373--class C: pass 374--a.f[B](b) 375--a.f[Any](b) 376--class A(Generic[T]): 377-- def f(self, a: S) -> Tuple[T, S]: pass 378--a = None # type: A[C] 379--b = None # type: B 380--[builtins fixtures/tuple.py] 381--[out] 382--CallExpr(6) : Tuple[C, B] 383--MemberExpr(6) : def (B a) -> Tuple[C, B] 384--TypeApplication(6) : def (B a) -> Tuple[C, B] 385--CallExpr(7) : Tuple[C, Any] 386--MemberExpr(7) : def (any a) -> Tuple[C, Any] 387--TypeApplication(7) : def (any a) -> Tuple[C, Any] 388 389[case testGenericTypeVariableInference] 390from typing import TypeVar, Generic 391T = TypeVar('T') 392class A(Generic[T]): 393 def __init__(self, a: T) -> None: pass 394class B: pass 395A(A(B())) 396[out] 397CallExpr(6) : A[A[B]] 398CallExpr(6) : A[B] 399CallExpr(6) : B 400NameExpr(6) : def (a: A[B]) -> A[A[B]] 401NameExpr(6) : def (a: B) -> A[B] 402NameExpr(6) : def () -> B 403 404 405-- Generic inheritance 406-- ------------------- 407 408 409[case testInheritedMethodReferenceWithGenericInheritance] 410from typing import TypeVar, Generic 411T = TypeVar('T') 412class C: pass 413class A(Generic[T]): 414 def f(self, a: T) -> None: pass 415class B(A[C]): 416 def g(self, c: C) -> None: 417 self.f(c) 418[out] 419CallExpr(8) : None 420MemberExpr(8) : def (a: C) 421NameExpr(8) : C 422NameExpr(8) : B 423 424[case testInheritedMethodReferenceWithGenericSubclass] 425from typing import TypeVar, Generic 426S = TypeVar('S') 427T = TypeVar('T') 428class C: pass 429class A(Generic[S, T]): 430 def f(self, a: C) -> None: pass 431class B(A[C, T], Generic[T]): 432 def g(self, c: C) -> None: 433 self.f(c) 434[out] 435CallExpr(9) : None 436MemberExpr(9) : def (a: C) 437NameExpr(9) : C 438NameExpr(9) : B[T`1] 439 440[case testExternalReferenceWithGenericInheritance] 441from typing import TypeVar, Generic 442T = TypeVar('T') 443class C: pass 444class A(Generic[T]): 445 def f(self, a: T) -> None: pass 446class B(A[C]): pass 447b = None # type: B 448c = None # type: C 449b.f(c) 450[out] 451CallExpr(9) : None 452MemberExpr(9) : def (a: C) 453NameExpr(9) : B 454NameExpr(9) : C 455 456 457-- Implicit Any types 458-- ------------------ 459 460 461[case testDynamicallyTypedFunction] 462 463def f(x): 464 y = x + o 465 z = o 466 z 467o = None # type: object 468[out] 469NameExpr(3) : builtins.object 470NameExpr(3) : Any 471NameExpr(3) : Any 472OpExpr(3) : Any 473NameExpr(4) : builtins.object 474NameExpr(4) : Any 475NameExpr(5) : Any 476 477[case testDynamicallyTypedMethod] 478 479class A: 480 def f(self, x): 481 y = ( 482 o) # Place y and o on separate lines 483 x 484 y 485o = None # type: object 486[out] 487NameExpr(4) : Any 488NameExpr(5) : builtins.object 489NameExpr(6) : Any 490NameExpr(7) : Any 491 492[case testDynamicallyTypedConstructor] 493 494class A: 495 def __init__(self, x): 496 y = o 497 x 498 y 499o = None # type: object 500[out] 501NameExpr(4) : builtins.object 502NameExpr(4) : Any 503NameExpr(5) : Any 504NameExpr(6) : Any 505 506[case testCallInDynamicallyTypedFunction] 507 508def f(): 509 g(o) 510def g(a: object) -> object: pass 511o = None # type: object 512[out] 513CallExpr(3) : Any 514NameExpr(3) : def (a: builtins.object) -> builtins.object 515NameExpr(3) : builtins.object 516 517[case testExpressionInDynamicallyTypedFn] 518import typing 519def f(): 520 x = None 521 x.f() 522[out] 523CallExpr(4) : Any 524MemberExpr(4) : Any 525NameExpr(4) : Any 526 527[case testGenericCall] 528from typing import TypeVar, Generic 529T = TypeVar('T') 530def f() -> None: 531 a1 = A(b) # type: A[B] 532 a2 = A(b) # type: A[object] 533class A(Generic[T]): 534 def __init__(self, a: T) -> None: pass 535class B: pass 536b = None # type: B 537[out] 538CallExpr(4) : A[B] 539NameExpr(4) : def (a: B) -> A[B] 540NameExpr(4) : B 541CallExpr(5) : A[builtins.object] 542NameExpr(5) : def (a: builtins.object) -> A[builtins.object] 543NameExpr(5) : B 544 545[case testGenericCallInDynamicallyTypedFunction] 546from typing import TypeVar, Generic 547T = TypeVar('T') 548def f(): 549 A() 550class A(Generic[T]): pass 551[out] 552CallExpr(4) : Any 553NameExpr(4) : def [T] () -> A[T`1] 554 555[case testGenericCallInDynamicallyTypedFunction2] 556from typing import TypeVar, Generic 557T = TypeVar('T') 558def f(): 559 A(f) 560class A(Generic[T]): 561 def __init__(self, x: T) -> None: pass 562[out] 563CallExpr(4) : Any 564NameExpr(4) : def [T] (x: T`1) -> A[T`1] 565NameExpr(4) : def () -> Any 566 567[case testGenericCallInDynamicallyTypedFunction3] 568from typing import TypeVar 569t = TypeVar('t') 570def f(): 571 g(None) 572def g(x: t) -> t: pass 573[out] 574CallExpr(4) : Any 575NameExpr(4) : def [t] (x: t`-1) -> t`-1 576 577 578-- Generic types and type inference 579-- -------------------------------- 580 581 582[case testInferenceInArgumentContext] 583## CallExpr 584from typing import TypeVar, Generic 585T = TypeVar('T') 586f(g()) 587f(h(b)) 588f(h(c)) 589 590b = None # type: B 591c = None # type: C 592 593def f(a: 'A[B]') -> None: pass 594 595def g() -> 'A[T]': pass 596def h(a: T) -> 'A[T]': pass 597 598class A(Generic[T]): pass 599class B: pass 600class C(B): pass 601[out] 602CallExpr(4) : None 603CallExpr(4) : A[B] 604CallExpr(5) : None 605CallExpr(5) : A[B] 606CallExpr(6) : None 607CallExpr(6) : A[B] 608 609[case testInferGenericTypeForLocalVariable] 610from typing import TypeVar, Generic 611T = TypeVar('T') 612def f() -> None: 613 a = A(b) 614 a 615 a2, a3 = A(b), A(c) 616 a2 617 a3 618b = None # type: B 619c = None # type: C 620class A(Generic[T]): 621 def __init__(self, x: T) -> None: pass 622class B: pass 623class C: pass 624[out] 625CallExpr(4) : A[B] 626NameExpr(4) : def (x: B) -> A[B] 627NameExpr(4) : A[B] 628NameExpr(4) : B 629NameExpr(5) : A[B] 630CallExpr(6) : A[B] 631CallExpr(6) : A[C] 632NameExpr(6) : def (x: B) -> A[B] 633NameExpr(6) : def (x: C) -> A[C] 634NameExpr(6) : A[B] 635NameExpr(6) : A[C] 636NameExpr(6) : B 637NameExpr(6) : C 638NameExpr(7) : A[B] 639NameExpr(8) : A[C] 640 641[case testNestedGenericCalls] 642from typing import TypeVar, Generic 643T = TypeVar('T') 644S = TypeVar('S') 645def h() -> None: 646 g(f(c)) 647 648c = None # type: C 649 650class A(Generic[T]): pass 651class B(Generic[T]): pass 652class C: pass 653def f(a: T) -> A[T]: pass 654def g(a: S) -> B[S]: pass 655[out] 656CallExpr(5) : A[C] 657CallExpr(5) : B[A[C]] 658NameExpr(5) : C 659NameExpr(5) : def (a: C) -> A[C] 660NameExpr(5) : def (a: A[C]) -> B[A[C]] 661 662[case testInferListLiterals] 663from typing import List 664a = [] # type: List[A] 665class A: pass 666[builtins fixtures/list.pyi] 667[out] 668ListExpr(2) : builtins.list[A] 669 670[case testInferGenericTypeInTypeAnyContext] 671from typing import Any 672a = [] # type: Any 673[builtins fixtures/list.pyi] 674[out] 675ListExpr(2) : builtins.list[Any] 676 677[case testHigherOrderFunction] 678from typing import TypeVar, Callable, List 679t = TypeVar('t') 680s = TypeVar('s') 681map( 682 f, 683 [A()]) 684def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass 685class A: pass 686class B: pass 687def f(a: A) -> B: pass 688[builtins fixtures/list.pyi] 689[out] 690CallExpr(4) : builtins.list[B] 691NameExpr(4) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B] 692NameExpr(5) : def (a: A) -> B 693CallExpr(6) : A 694ListExpr(6) : builtins.list[A] 695NameExpr(6) : def () -> A 696 697 698-- Lambdas 699-- ------- 700 701 702[case testLambdaWithTypeInferredFromContext] 703from typing import Callable 704f = lambda x: x.a # type: Callable[[B], A] 705class A: pass 706class B: 707 a = None # type: A 708[out] 709LambdaExpr(2) : def (B) -> A 710MemberExpr(2) : A 711NameExpr(2) : B 712 713[case testLambdaWithInferredType] 714## LambdaExpr|NameExpr 715import typing 716f = lambda: 1 717[out] 718LambdaExpr(3) : def () -> Literal[1]? 719NameExpr(3) : def () -> builtins.int 720 721[case testLambdaWithInferredType2] 722## LambdaExpr|NameExpr 723import typing 724f = lambda: [1] 725[builtins fixtures/list.pyi] 726[out] 727LambdaExpr(3) : def () -> builtins.list[builtins.int] 728NameExpr(3) : def () -> builtins.list[builtins.int] 729 730[case testLambdaWithInferredType2] 731from typing import List, Callable 732f = lambda x: [] # type: Callable[[B], List[A]] 733class A: pass 734class B: 735 a = None # type: A 736[builtins fixtures/list.pyi] 737[out] 738LambdaExpr(2) : def (B) -> builtins.list[A] 739ListExpr(2) : builtins.list[A] 740 741[case testLambdaAndHigherOrderFunction] 742from typing import TypeVar, Callable, List 743t = TypeVar('t') 744s = TypeVar('s') 745l = None # type: List[A] 746map( 747 lambda x: f(x), l) 748def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass 749class A: pass 750class B: pass 751def f(a: A) -> B: pass 752[builtins fixtures/list.pyi] 753[out] 754CallExpr(5) : builtins.list[B] 755NameExpr(5) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B] 756CallExpr(6) : B 757LambdaExpr(6) : def (A) -> B 758NameExpr(6) : def (a: A) -> B 759NameExpr(6) : builtins.list[A] 760NameExpr(6) : A 761 762[case testLambdaAndHigherOrderFunction2] 763## LambdaExpr|NameExpr|ListExpr 764from typing import TypeVar, List, Callable 765t = TypeVar('t') 766s = TypeVar('s') 767l = None # type: List[A] 768map( 769 lambda x: [f(x)], l) 770def map(f: Callable[[t], List[s]], a: List[t]) -> List[s]: pass 771class A: pass 772class B: pass 773def f(a: A) -> B: pass 774[builtins fixtures/list.pyi] 775[out] 776NameExpr(6) : def (f: def (A) -> builtins.list[B], a: builtins.list[A]) -> builtins.list[B] 777LambdaExpr(7) : def (A) -> builtins.list[B] 778ListExpr(7) : builtins.list[B] 779NameExpr(7) : def (a: A) -> B 780NameExpr(7) : builtins.list[A] 781NameExpr(7) : A 782 783[case testLambdaInListAndHigherOrderFunction] 784from typing import TypeVar, Callable, List 785t = TypeVar('t') 786s = TypeVar('s') 787l = None # type: List[A] 788map( 789 [lambda x: x], 790 l) 791def map(f: List[Callable[[t], s]], a: List[t]) -> List[s]: pass 792class A: pass 793[builtins fixtures/list.pyi] 794[out] 795-- TODO We probably should not silently infer 'Any' types in statically typed 796-- context. Perhaps just fail instead? 797CallExpr(5) : builtins.list[Any] 798NameExpr(5) : def (f: builtins.list[def (A) -> Any], a: builtins.list[A]) -> builtins.list[Any] 799LambdaExpr(6) : def (A) -> A 800ListExpr(6) : builtins.list[def (A) -> Any] 801NameExpr(6) : A 802NameExpr(7) : builtins.list[A] 803 804[case testLambdaAndHigherOrderFunction3] 805from typing import TypeVar, Callable, List 806t = TypeVar('t') 807s = TypeVar('s') 808l = None # type: List[A] 809map( 810 lambda x: x.b, 811 l) 812def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass 813class A: 814 b = None # type: B 815class B: pass 816[builtins fixtures/list.pyi] 817[out] 818CallExpr(5) : builtins.list[B] 819NameExpr(5) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B] 820LambdaExpr(6) : def (A) -> B 821MemberExpr(6) : B 822NameExpr(6) : A 823NameExpr(7) : builtins.list[A] 824 825[case testLambdaAndHigherOrderFunctionAndKeywordArgs] 826from typing import TypeVar, Callable, List 827t = TypeVar('t') 828s = TypeVar('s') 829l = None # type: List[A] 830map( 831 a=l, 832 f=lambda x: x.b) 833def map(f: Callable[[t], s], a: List[t]) -> List[s]: pass 834class A: 835 b = None # type: B 836class B: pass 837[builtins fixtures/list.pyi] 838[out] 839CallExpr(5) : builtins.list[B] 840NameExpr(5) : def (f: def (A) -> B, a: builtins.list[A]) -> builtins.list[B] 841NameExpr(6) : builtins.list[A] 842LambdaExpr(7) : def (A) -> B 843MemberExpr(7) : B 844NameExpr(7) : A 845 846 847-- Boolean operations 848-- ------------------ 849 850 851[case testBooleanOr] 852from typing import List 853a = None # type: List[A] 854a or [] 855a = a or [] 856if int(): 857 a = [] or a 858class A: pass 859[builtins fixtures/list.pyi] 860[out] 861ListExpr(3) : builtins.list[A] 862NameExpr(3) : builtins.list[A] 863OpExpr(3) : builtins.list[A] 864ListExpr(4) : builtins.list[A] 865NameExpr(4) : builtins.list[A] 866NameExpr(4) : builtins.list[A] 867OpExpr(4) : builtins.list[A] 868CallExpr(5) : builtins.int 869NameExpr(5) : def () -> builtins.int 870ListExpr(6) : builtins.list[A] 871NameExpr(6) : builtins.list[A] 872NameExpr(6) : builtins.list[A] 873OpExpr(6) : builtins.list[A] 874 875 876-- Class attributes 877-- ---------------- 878 879 880[case testUnboundMethod] 881## MemberExpr 882import typing 883class A: 884 def f(self) -> None: pass 885A.f 886[out] 887MemberExpr(5) : def (self: A) 888 889[case testUnboundMethodWithImplicitSig] 890## MemberExpr 891import typing 892class A: 893 def f(self): pass 894A.f 895[out] 896MemberExpr(5) : def (self: A) -> Any 897 898[case testOverloadedUnboundMethod] 899## MemberExpr 900from typing import overload 901class A: 902 @overload 903 def f(self) -> None: pass 904 @overload 905 def f(self, __x: object) -> None: pass 906 907 def f(self, *args) -> None: pass 908A.f 909[builtins fixtures/tuple.pyi] 910[out] 911MemberExpr(10) : Overload(def (self: A), def (self: A, builtins.object)) 912 913[case testOverloadedUnboundMethodWithImplicitSig] 914## MemberExpr 915from typing import overload 916class A: 917 @overload 918 def f(self): pass 919 @overload 920 def f(self, __x): pass 921 922 def f(self, *args): pass 923A.f 924[builtins fixtures/tuple.pyi] 925[out] 926MemberExpr(10) : Overload(def (self: A) -> Any, def (self: A, Any) -> Any) 927 928[case testUnboundMethodWithInheritance] 929## MemberExpr 930import typing 931class A: 932 def __init__(self) -> None: pass 933 def f(self) -> None: pass 934class B(A): 935 pass 936B.f 937[out] 938MemberExpr(8) : def (self: A) 939 940[case testUnboundGenericMethod] 941## MemberExpr 942from typing import TypeVar 943t = TypeVar('t') 944class B: pass 945class A: 946 def f(self, x: t) -> None: pass 947A.f(A(), B()) 948[out] 949MemberExpr(7) : def (self: A, x: B) 950 951[case testUnboundMethodOfGenericClass] 952## MemberExpr 953from typing import TypeVar, Generic 954t = TypeVar('t') 955class B: pass 956class A(Generic[t]): 957 def f(self, x: t) -> None: pass 958A.f 959a_b = A() # type: A[B] 960A.f(a_b, B()) 961[out] 962MemberExpr(7) : def [t] (self: A[t`1], x: t`1) 963MemberExpr(9) : def (self: A[B], x: B) 964 965[case testUnboundOverloadedMethodOfGenericClass] 966## CallExpr 967from typing import TypeVar, Generic, overload 968t = TypeVar('t') 969class B: pass 970class A(Generic[t]): 971 @overload 972 def f(self, x: t) -> t: pass 973 @overload 974 def f(self) -> object: pass 975 def f(self, *args): pass 976 977ab, b = None, None # type: (A[B], B) 978A.f(ab, b) 979[builtins fixtures/tuple.pyi] 980[out] 981CallExpr(13) : B 982 983[case testUnboundMethodOfGenericClassWithImplicitSig] 984## MemberExpr 985from typing import TypeVar, Generic 986t = TypeVar('t') 987class B: pass 988class A(Generic[t]): 989 def f(self, x): pass 990A.f(None, None) 991[out] 992MemberExpr(7) : def (self: A[t`1], x: Any) -> Any 993 994[case testGenericMethodOfGenericClass] 995## MemberExpr 996from typing import TypeVar, Generic 997t = TypeVar('t') 998s = TypeVar('s') 999class B: pass 1000class A(Generic[t]): 1001 def f(self, y: s) -> None: pass 1002ab = None # type: A[B] 1003o = None # type: object 1004A.f(ab, o) 1005[out] 1006MemberExpr(10) : def (self: A[B], y: builtins.object) 1007 1008 1009-- Type variables with value restriction 1010-- ------------------------------------- 1011 1012 1013[case testTypeVariableWithValueRestriction] 1014## NameExpr 1015from typing import TypeVar 1016T = TypeVar('T', int, str) 1017def f(x: T) -> None: pass 1018f(1) 1019f('x') 1020[out] 1021NameExpr(5) : def (x: builtins.int) 1022NameExpr(6) : def (x: builtins.str) 1023 1024[case testTypeVariableWithValueRestrictionAndSubtype] 1025## NameExpr|CallExpr 1026from typing import TypeVar 1027T = TypeVar('T', int, str) 1028def f(x: T) -> T: pass 1029class S(str): pass 1030s = None # type: S 1031f(s) 1032[out] 1033CallExpr(7) : builtins.str 1034NameExpr(7) : def (x: builtins.str) -> builtins.str 1035NameExpr(7) : S 1036 1037 1038-- Binary operations 1039-- ----------------- 1040 1041 1042[case testBinaryOperatorWithAnyLeftOperand] 1043## OpExpr 1044from typing import Any, cast 1045class B: 1046 def __add__(self, x: int) -> str: pass 1047class A: 1048 def __radd__(self, x: B) -> int: pass 1049cast(Any, 1) + A() 1050B() + A() 1051[out] 1052OpExpr(7) : Any 1053OpExpr(8) : builtins.int 1054 1055[case testBinaryOperatorWithAnyRightOperand] 1056## OpExpr 1057from typing import Any, cast 1058class A: 1059 def __add__(self, x: str) -> int: pass 1060A() + cast(Any, 1) 1061[out] 1062OpExpr(5) : Any 1063 1064 1065-- Callable overloading 1066-- -------------------- 1067 1068 1069[case testOverloadedFunctionType] 1070## CallExpr 1071from typing import overload 1072@overload 1073def f(x: int) -> str: pass 1074@overload 1075def f(x: str) -> int: pass 1076def f(x): pass 1077f(1) 1078f('') 1079[out] 1080CallExpr(8) : builtins.str 1081CallExpr(9) : builtins.int 1082 1083[case testOverlappingOverloadedFunctionType] 1084## CallExpr 1085from typing import overload, Any 1086class A: pass 1087class B(A): pass 1088@overload 1089def f(x: B) -> B: pass 1090@overload 1091def f(x: A) -> A: pass 1092 1093def f(x) -> Any: pass 1094 1095a = None # type: A 1096b = None # type: B 1097f(a) 1098f(b) 1099[out] 1100CallExpr(14) : A 1101CallExpr(15) : B 1102 1103 1104 1105[case testOverloadedErasedType] 1106from typing import Callable 1107from typing import List 1108from typing import overload 1109from typing import TypeVar 1110 1111T = TypeVar("T") 1112V = TypeVar("V") 1113 1114def fun(s: int) -> int: pass 1115 1116def m(fun: Callable[[T], V], iter: List[T]) -> None: pass 1117 1118nums = [1] # type: List[int] 1119m(fun, 1120 nums) 1121[builtins fixtures/list.pyi] 1122[out] 1123IntExpr(13) : Literal[1]? 1124ListExpr(13) : builtins.list[builtins.int] 1125CallExpr(14) : None 1126NameExpr(14) : def (s: builtins.int) -> builtins.int 1127NameExpr(14) : def (fun: def (builtins.int) -> builtins.int, iter: builtins.list[builtins.int]) 1128NameExpr(15) : builtins.list[builtins.int] 1129 1130 1131-- Special cases 1132-- ------------- 1133 1134 1135[case testImplicitDataAttributeInit] 1136## NameExpr 1137import typing 1138class A: 1139 def __init__(self) -> None: 1140 self.x = ( 1141 A()) 1142[out] 1143NameExpr(5) : A 1144NameExpr(6) : def () -> A 1145 1146[case testListMultiplicationInContext] 1147## ListExpr|OpExpr|IntExpr 1148from typing import List 1149a = [None] * 3 # type: List[str] 1150[builtins fixtures/list.pyi] 1151[out] 1152IntExpr(3) : Literal[3]? 1153ListExpr(3) : builtins.list[builtins.str] 1154OpExpr(3) : builtins.list[builtins.str] 1155 1156[case testStringFormatting] 1157## IntExpr|OpExpr|StrExpr 1158'%d' % 1 1159[builtins fixtures/primitives.pyi] 1160[typing fixtures/typing-medium.pyi] 1161[out] 1162IntExpr(2) : Literal[1]? 1163OpExpr(2) : builtins.str 1164StrExpr(2) : Literal['%d']? 1165 1166-- TODO 1167-- 1168-- test expressions 1169-- list literal 1170-- tuple literal 1171-- unary minus 1172-- indexing 1173-- super expression 1174-- more complex lambda (multiple arguments etc.) 1175-- list comprehension 1176-- generator expression 1177-- overloads 1178-- other things 1179-- type inference 1180-- default argument value 1181-- for loop variable 1182-- exception variable 1183-- varargs 1184-- generics 1185-- explicit types 1186-- type of 'None' (currently stripped, but sometimes we may want to dump it) 1187