1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
|
-- Assignment
-- ----------
[case testAssignmentWithDynamic]
from typing import Any
d: Any
a: A
if int():
a = d # Everything ok
if int():
d = a
if int():
d = d
d.x = a
d.x = d
class A: pass
[case testMultipleAssignmentWithDynamic]
from typing import Any
d: Any
a: A
b: B
if int():
d, a = b, b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
if int():
d, d = d, d, d # E: Too many values to unpack (2 expected, 3 provided)
if int():
a, b = d, d
if int():
d, d = a, b
if int():
a, b = d
s, t = d
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
-- Expressions
-- -----------
[case testCallingFunctionWithDynamicArgumentTypes]
from typing import Any
def f(x: Any) -> 'A':
pass
a: A
b: B
if int():
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a = f(a)
if int():
a = f(b)
if int():
a = f(None)
if int():
a = f(f)
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testCallingWithDynamicReturnType]
from typing import Any
def f(x: 'A') -> Any:
pass
a: A
b: B
a = f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
a = f(a)
b = f(a)
class A: pass
class B: pass
[builtins fixtures/tuple.pyi]
[case testBinaryOperationsWithDynamicLeftOperand]
from typing import Any
d: Any
a: A
c: C
b: bool
n = 0
d in a # E: Unsupported right operand type for in ("A")
d and a
d or a
if int():
c = d and b # E: Incompatible types in assignment (expression has type "Union[Any, bool]", variable has type "C")
if int():
c = d or b # E: Incompatible types in assignment (expression has type "Union[Any, bool]", variable has type "C")
if int():
c = d + a
if int():
c = d - a
if int():
c = d * a
if int():
c = d / a
if int():
c = d // a
if int():
c = d % a
if int():
c = d ** a
if int():
b = d == a
if int():
b = d != a
if int():
b = d < a
if int():
b = d <= a
if int():
b = d > a
if int():
b = d >= a
if int():
b = d in c
if int():
b = d and b
if int():
b = d or b
class A: pass
class C:
def __contains__(self, a: A) -> bool:
pass
[file builtins.py]
class object:
def __init__(self): pass
class bool: pass
class int: pass
class type: pass
class function: pass
class str: pass
class dict: pass
[case testBinaryOperationsWithDynamicAsRightOperand]
from typing import Any
d: Any
a: A
c: C
b: bool
n = 0
a and d
a or d
if int():
c = a in d # E: Incompatible types in assignment (expression has type "bool", variable has type "C")
if int():
c = b and d # E: Incompatible types in assignment (expression has type "Union[Literal[False], Any]", variable has type "C")
if int():
c = b or d # E: Incompatible types in assignment (expression has type "Union[Literal[True], Any]", variable has type "C")
if int():
b = a + d
if int():
b = a / d
if int():
c = a + d
if int():
c = a - d
if int():
c = a * d
if int():
c = a / d
if int():
c = a // d
if int():
c = a % d
if int():
c = a ** d
if int():
b = a in d
if int():
b = b and d
if int():
b = b or d
class A:
def __add__(self, a: 'A') -> 'C':
pass
def __sub__(self, a: 'A') -> 'C':
pass
def __mul__(self, a: 'A') -> 'C':
pass
def __truediv__(self, a: 'A') -> 'C':
pass
def __floordiv__(self, a: 'A') -> 'C':
pass
def __mod__(self, a: 'A') -> 'C':
pass
def __pow__(self, a: 'A') -> 'C':
pass
def _lt(self, a: 'A') -> bool:
pass
def _gt(self, a: 'A') -> bool:
pass
class C: pass
[file builtins.py]
class object:
def __init__(self): pass
class bool: pass
class int: pass
class type: pass
class function: pass
class str: pass
class dict: pass
[case testDynamicWithUnaryExpressions]
from typing import Any
d: Any
a: A
b: bool
if int():
a = not d # E: Incompatible types in assignment (expression has type "bool", variable has type "A")
if int():
b = not d
a = -d
class A: pass
[builtins fixtures/bool.pyi]
[out]
[case testDynamicWithMemberAccess]
from typing import Any
d: Any
a: A
if int():
a = d.foo(a()) # E: "A" not callable
if int():
a = d.x
if int():
a = d.foo(a, a)
d.x = a
d.x.y.z
class A: pass
[out]
[case testIndexingWithDynamic]
from typing import Any
d: Any
a: A
if int():
a = d[a()] # E: "A" not callable
d[a()] = a # E: "A" not callable
if int():
a = d[a]
d[a] = a
d[a], d[a] = a, a
class A: pass
[case testTupleExpressionsWithDynamic]
from typing import Tuple, Any
t2: Tuple[A, A]
d: Any
if int():
t2 = (d, d, d) # E: Incompatible types in assignment (expression has type "tuple[Any, Any, Any]", variable has type "tuple[A, A]")
if int():
t2 = (d, d)
class A: pass
[builtins fixtures/tuple.pyi]
[case testCastsWithDynamicType]
from typing import Any, cast
class A: pass
class B: pass
def f() -> None: pass
d: Any
a: A
b: B
if int():
b = cast(A, d) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
if int():
a = cast(A, d)
if int():
b = cast(Any, d)
if int():
a = cast(Any, f())
[case testCompatibilityOfDynamicWithOtherTypes]
from typing import Any, Tuple
def g(a: 'A') -> None:
pass
class A: pass
class B: pass
d: Any
t: Tuple[A, A]
# TODO: callable types, overloaded functions
d = None # All ok
d = t
d = g
d = A
d1: Any
t = d1
f = d1
[builtins fixtures/tuple.pyi]
-- Statements
-- ----------
[case testDynamicCondition]
from typing import Any
d = None # type: Any
while d:
pass
if d:
pass
elif d:
pass
[builtins fixtures/bool.pyi]
[case testRaiseWithDynamic]
from typing import Any
d = None # type: Any
raise d
[builtins fixtures/exception.pyi]
[case testReturnWithDynamic]
from typing import Any
d = None # type: Any
def f() -> None:
return d # Ok
def g() -> 'A':
return d # Ok
class A: pass
-- Implicit dynamic types for functions
-- ------------------------------------
[case testImplicitGlobalFunctionSignature]
from typing import Any, Callable
x: Any
a: A
g: Callable[[], None]
h: Callable[[A], None]
def f(x): pass
f() # E: Missing positional argument "x" in call to "f"
f(x, x) # E: Too many arguments for "f"
if int():
g = f # E: Incompatible types in assignment (expression has type "Callable[[Any], Any]", variable has type "Callable[[], None]")
f(a)
f(x)
if int():
a = f(a)
if int():
h = f
class A: pass
[case testImplicitGlobalFunctionSignatureWithDifferentArgCounts]
from typing import Callable
g0: Callable[[], None]
g1: Callable[[A], None]
g2: Callable[[A, A], None]
a: A
def f0(): pass
def f2(x, y): pass
if int():
g1 = f0 # E: Incompatible types in assignment (expression has type "Callable[[], Any]", variable has type "Callable[[A], None]")
if int():
g2 = f0 # E: Incompatible types in assignment (expression has type "Callable[[], Any]", variable has type "Callable[[A, A], None]")
if int():
g0 = f2 # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], Any]", variable has type "Callable[[], None]")
if int():
g1 = f2 # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], Any]", variable has type "Callable[[A], None]")
if int():
g0 = g0
if int():
g2 = f2
f0()
f2(a, a)
class A: pass
[case testImplicitGlobalFunctionSignatureWithDefaultArgs]
from typing import Callable
class A: pass
class B: pass
a: A
b: B
def f01(x = b): pass
def f13(x, y = b, z = b): pass
g0: Callable[[], None]
g1: Callable[[A], None]
g2: Callable[[A, A], None]
g3: Callable[[A, A, A], None]
g4: Callable[[A, A, A, A], None]
f01(a, a) # E: Too many arguments for "f01"
f13() # E: Missing positional argument "x" in call to "f13"
f13(a, a, a, a) # E: Too many arguments for "f13"
if int():
g2 = f01 # E: Incompatible types in assignment (expression has type "Callable[[Any], Any]", variable has type "Callable[[A, A], None]")
if int():
g0 = f13 # E: Incompatible types in assignment (expression has type "Callable[[Any, Any, Any], Any]", variable has type "Callable[[], None]")
if int():
g4 = f13 # E: Incompatible types in assignment (expression has type "Callable[[Any, Any, Any], Any]", variable has type "Callable[[A, A, A, A], None]")
f01()
f01(a)
f13(a)
f13(a, a)
f13(a, a, a)
if int():
g0 = f01
if int():
g1 = f01
if int():
g1 = f13
if int():
g2 = f13
if int():
g3 = f13
[builtins fixtures/tuple.pyi]
[case testSkipTypeCheckingWithImplicitSignature]
a: A
def f():
a()
def g(x):
a()
a.x
a + a
if a():
a()
class A: pass
[builtins fixtures/bool.pyi]
[case testSkipTypeCheckingWithImplicitSignatureAndDefaultArgs]
a: A
def f(x=a()):
a()
def g(x, y=a, z=a()):
a()
class A: pass
[case testImplicitMethodSignature]
from typing import Callable
g0: Callable[[], None]
g1: Callable[[A], None]
g2: Callable[[A, A], None]
a: A
if int():
g0 = a.f # E: Incompatible types in assignment (expression has type "Callable[[Any], Any]", variable has type "Callable[[], None]")
if int():
g2 = a.f # E: Incompatible types in assignment (expression has type "Callable[[Any], Any]", variable has type "Callable[[A, A], None]")
if int():
a = a.f # E: Incompatible types in assignment (expression has type "Callable[[Any], Any]", variable has type "A")
class A:
def g(self) -> None:
a = self.f(a)
def f(self, x): pass
if int():
g1 = a.f
if int():
a = a.f(a)
[case testSkipTypeCheckingImplicitMethod]
a: A
class A:
def f(self):
a()
def g(self, x, y=a()):
a()
[case testImplicitInheritedMethod]
from typing import Callable
g0: Callable[[], None]
g1: Callable[[A], None]
a: A
if int():
g0 = a.f # E: Incompatible types in assignment (expression has type "Callable[[Any], Any]", variable has type "Callable[[], None]")
if int():
g1 = a.f
if int():
a = a.f(a)
class B:
def f(self, x):
pass
class A(B):
def g(self) -> None:
a = self.f(a)
[case testEmptyReturnWithImplicitSignature]
import typing
def f():
return
class A:
def g(self):
return
[case testVarArgsWithImplicitSignature]
from typing import Any
o = None # type: Any
def f(x, *a): pass
f() # E: Missing positional argument "x" in call to "f"
f(o)
f(o, o)
f(o, o, o)
[builtins fixtures/list.pyi]
-- Implicit types for constructors
-- -------------------------------
[case testInitMethodWithImplicitSignature]
from typing import Callable
class A:
def __init__(self, a, b): pass
f1: Callable[[A], A]
f2: Callable[[A, A], A]
a: A
A(a) # E: Missing positional argument "b" in call to "A"
if int():
f1 = A # E: Incompatible types in assignment (expression has type "type[A]", variable has type "Callable[[A], A]")
A(a, a)
if int():
f2 = A
[case testUsingImplicitTypeObjectWithIs]
class A: pass
class B:
def __init__(self): pass
t: type
t = A
t = B
-- Type compatibility
-- ------------------
[case testTupleTypeCompatibility]
from typing import Any, Tuple
t1: Tuple[Any, A]
t2: Tuple[A, Any]
t3: Tuple[Any, Any]
t4: Tuple[A, A]
t5: Tuple[Any, Any, Any]
def f(): t1, t2, t3, t4, t5 # Prevent redefinition
t3 = t5 # E: Incompatible types in assignment (expression has type "tuple[Any, Any, Any]", variable has type "tuple[Any, Any]")
t5 = t4 # E: Incompatible types in assignment (expression has type "tuple[A, A]", variable has type "tuple[Any, Any, Any]")
t1 = t1
t1 = t2
t1 = t3
t1 = t4
t2 = t1
t2 = t3
t2 = t4
t3 = t1
t3 = t2
t3 = t4
t4 = t1
t4 = t2
t4 = t3
class A: pass
[builtins fixtures/tuple.pyi]
[case testFunctionTypeCompatibilityAndReturnTypes]
from typing import Any, Callable, Optional
f1: Callable[[], Any]
f11: Callable[[], Any]
f2: Callable[[], Optional[A]]
f3: Callable[[], None]
f2 = f3
f1 = f2
f1 = f3
f2 = f11
f3 = f11
class A: pass
[case testFunctionTypeCompatibilityAndArgumentTypes]
from typing import Any, Callable
f1: Callable[[A, Any], None]
f2: Callable[[Any, A], None]
f3: Callable[[A, A], None]
f1 = f1
f1 = f2
f1 = f3
f2 = f1
f2 = f2
f2 = f3
f3 = f1
f3 = f2
f3 = f3
class A: pass
[case testFunctionTypeCompatibilityAndArgumentCounts]
from typing import Any, Callable
f1: Callable[[Any], None]
f2: Callable[[Any, Any], None]
if int():
f1 = f2 # E: Incompatible types in assignment (expression has type "Callable[[Any, Any], None]", variable has type "Callable[[Any], None]")
-- Overriding
-- ----------
[case testOverridingMethodWithDynamicTypes]
from typing import Any
a: A
b: B
b.f(b) # E: Argument 1 to "f" of "B" has incompatible type "B"; expected "A"
a = a.f(b)
class B:
def f(self, x: 'A') -> 'B':
pass
def g(self, x: 'B') -> None:
pass
class A(B):
def f(self, x: Any) -> Any:
pass
def g(self, x: Any) -> None:
pass
[builtins fixtures/tuple.pyi]
[case testOverridingMethodWithImplicitDynamicTypes]
a: A
b: B
b.f(b) # E: Argument 1 to "f" of "B" has incompatible type "B"; expected "A"
a = a.f(b)
class B:
def f(self, x: 'A') -> 'B':
pass
def g(self, x: 'B') -> None:
pass
class A(B):
def f(self, x):
pass
def g(self, x):
pass
[builtins fixtures/tuple.pyi]
[case testOverridingMethodAcrossHierarchy]
import typing
class C:
def f(self, a: 'A') -> None: pass
class B(C):
def f(self, a): pass
class A(B):
def f(self, a: 'D') -> None: # E: Argument 1 of "f" is incompatible with supertype "C"; supertype defines the argument type as "A" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
pass
class D: pass
[out]
[case testInvalidOverrideArgumentCountWithImplicitSignature1]
import typing
class B:
def f(self, x: A) -> None: pass
class A(B):
def f(self, x, y): # dynamic function not type checked
x()
[out]
[case testInvalidOverrideArgumentCountWithImplicitSignature2]
import typing
class B:
def f(self, x, y): pass
class A(B):
def f(self, x: 'A') -> None: # Fail
pass
[out]
main:5: error: Signature of "f" incompatible with supertype "B"
main:5: note: Superclass:
main:5: note: def f(self, x: Any, y: Any) -> Any
main:5: note: Subclass:
main:5: note: def f(self, x: A) -> None
[case testInvalidOverrideArgumentCountWithImplicitSignature3]
import typing
class B:
def f(self, x: A) -> None: pass
class A(B):
def f(self, x, y) -> None: # Fail
x()
[out]
main:5: error: Signature of "f" incompatible with supertype "B"
main:5: note: Superclass:
main:5: note: def f(self, x: A) -> None
main:5: note: Subclass:
main:5: note: def f(self, x: Any, y: Any) -> None
[case testInvalidOverrideArgumentCountWithImplicitSignature4]
# flags: --check-untyped-defs
import typing
class B:
def f(self, x: A) -> None: pass
class A(B):
def f(self, x, y):
x()
[out]
main:6: error: Signature of "f" incompatible with supertype "B"
main:6: note: Superclass:
main:6: note: def f(self, x: A) -> None
main:6: note: Subclass:
main:6: note: def f(self, x: Any, y: Any) -> Any
[case testInvalidOverrideWithImplicitSignatureAndClassMethod1]
class B:
@classmethod
def f(cls, x, y): pass
class A(B):
@classmethod
def f(cls, x, y, z): pass # No error since no annotations
[builtins fixtures/classmethod.pyi]
[case testInvalidOverrideWithImplicitSignatureAndClassMethod2]
class B:
@classmethod
def f(cls, x: int, y): pass
class A(B):
@classmethod
def f(cls, x, y, z): pass # No error since no annotations
[builtins fixtures/classmethod.pyi]
[case testInvalidOverrideWithImplicitSignatureAndStaticMethod1]
class B:
@staticmethod
def f(x, y): pass
class A(B):
@staticmethod
def f(x, y, z): pass # No error since no annotations
[builtins fixtures/classmethod.pyi]
[case testInvalidOverrideWithImplicitSignatureAndStaticMethod2]
class B:
@staticmethod
def f(self, x: int, y): pass
class A(B):
@staticmethod
def f(self, x, y, z): pass # No error since no annotations
[builtins fixtures/classmethod.pyi]
-- Don't complain about too few/many arguments in dynamic functions
-- ----------------------------------------------------------------
[case testTooManyArgsInDynamic]
def f() -> None: pass
def g():
f(1) # Silent
[out]
[case testTooFewArgsInDynamic]
def f(a: int) -> None: pass
def g():
f() # Silent
[out]
[case testJustRightInDynamic]
def f(a: int) -> None: pass
def g():
f('') # Silent
[out]
|