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