| 1 |
-- Test cases for simple expressions. |
| 2 |
-- |
| 3 |
-- See also: |
| 4 |
-- * check-functions.test contains test cases for calls. |
| 5 |
-- * check-varargs.test contains test cases for *args. |
| 6 |
-- * check-dynamic.test contains test cases related to 'Any' type. |
| 7 |
-- * check-generics.test contains test cases for generic values. |
| 8 |
|
| 9 |
|
| 10 |
-- None expression |
| 11 |
-- --------------- |
| 12 |
|
| 13 |
|
| 14 |
[case testNoneAsRvalue] |
| 15 |
import typing |
| 16 |
a: A |
| 17 |
class A: pass |
| 18 |
[out] |
| 19 |
|
| 20 |
[case testNoneAsArgument] |
| 21 |
# flags: --no-strict-optional |
| 22 |
import typing |
| 23 |
def f(x: 'A', y: 'B') -> None: pass |
| 24 |
f(None, None) |
| 25 |
class A: pass |
| 26 |
class B(A): pass |
| 27 |
[out] |
| 28 |
|
| 29 |
|
| 30 |
-- Simple expressions |
| 31 |
-- ------------------ |
| 32 |
|
| 33 |
|
| 34 |
[case testIntLiteral] |
| 35 |
a = 0 |
| 36 |
b: A |
| 37 |
if int(): |
| 38 |
b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "A") |
| 39 |
if int(): |
| 40 |
a = 1 |
| 41 |
class A: |
| 42 |
pass |
| 43 |
|
| 44 |
[case testStrLiteral] |
| 45 |
a = '' |
| 46 |
b: A |
| 47 |
if int(): |
| 48 |
b = 'x' # E: Incompatible types in assignment (expression has type "str", variable has type "A") |
| 49 |
if int(): |
| 50 |
a = 'x' |
| 51 |
if int(): |
| 52 |
a = r"x" |
| 53 |
if int(): |
| 54 |
a = """foo""" |
| 55 |
class A: |
| 56 |
pass |
| 57 |
|
| 58 |
[case testFloatLiteral] |
| 59 |
a = 0.0 |
| 60 |
b: A |
| 61 |
if str(): |
| 62 |
b = 1.1 # E: Incompatible types in assignment (expression has type "float", variable has type "A") |
| 63 |
if str(): |
| 64 |
a = 1.1 |
| 65 |
class A: |
| 66 |
pass |
| 67 |
[builtins fixtures/dict.pyi] |
| 68 |
|
| 69 |
[case testComplexLiteral] |
| 70 |
a = 0.0j |
| 71 |
b: A |
| 72 |
if str(): |
| 73 |
b = 1.1j # E: Incompatible types in assignment (expression has type "complex", variable has type "A") |
| 74 |
if str(): |
| 75 |
a = 1.1j |
| 76 |
class A: |
| 77 |
pass |
| 78 |
[builtins fixtures/dict.pyi] |
| 79 |
|
| 80 |
[case testBytesLiteral] |
| 81 |
b: bytes |
| 82 |
a: A |
| 83 |
if str(): |
| 84 |
b = b'foo' |
| 85 |
if str(): |
| 86 |
b = br"foo" |
| 87 |
if str(): |
| 88 |
b = b'''foo''' |
| 89 |
if str(): |
| 90 |
a = b'foo' # E: Incompatible types in assignment (expression has type "bytes", variable has type "A") |
| 91 |
class A: pass |
| 92 |
[builtins fixtures/dict.pyi] |
| 93 |
|
| 94 |
[case testUnicodeLiteralInPython3] |
| 95 |
s: str |
| 96 |
if int(): |
| 97 |
s = u'foo' |
| 98 |
b: bytes |
| 99 |
if int(): |
| 100 |
b = u'foo' # E: Incompatible types in assignment (expression has type "str", variable has type "bytes") |
| 101 |
[builtins fixtures/primitives.pyi] |
| 102 |
|
| 103 |
|
| 104 |
-- Binary operators |
| 105 |
-- ---------------- |
| 106 |
|
| 107 |
|
| 108 |
[case testAdd] |
| 109 |
a: A |
| 110 |
b: B |
| 111 |
c: C |
| 112 |
if int(): |
| 113 |
c = a + c # E: Unsupported operand types for + ("A" and "C") |
| 114 |
if int(): |
| 115 |
a = a + b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 116 |
if int(): |
| 117 |
c = b + a # E: Unsupported left operand type for + ("B") |
| 118 |
if int(): |
| 119 |
c = a + b |
| 120 |
|
| 121 |
class A: |
| 122 |
def __add__(self, x: 'B') -> 'C': |
| 123 |
pass |
| 124 |
class B: |
| 125 |
pass |
| 126 |
class C: |
| 127 |
pass |
| 128 |
[builtins fixtures/tuple.pyi] |
| 129 |
|
| 130 |
[case testSub] |
| 131 |
a: A |
| 132 |
b: B |
| 133 |
c: C |
| 134 |
if int(): |
| 135 |
c = a - c # E: Unsupported operand types for - ("A" and "C") |
| 136 |
if int(): |
| 137 |
a = a - b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 138 |
if int(): |
| 139 |
c = b - a # E: Unsupported left operand type for - ("B") |
| 140 |
if int(): |
| 141 |
c = a - b |
| 142 |
|
| 143 |
class A: |
| 144 |
def __sub__(self, x: 'B') -> 'C': |
| 145 |
pass |
| 146 |
class B: |
| 147 |
pass |
| 148 |
class C: |
| 149 |
pass |
| 150 |
[builtins fixtures/tuple.pyi] |
| 151 |
|
| 152 |
[case testMul] |
| 153 |
a: A |
| 154 |
b: B |
| 155 |
c: C |
| 156 |
if int(): |
| 157 |
c = a * c # E: Unsupported operand types for * ("A" and "C") |
| 158 |
if int(): |
| 159 |
a = a * b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 160 |
if int(): |
| 161 |
c = b * a # E: Unsupported left operand type for * ("B") |
| 162 |
if int(): |
| 163 |
c = a * b |
| 164 |
|
| 165 |
class A: |
| 166 |
def __mul__(self, x: 'B') -> 'C': |
| 167 |
pass |
| 168 |
class B: |
| 169 |
pass |
| 170 |
class C: |
| 171 |
pass |
| 172 |
[builtins fixtures/tuple.pyi] |
| 173 |
|
| 174 |
[case testMatMul] |
| 175 |
a: A |
| 176 |
b: B |
| 177 |
c: C |
| 178 |
if int(): |
| 179 |
c = a @ c # E: Unsupported operand types for @ ("A" and "C") |
| 180 |
if int(): |
| 181 |
a = a @ b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 182 |
if int(): |
| 183 |
c = b @ a # E: Unsupported left operand type for @ ("B") |
| 184 |
if int(): |
| 185 |
c = a @ b |
| 186 |
|
| 187 |
class A: |
| 188 |
def __matmul__(self, x: 'B') -> 'C': |
| 189 |
pass |
| 190 |
class B: |
| 191 |
pass |
| 192 |
class C: |
| 193 |
pass |
| 194 |
[builtins fixtures/tuple.pyi] |
| 195 |
|
| 196 |
[case testDiv] |
| 197 |
a: A |
| 198 |
b: B |
| 199 |
c: C |
| 200 |
if int(): |
| 201 |
c = a / c # E: Unsupported operand types for / ("A" and "C") |
| 202 |
a = a / b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 203 |
if int(): |
| 204 |
c = b / a # E: Unsupported left operand type for / ("B") |
| 205 |
if int(): |
| 206 |
c = a / b |
| 207 |
|
| 208 |
class A: |
| 209 |
def __truediv__(self, x: 'B') -> 'C': |
| 210 |
pass |
| 211 |
class B: |
| 212 |
pass |
| 213 |
class C: |
| 214 |
pass |
| 215 |
[builtins fixtures/tuple.pyi] |
| 216 |
|
| 217 |
[case testIntDiv] |
| 218 |
a: A |
| 219 |
b: B |
| 220 |
c: C |
| 221 |
if int(): |
| 222 |
c = a // c # E: Unsupported operand types for // ("A" and "C") |
| 223 |
a = a // b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 224 |
if int(): |
| 225 |
c = b // a # E: Unsupported left operand type for // ("B") |
| 226 |
if int(): |
| 227 |
c = a // b |
| 228 |
|
| 229 |
class A: |
| 230 |
def __floordiv__(self, x: 'B') -> 'C': |
| 231 |
pass |
| 232 |
class B: |
| 233 |
pass |
| 234 |
class C: |
| 235 |
pass |
| 236 |
[builtins fixtures/tuple.pyi] |
| 237 |
|
| 238 |
[case testMod] |
| 239 |
a: A |
| 240 |
b: B |
| 241 |
c: C |
| 242 |
if int(): |
| 243 |
c = a % c # E: Unsupported operand types for % ("A" and "C") |
| 244 |
if int(): |
| 245 |
a = a % b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 246 |
if int(): |
| 247 |
c = b % a # E: Unsupported left operand type for % ("B") |
| 248 |
if int(): |
| 249 |
c = a % b |
| 250 |
|
| 251 |
class A: |
| 252 |
def __mod__(self, x: 'B') -> 'C': |
| 253 |
pass |
| 254 |
class B: |
| 255 |
pass |
| 256 |
class C: |
| 257 |
pass |
| 258 |
[builtins fixtures/tuple.pyi] |
| 259 |
|
| 260 |
[case testPow] |
| 261 |
a: A |
| 262 |
b: B |
| 263 |
c: C |
| 264 |
if int(): |
| 265 |
c = a ** c # E: Unsupported operand types for ** ("A" and "C") |
| 266 |
if int(): |
| 267 |
a = a ** b # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 268 |
if int(): |
| 269 |
c = b ** a # E: Unsupported left operand type for ** ("B") |
| 270 |
if int(): |
| 271 |
c = a ** b |
| 272 |
|
| 273 |
class A: |
| 274 |
def __pow__(self, x: 'B') -> 'C': |
| 275 |
pass |
| 276 |
class B: |
| 277 |
pass |
| 278 |
class C: |
| 279 |
pass |
| 280 |
[builtins fixtures/tuple.pyi] |
| 281 |
|
| 282 |
[case testMiscBinaryOperators] |
| 283 |
a: A |
| 284 |
b: B |
| 285 |
b = a & a # Fail |
| 286 |
b = a | b # Fail |
| 287 |
b = a ^ a # Fail |
| 288 |
b = a << b # Fail |
| 289 |
b = a >> a # Fail |
| 290 |
|
| 291 |
b = a & b |
| 292 |
b = a | a |
| 293 |
b = a ^ b |
| 294 |
b = a << a |
| 295 |
b = a >> b |
| 296 |
class A: |
| 297 |
def __and__(self, x: 'B') -> 'B': pass |
| 298 |
def __or__(self, x: 'A') -> 'B': pass |
| 299 |
def __xor__(self, x: 'B') -> 'B': pass |
| 300 |
def __lshift__(self, x: 'A') -> 'B': pass |
| 301 |
def __rshift__(self, x: 'B') -> 'B': pass |
| 302 |
class B: pass |
| 303 |
[builtins fixtures/tuple.pyi] |
| 304 |
[out] |
| 305 |
main:3: error: Unsupported operand types for & ("A" and "A") |
| 306 |
main:4: error: Unsupported operand types for | ("A" and "B") |
| 307 |
main:5: error: Unsupported operand types for ^ ("A" and "A") |
| 308 |
main:6: error: Unsupported operand types for << ("A" and "B") |
| 309 |
main:7: error: Unsupported operand types for >> ("A" and "A") |
| 310 |
|
| 311 |
[case testBooleanAndOr] |
| 312 |
a: A |
| 313 |
b: bool |
| 314 |
if int(): |
| 315 |
b = b and b |
| 316 |
if int(): |
| 317 |
b = b or b |
| 318 |
if int(): |
| 319 |
b = b and a # E: Incompatible types in assignment (expression has type "Literal[False] | A", variable has type "bool") |
| 320 |
if int(): |
| 321 |
b = a and b # E: Incompatible types in assignment (expression has type "A | bool", variable has type "bool") |
| 322 |
if int(): |
| 323 |
b = b or a # E: Incompatible types in assignment (expression has type "Literal[True] | A", variable has type "bool") |
| 324 |
if int(): |
| 325 |
b = a or b # E: Incompatible types in assignment (expression has type "A | bool", variable has type "bool") |
| 326 |
class A: pass |
| 327 |
|
| 328 |
[builtins fixtures/bool.pyi] |
| 329 |
|
| 330 |
[case testRestrictedTypeAnd] |
| 331 |
|
| 332 |
b: bool |
| 333 |
i: str |
| 334 |
j = not b and i |
| 335 |
if j: |
| 336 |
reveal_type(j) # N: Revealed type is "builtins.str" |
| 337 |
[builtins fixtures/bool.pyi] |
| 338 |
|
| 339 |
[case testRestrictedTypeOr] |
| 340 |
|
| 341 |
b: bool |
| 342 |
i: str |
| 343 |
j = b or i |
| 344 |
if not j: |
| 345 |
reveal_type(j) # N: Revealed type is "Literal['']" |
| 346 |
[builtins fixtures/bool.pyi] |
| 347 |
|
| 348 |
[case testAndOr] |
| 349 |
|
| 350 |
s = "" |
| 351 |
b = bool() |
| 352 |
reveal_type(s and b or b) # N: Revealed type is "builtins.bool" |
| 353 |
[builtins fixtures/bool.pyi] |
| 354 |
|
| 355 |
[case testRestrictedBoolAndOrWithGenerics] |
| 356 |
from typing import List |
| 357 |
|
| 358 |
def f(a: List[str], b: bool) -> bool: |
| 359 |
x = a and b |
| 360 |
y: bool |
| 361 |
return reveal_type(x or y) # N: Revealed type is "builtins.bool" |
| 362 |
[builtins fixtures/list.pyi] |
| 363 |
|
| 364 |
[case testNonBooleanOr] |
| 365 |
c: C |
| 366 |
d: D |
| 367 |
b: bool |
| 368 |
if int(): |
| 369 |
c = c or c |
| 370 |
if int(): |
| 371 |
c = c or d |
| 372 |
if int(): |
| 373 |
c = d or c |
| 374 |
if int(): |
| 375 |
b = c or c # E: Incompatible types in assignment (expression has type "C", variable has type "bool") |
| 376 |
if int(): |
| 377 |
d = c or d # E: Incompatible types in assignment (expression has type "C", variable has type "D") |
| 378 |
if int(): |
| 379 |
d = d or c # E: Incompatible types in assignment (expression has type "C", variable has type "D") |
| 380 |
class C: pass |
| 381 |
class D(C): pass |
| 382 |
[builtins fixtures/bool.pyi] |
| 383 |
|
| 384 |
[case testInOperator] |
| 385 |
from typing import Iterator, Iterable, Any |
| 386 |
a: A |
| 387 |
b: B |
| 388 |
c: bool |
| 389 |
d: D |
| 390 |
e: Any |
| 391 |
if int(): |
| 392 |
c = c in a # E: Unsupported operand types for in ("bool" and "A") |
| 393 |
if int(): |
| 394 |
a = b in a # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 395 |
if int(): |
| 396 |
c = a in b # E: Unsupported right operand type for in ("B") |
| 397 |
if int(): |
| 398 |
c = b in d # E: Unsupported operand types for in ("B" and "D") |
| 399 |
if int(): |
| 400 |
c = b in a |
| 401 |
if int(): |
| 402 |
c = a in d |
| 403 |
if int(): |
| 404 |
c = e in d |
| 405 |
if int(): |
| 406 |
c = a in e |
| 407 |
|
| 408 |
class A: |
| 409 |
def __contains__(self, x: 'B') -> bool: pass |
| 410 |
class B: pass |
| 411 |
class D(Iterable[A]): |
| 412 |
def __iter__(self) -> Iterator[A]: pass |
| 413 |
[builtins fixtures/bool.pyi] |
| 414 |
|
| 415 |
[case testNotInOperator] |
| 416 |
from typing import Iterator, Iterable, Any |
| 417 |
a: A |
| 418 |
b: B |
| 419 |
c: bool |
| 420 |
d: D |
| 421 |
e: Any |
| 422 |
if int(): |
| 423 |
c = c not in a # E: Unsupported operand types for in ("bool" and "A") |
| 424 |
if int(): |
| 425 |
a = b not in a # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 426 |
if int(): |
| 427 |
c = a not in b # E: Unsupported right operand type for in ("B") |
| 428 |
if int(): |
| 429 |
c = b not in d # E: Unsupported operand types for in ("B" and "D") |
| 430 |
if int(): |
| 431 |
c = b not in a |
| 432 |
if int(): |
| 433 |
c = a not in d |
| 434 |
if int(): |
| 435 |
c = e in d |
| 436 |
if int(): |
| 437 |
c = a in e |
| 438 |
|
| 439 |
class A: |
| 440 |
def __contains__(self, x: 'B') -> bool: pass |
| 441 |
class B: pass |
| 442 |
class D(Iterable[A]): |
| 443 |
def __iter__(self) -> Iterator[A]: pass |
| 444 |
[builtins fixtures/bool.pyi] |
| 445 |
|
| 446 |
[case testNonBooleanContainsReturnValue] |
| 447 |
a: A |
| 448 |
b: bool |
| 449 |
c: str |
| 450 |
if int(): |
| 451 |
b = a not in a |
| 452 |
if int(): |
| 453 |
b = a in a |
| 454 |
if int(): |
| 455 |
c = a not in a # E: Incompatible types in assignment (expression has type "bool", variable has type "str") |
| 456 |
if int(): |
| 457 |
c = a in a # E: Incompatible types in assignment (expression has type "bool", variable has type "str") |
| 458 |
|
| 459 |
class A: |
| 460 |
def __contains__(self, x: 'A') -> str: pass |
| 461 |
[builtins fixtures/bool.pyi] |
| 462 |
|
| 463 |
[case testInWithInvalidArgs] |
| 464 |
a = 1 in ([1] + ['x']) # E: List item 0 has incompatible type "str"; expected "int" |
| 465 |
[builtins fixtures/list.pyi] |
| 466 |
|
| 467 |
[case testEq] |
| 468 |
a: A |
| 469 |
b: bool |
| 470 |
if int(): |
| 471 |
a = a == b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 472 |
if int(): |
| 473 |
a = a != b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 474 |
if int(): |
| 475 |
b = a == b |
| 476 |
if int(): |
| 477 |
b = a != b |
| 478 |
|
| 479 |
class A: |
| 480 |
def __eq__(self, o: object) -> bool: pass |
| 481 |
def __ne__(self, o: object) -> bool: pass |
| 482 |
[builtins fixtures/bool.pyi] |
| 483 |
|
| 484 |
[case testLtAndGt] |
| 485 |
a: A |
| 486 |
b: B |
| 487 |
bo: bool |
| 488 |
if int(): |
| 489 |
a = a < b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 490 |
if int(): |
| 491 |
a = a > b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 492 |
if int(): |
| 493 |
bo = a < b |
| 494 |
if int(): |
| 495 |
bo = a > b |
| 496 |
|
| 497 |
class A: |
| 498 |
def __lt__(self, o: 'B') -> bool: pass |
| 499 |
def __gt__(self, o: 'B') -> bool: pass |
| 500 |
class B: |
| 501 |
def __lt__(self, o: 'B') -> bool: pass |
| 502 |
def __gt__(self, o: 'B') -> bool: pass |
| 503 |
[builtins fixtures/bool.pyi] |
| 504 |
|
| 505 |
[case cmpIgnoredPy3] |
| 506 |
a: A |
| 507 |
b: B |
| 508 |
bo: bool |
| 509 |
bo = a <= b # E: Unsupported left operand type for <= ("A") |
| 510 |
|
| 511 |
class A: |
| 512 |
def __cmp__(self, o: 'B') -> bool: pass |
| 513 |
class B: |
| 514 |
pass |
| 515 |
[builtins fixtures/bool.pyi] |
| 516 |
|
| 517 |
[case testLeAndGe] |
| 518 |
a: A |
| 519 |
b: B |
| 520 |
bo: bool |
| 521 |
if int(): |
| 522 |
a = a <= b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 523 |
if int(): |
| 524 |
a = a >= b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 525 |
if int(): |
| 526 |
bo = a <= b |
| 527 |
if int(): |
| 528 |
bo = a >= b |
| 529 |
|
| 530 |
class A: |
| 531 |
def __le__(self, o: 'B') -> bool: pass |
| 532 |
def __ge__(self, o: 'B') -> bool: pass |
| 533 |
class B: |
| 534 |
def __le__(self, o: 'B') -> bool: pass |
| 535 |
def __ge__(self, o: 'B') -> bool: pass |
| 536 |
[builtins fixtures/bool.pyi] |
| 537 |
|
| 538 |
[case testChainedComp] |
| 539 |
a: A |
| 540 |
b: B |
| 541 |
bo: bool |
| 542 |
a < a < b < b # Fail |
| 543 |
a < b < b < b |
| 544 |
a < a > a < b # Fail |
| 545 |
|
| 546 |
class A: |
| 547 |
def __lt__(self, o: 'B') -> bool: pass |
| 548 |
def __gt__(self, o: 'B') -> bool: pass |
| 549 |
class B: |
| 550 |
def __lt__(self, o: 'B') -> bool: pass |
| 551 |
def __gt__(self, o: 'B') -> bool: pass |
| 552 |
[builtins fixtures/bool.pyi] |
| 553 |
[out] |
| 554 |
main:4: error: Unsupported operand types for < ("A" and "A") |
| 555 |
main:6: error: Unsupported operand types for < ("A" and "A") |
| 556 |
main:6: error: Unsupported operand types for > ("A" and "A") |
| 557 |
|
| 558 |
|
| 559 |
[case testChainedCompBoolRes] |
| 560 |
a: A |
| 561 |
b: B |
| 562 |
bo: bool |
| 563 |
if int(): |
| 564 |
bo = a < b < b |
| 565 |
if int(): |
| 566 |
a = a < b < b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 567 |
|
| 568 |
class A: |
| 569 |
def __lt__(self, o: 'B') -> bool: pass |
| 570 |
def __gt__(self, o: 'B') -> bool: pass |
| 571 |
class B: |
| 572 |
def __lt__(self, o: 'B') -> bool: pass |
| 573 |
def __gt__(self, o: 'B') -> bool: pass |
| 574 |
[builtins fixtures/bool.pyi] |
| 575 |
|
| 576 |
|
| 577 |
[case testChainedCompResTyp] |
| 578 |
x: X |
| 579 |
y: Y |
| 580 |
a: A |
| 581 |
b: B |
| 582 |
p: P |
| 583 |
bo: bool |
| 584 |
if int(): |
| 585 |
b = y == y == y |
| 586 |
if int(): |
| 587 |
bo = y == y == y # E: Incompatible types in assignment (expression has type "B", variable has type "bool") |
| 588 |
if int(): |
| 589 |
a = x < y |
| 590 |
if int(): |
| 591 |
a = x < y == y # E: Incompatible types in assignment (expression has type "P", variable has type "A") |
| 592 |
if int(): |
| 593 |
p = x < y == y |
| 594 |
|
| 595 |
class P: |
| 596 |
pass |
| 597 |
class A(P): |
| 598 |
pass |
| 599 |
class B(P): |
| 600 |
pass |
| 601 |
|
| 602 |
class X: |
| 603 |
def __lt__(self, o: 'Y') -> A: pass |
| 604 |
def __gt__(self, o: 'Y') -> A: pass |
| 605 |
class Y: |
| 606 |
def __lt__(self, o: 'Y') -> A: pass |
| 607 |
def __gt__(self, o: 'Y') -> A: pass |
| 608 |
def __eq__(self, o: 'Y') -> B: pass # type: ignore |
| 609 |
[builtins fixtures/bool.pyi] |
| 610 |
|
| 611 |
|
| 612 |
[case testIs] |
| 613 |
a: A |
| 614 |
b: bool |
| 615 |
if int(): |
| 616 |
a = a is b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 617 |
if int(): |
| 618 |
b = a is b |
| 619 |
if int(): |
| 620 |
b = b is a |
| 621 |
if int(): |
| 622 |
b = a is None |
| 623 |
class A: pass |
| 624 |
[builtins fixtures/bool.pyi] |
| 625 |
|
| 626 |
[case testIsNot] |
| 627 |
a: A |
| 628 |
b: bool |
| 629 |
if int(): |
| 630 |
a = a is not b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 631 |
if int(): |
| 632 |
b = a is not b |
| 633 |
if int(): |
| 634 |
b = b is not a |
| 635 |
if int(): |
| 636 |
b = a is not None |
| 637 |
class A: pass |
| 638 |
[builtins fixtures/bool.pyi] |
| 639 |
|
| 640 |
[case testIsRightOperand] |
| 641 |
|
| 642 |
1 is 1() |
| 643 |
[builtins fixtures/bool.pyi] |
| 644 |
[out] |
| 645 |
main:2: error: "int" not callable |
| 646 |
|
| 647 |
[case testReverseBinaryOperator] |
| 648 |
|
| 649 |
class A: |
| 650 |
def __add__(self, x: int) -> int: pass |
| 651 |
class B: |
| 652 |
def __radd__(self, x: A) -> str: pass |
| 653 |
s: str |
| 654 |
n: int |
| 655 |
if int(): |
| 656 |
n = A() + 1 |
| 657 |
if int(): |
| 658 |
s = A() + B() |
| 659 |
if int(): |
| 660 |
n = A() + B() # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 661 |
|
| 662 |
[case testReverseBinaryOperator2] |
| 663 |
class A: |
| 664 |
def __add__(self, x: 'A') -> object: pass |
| 665 |
class B: |
| 666 |
def __radd__(self, x: A) -> str: pass |
| 667 |
s: str |
| 668 |
n: int |
| 669 |
if int(): |
| 670 |
s = A() + B() |
| 671 |
n = A() + B() # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 672 |
|
| 673 |
[case testReverseBinaryOperator3] |
| 674 |
|
| 675 |
class N: |
| 676 |
def __add__(self, x: 'N') -> object: pass |
| 677 |
class A: |
| 678 |
def __add__(self, x: N) -> int: pass |
| 679 |
class B: |
| 680 |
def __radd__(self, x: N) -> str: pass |
| 681 |
s: str |
| 682 |
s = A() + B() # E: Unsupported operand types for + ("A" and "B") |
| 683 |
|
| 684 |
|
| 685 |
[case testReverseBinaryOperator4] |
| 686 |
from typing import assert_type, Never |
| 687 |
|
| 688 |
class Size(tuple[int, ...]): |
| 689 |
def __add__(self, other: tuple[int, ...], /) -> "Size": return Size() # type: ignore[override] |
| 690 |
def __radd__(self, other: tuple[int, ...], /) -> "Size": return Size() |
| 691 |
|
| 692 |
size: Size = Size([3, 4]) |
| 693 |
tup0: tuple[()] = () |
| 694 |
tup1: tuple[int] = (1,) |
| 695 |
tup2: tuple[int, int] = (1, 2) |
| 696 |
tupN: tuple[int, ...] = (1, 2, 3) |
| 697 |
tupX: tuple[Never, ...] = () |
| 698 |
|
| 699 |
assert_type(tup0 + size, Size) |
| 700 |
assert_type(tup1 + size, Size) |
| 701 |
assert_type(tup2 + size, Size) |
| 702 |
assert_type(tupN + size, Size) |
| 703 |
assert_type(tupX + size, Size) |
| 704 |
|
| 705 |
[builtins fixtures/tuple-typeshed.pyi] |
| 706 |
|
| 707 |
[case testBinaryOperatorWithAnyRightOperand] |
| 708 |
from typing import Any, cast |
| 709 |
class A: pass |
| 710 |
A() + cast(Any, 1) |
| 711 |
|
| 712 |
[case testReverseComparisonOperator] |
| 713 |
class C: |
| 714 |
def __gt__(self, x: 'A') -> object: pass |
| 715 |
class A: |
| 716 |
def __lt__(self, x: C) -> int: pass # E: Signatures of "__lt__" of "A" and "__gt__" of "C" are unsafely overlapping |
| 717 |
class B: |
| 718 |
def __gt__(self, x: A) -> str: pass |
| 719 |
s: str |
| 720 |
n: int |
| 721 |
if int(): |
| 722 |
n = A() < C() |
| 723 |
s = A() < B() |
| 724 |
if int(): |
| 725 |
n = A() < B() # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 726 |
s = object() < B() # E: Unsupported operand types for > ("B" and "object") |
| 727 |
|
| 728 |
[case testReversibleComparisonWithExtraArgument] |
| 729 |
class C: |
| 730 |
def __lt__(self, o: object, x: str = "") -> int: ... |
| 731 |
|
| 732 |
[case testErrorContextAndBinaryOperators] |
| 733 |
import typing |
| 734 |
class A: |
| 735 |
def __getitem__(self, i: str) -> int: pass |
| 736 |
def f() -> None: |
| 737 |
A()[1] # Error |
| 738 |
class B: |
| 739 |
A()[1] # Error |
| 740 |
A()[1] # Error |
| 741 |
[out] |
| 742 |
main:5: error: Invalid index type "int" for "A"; expected type "str" |
| 743 |
main:7: error: Invalid index type "int" for "A"; expected type "str" |
| 744 |
main:8: error: Invalid index type "int" for "A"; expected type "str" |
| 745 |
|
| 746 |
[case testErrorContextAndBinaryOperators2] |
| 747 |
import m |
| 748 |
[file m.py] |
| 749 |
import typing |
| 750 |
class A: |
| 751 |
def __getitem__(self, i: str) -> int: pass |
| 752 |
def f() -> None: |
| 753 |
A()[1] # Error |
| 754 |
class B: |
| 755 |
A()[1] # Error |
| 756 |
A()[1] # Error |
| 757 |
[out] |
| 758 |
tmp/m.py:5: error: Invalid index type "int" for "A"; expected type "str" |
| 759 |
tmp/m.py:7: error: Invalid index type "int" for "A"; expected type "str" |
| 760 |
tmp/m.py:8: error: Invalid index type "int" for "A"; expected type "str" |
| 761 |
|
| 762 |
|
| 763 |
[case testDivmod] |
| 764 |
# flags: --disable-error-code=used-before-def |
| 765 |
from typing import Tuple, Union, SupportsInt |
| 766 |
_Decimal = Union[Decimal, int] |
| 767 |
class Decimal(SupportsInt): |
| 768 |
def __init__(self, int) -> None: ... |
| 769 |
def __divmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ... |
| 770 |
def __rdivmod__(self, other: _Decimal) -> Tuple[Decimal, Decimal]: ... |
| 771 |
|
| 772 |
i = 8 |
| 773 |
f = 8.0 |
| 774 |
d = Decimal(8) |
| 775 |
|
| 776 |
reveal_type(divmod(i, i)) # N: Revealed type is "tuple[builtins.int, builtins.int]" |
| 777 |
reveal_type(divmod(f, i)) # N: Revealed type is "tuple[builtins.float, builtins.float]" |
| 778 |
reveal_type(divmod(d, i)) # N: Revealed type is "tuple[__main__.Decimal, __main__.Decimal]" |
| 779 |
|
| 780 |
reveal_type(divmod(i, f)) # N: Revealed type is "tuple[builtins.float, builtins.float]" |
| 781 |
reveal_type(divmod(f, f)) # N: Revealed type is "tuple[builtins.float, builtins.float]" |
| 782 |
divmod(d, f) # E: Unsupported operand types for divmod ("Decimal" and "float") |
| 783 |
|
| 784 |
reveal_type(divmod(i, d)) # N: Revealed type is "tuple[__main__.Decimal, __main__.Decimal]" |
| 785 |
divmod(f, d) # E: Unsupported operand types for divmod ("float" and "Decimal") |
| 786 |
reveal_type(divmod(d, d)) # N: Revealed type is "tuple[__main__.Decimal, __main__.Decimal]" |
| 787 |
|
| 788 |
# Now some bad calls |
| 789 |
divmod() # E: "divmod" expects 2 arguments \ |
| 790 |
# E: Missing positional arguments "_x", "_y" in call to "divmod" |
| 791 |
divmod(7) # E: "divmod" expects 2 arguments \ |
| 792 |
# E: Missing positional argument "_y" in call to "divmod" |
| 793 |
divmod(7, 8, 9) # E: "divmod" expects 2 arguments \ |
| 794 |
# E: Too many arguments for "divmod" |
| 795 |
divmod(_x=7, _y=9) # E: "divmod" must be called with 2 positional arguments |
| 796 |
|
| 797 |
divmod('foo', 'foo') # E: Unsupported left operand type for divmod ("str") |
| 798 |
divmod(i, 'foo') # E: Unsupported operand types for divmod ("int" and "str") |
| 799 |
divmod(f, 'foo') # E: Unsupported operand types for divmod ("float" and "str") |
| 800 |
divmod(d, 'foo') # E: Unsupported operand types for divmod ("Decimal" and "str") |
| 801 |
|
| 802 |
divmod('foo', i) # E: Unsupported operand types for divmod ("str" and "int") |
| 803 |
divmod('foo', f) # E: Unsupported operand types for divmod ("str" and "float") |
| 804 |
divmod('foo', d) # E: Unsupported operand types for divmod ("str" and "Decimal") |
| 805 |
|
| 806 |
[builtins fixtures/divmod.pyi] |
| 807 |
[typing fixtures/typing-medium.pyi] |
| 808 |
|
| 809 |
|
| 810 |
-- Unary operators |
| 811 |
-- --------------- |
| 812 |
|
| 813 |
|
| 814 |
[case testUnaryMinus] |
| 815 |
a: A |
| 816 |
b: B |
| 817 |
if int(): |
| 818 |
a = -a # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 819 |
if int(): |
| 820 |
b = -b # E: Unsupported operand type for unary - ("B") |
| 821 |
if int(): |
| 822 |
b = -a |
| 823 |
|
| 824 |
class A: |
| 825 |
def __neg__(self) -> 'B': |
| 826 |
pass |
| 827 |
class B: |
| 828 |
pass |
| 829 |
[builtins fixtures/tuple.pyi] |
| 830 |
|
| 831 |
[case testUnaryPlus] |
| 832 |
a: A |
| 833 |
b: B |
| 834 |
if int(): |
| 835 |
a = +a # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 836 |
if int(): |
| 837 |
b = +b # E: Unsupported operand type for unary + ("B") |
| 838 |
if int(): |
| 839 |
b = +a |
| 840 |
|
| 841 |
class A: |
| 842 |
def __pos__(self) -> 'B': |
| 843 |
pass |
| 844 |
class B: |
| 845 |
pass |
| 846 |
[builtins fixtures/tuple.pyi] |
| 847 |
|
| 848 |
[case testUnaryNot] |
| 849 |
a: A |
| 850 |
b: bool |
| 851 |
if int(): |
| 852 |
a = not b # E: Incompatible types in assignment (expression has type "bool", variable has type "A") |
| 853 |
if int(): |
| 854 |
b = not a |
| 855 |
if int(): |
| 856 |
b = not b |
| 857 |
class A: |
| 858 |
pass |
| 859 |
[builtins fixtures/bool.pyi] |
| 860 |
|
| 861 |
[case testUnaryBitwiseNeg] |
| 862 |
a: A |
| 863 |
b: B |
| 864 |
if int(): |
| 865 |
a = ~a # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 866 |
if int(): |
| 867 |
b = ~b # E: Unsupported operand type for ~ ("B") |
| 868 |
if int(): |
| 869 |
b = ~a |
| 870 |
|
| 871 |
class A: |
| 872 |
def __invert__(self) -> 'B': |
| 873 |
pass |
| 874 |
class B: |
| 875 |
pass |
| 876 |
|
| 877 |
|
| 878 |
-- Indexing |
| 879 |
-- -------- |
| 880 |
[builtins fixtures/tuple.pyi] |
| 881 |
|
| 882 |
|
| 883 |
[case testIndexing] |
| 884 |
a: A |
| 885 |
b: B |
| 886 |
c: C |
| 887 |
if int(): |
| 888 |
c = a[c] # E: Invalid index type "C" for "A"; expected type "B" |
| 889 |
if int(): |
| 890 |
a = a[b] # E: Incompatible types in assignment (expression has type "C", variable has type "A") |
| 891 |
if int(): |
| 892 |
c = b[a] # E: Value of type "B" is not indexable |
| 893 |
if int(): |
| 894 |
c = a[b] |
| 895 |
|
| 896 |
class A: |
| 897 |
def __getitem__(self, x: 'B') -> 'C': |
| 898 |
pass |
| 899 |
class B: pass |
| 900 |
class C: pass |
| 901 |
[builtins fixtures/tuple.pyi] |
| 902 |
|
| 903 |
[case testIndexingAsLvalue] |
| 904 |
a: A |
| 905 |
b: B |
| 906 |
c: C |
| 907 |
a[c] = c # Fail |
| 908 |
a[b] = a # Fail |
| 909 |
b[a] = c # Fail |
| 910 |
a[b] = c |
| 911 |
|
| 912 |
class A: |
| 913 |
def __setitem__(self, x: 'B', y: 'C') -> None: |
| 914 |
pass |
| 915 |
class B: |
| 916 |
pass |
| 917 |
class C: |
| 918 |
pass |
| 919 |
[builtins fixtures/tuple.pyi] |
| 920 |
[out] |
| 921 |
main:4: error: Invalid index type "C" for "A"; expected type "B" |
| 922 |
main:5: error: Incompatible types in assignment (expression has type "A", target has type "C") |
| 923 |
main:6: error: Unsupported target for indexed assignment ("B") |
| 924 |
|
| 925 |
[case testOverloadedIndexing] |
| 926 |
from foo import * |
| 927 |
[file foo.pyi] |
| 928 |
from typing import overload |
| 929 |
a: A |
| 930 |
b: B |
| 931 |
c: C |
| 932 |
a[b] |
| 933 |
a[c] |
| 934 |
a[1] # E: No overload variant of "__getitem__" of "A" matches argument type "int" \ |
| 935 |
# N: Possible overload variants: \ |
| 936 |
# N: def __getitem__(self, B, /) -> int \ |
| 937 |
# N: def __getitem__(self, C, /) -> str |
| 938 |
|
| 939 |
i: int |
| 940 |
s: str |
| 941 |
if int(): |
| 942 |
i = a[b] |
| 943 |
if int(): |
| 944 |
s = a[b] # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 945 |
if int(): |
| 946 |
i = a[c] # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 947 |
if int(): |
| 948 |
s = a[c] |
| 949 |
|
| 950 |
class A: |
| 951 |
@overload |
| 952 |
def __getitem__(self, x: 'B') -> int: |
| 953 |
pass |
| 954 |
@overload |
| 955 |
def __getitem__(self, x: 'C') -> str: |
| 956 |
pass |
| 957 |
class B: pass |
| 958 |
class C: pass |
| 959 |
[builtins fixtures/tuple.pyi] |
| 960 |
[out] |
| 961 |
|
| 962 |
|
| 963 |
-- Cast expression |
| 964 |
-- --------------- |
| 965 |
|
| 966 |
|
| 967 |
[case testCastExpressions] |
| 968 |
from typing import cast, Any |
| 969 |
class A: pass |
| 970 |
class B: pass |
| 971 |
class C(A): pass |
| 972 |
a: A |
| 973 |
b: B |
| 974 |
c: C |
| 975 |
|
| 976 |
if int(): |
| 977 |
a = cast(A, a()) # E: "A" not callable |
| 978 |
if int(): |
| 979 |
a = cast(Any, a()) # E: "A" not callable |
| 980 |
b = cast(A, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 981 |
|
| 982 |
if int(): |
| 983 |
a = cast(A, b) |
| 984 |
if int(): |
| 985 |
a = cast(A, a) |
| 986 |
c = cast(C, a) |
| 987 |
if int(): |
| 988 |
a = cast(A, c) |
| 989 |
if int(): |
| 990 |
a = cast(Any, b) |
| 991 |
b = cast(Any, a) |
| 992 |
[builtins fixtures/tuple.pyi] |
| 993 |
[out] |
| 994 |
|
| 995 |
[case testAnyCast] |
| 996 |
from typing import cast, Any |
| 997 |
a: A |
| 998 |
b: B |
| 999 |
a = cast(Any, a()) # Fail |
| 1000 |
a = cast(Any, b) |
| 1001 |
b = cast(Any, a) |
| 1002 |
class A: pass |
| 1003 |
class B: pass |
| 1004 |
[builtins fixtures/tuple.pyi] |
| 1005 |
[out] |
| 1006 |
main:4: error: "A" not callable |
| 1007 |
|
| 1008 |
-- assert_type() |
| 1009 |
|
| 1010 |
[case testAssertType] |
| 1011 |
from typing import assert_type, Any, Literal |
| 1012 |
a: int = 1 |
| 1013 |
returned = assert_type(a, int) |
| 1014 |
reveal_type(returned) # N: Revealed type is "builtins.int" |
| 1015 |
assert_type(a, str) # E: Expression is of type "int", not "str" |
| 1016 |
assert_type(a, Any) # E: Expression is of type "int", not "Any" |
| 1017 |
assert_type(a, Literal[1]) # E: Expression is of type "int", not "Literal[1]" |
| 1018 |
assert_type(42, Literal[42]) |
| 1019 |
assert_type(42, int) # E: Expression is of type "Literal[42]", not "int" |
| 1020 |
[builtins fixtures/tuple.pyi] |
| 1021 |
|
| 1022 |
[case testAssertTypeGeneric] |
| 1023 |
from typing import assert_type, Literal, TypeVar, Generic |
| 1024 |
T = TypeVar("T") |
| 1025 |
def f(x: T) -> T: return x |
| 1026 |
assert_type(f(1), int) |
| 1027 |
class Gen(Generic[T]): |
| 1028 |
def __new__(cls, obj: T) -> Gen[T]: ... |
| 1029 |
assert_type(Gen(1), Gen[int]) |
| 1030 |
# With type context, it infers Gen[Literal[1]] instead. |
| 1031 |
y: Gen[Literal[1]] = assert_type(Gen(1), Gen[Literal[1]]) |
| 1032 |
|
| 1033 |
[builtins fixtures/tuple.pyi] |
| 1034 |
|
| 1035 |
[case testAssertTypeUncheckedFunction] |
| 1036 |
from typing import Literal, assert_type |
| 1037 |
def f(): |
| 1038 |
x = 42 |
| 1039 |
assert_type(x, Literal[42]) |
| 1040 |
[out] |
| 1041 |
main:4: error: Expression is of type "Any", not "Literal[42]" |
| 1042 |
main:4: note: "assert_type" expects everything to be "Any" in unchecked functions |
| 1043 |
[builtins fixtures/tuple.pyi] |
| 1044 |
|
| 1045 |
[case testAssertTypeUncheckedFunctionWithUntypedCheck] |
| 1046 |
# flags: --check-untyped-defs |
| 1047 |
from typing import Literal, assert_type |
| 1048 |
def f(): |
| 1049 |
x = 42 |
| 1050 |
assert_type(x, Literal[42]) |
| 1051 |
[out] |
| 1052 |
main:5: error: Expression is of type "int", not "Literal[42]" |
| 1053 |
[builtins fixtures/tuple.pyi] |
| 1054 |
|
| 1055 |
[case testAssertTypeNoPromoteUnion] |
| 1056 |
from typing import Union, assert_type |
| 1057 |
|
| 1058 |
Scalar = Union[int, bool, bytes, bytearray] |
| 1059 |
|
| 1060 |
|
| 1061 |
def reduce_it(s: Scalar) -> Scalar: |
| 1062 |
return s |
| 1063 |
|
| 1064 |
assert_type(reduce_it(True), Scalar) |
| 1065 |
[builtins fixtures/tuple.pyi] |
| 1066 |
|
| 1067 |
[case testAssertTypeWithDeferredNodes] |
| 1068 |
from typing import Callable, TypeVar, assert_type |
| 1069 |
|
| 1070 |
T = TypeVar("T") |
| 1071 |
|
| 1072 |
def dec(f: Callable[[], T]) -> Callable[[], T]: |
| 1073 |
return f |
| 1074 |
|
| 1075 |
def func() -> None: |
| 1076 |
some = _inner_func() |
| 1077 |
assert_type(some, int) |
| 1078 |
|
| 1079 |
@dec |
| 1080 |
def _inner_func() -> int: |
| 1081 |
return 1 |
| 1082 |
[builtins fixtures/tuple.pyi] |
| 1083 |
|
| 1084 |
-- None return type |
| 1085 |
-- ---------------- |
| 1086 |
|
| 1087 |
|
| 1088 |
[case testNoneReturnTypeBasics] |
| 1089 |
def f() -> None: |
| 1090 |
pass |
| 1091 |
|
| 1092 |
class A: |
| 1093 |
def g(self, x: object) -> None: |
| 1094 |
pass |
| 1095 |
def __call__(self) -> None: |
| 1096 |
pass |
| 1097 |
|
| 1098 |
a: A |
| 1099 |
o: object |
| 1100 |
if int(): |
| 1101 |
a = f() # E: "f" does not return a value (it only ever returns None) \ |
| 1102 |
# E: Incompatible types in assignment (expression has type "None", variable has type "A") |
| 1103 |
if int(): |
| 1104 |
o = a() # E: Function does not return a value (it only ever returns None) |
| 1105 |
if int(): |
| 1106 |
o = A().g(a) # E: "g" of "A" does not return a value (it only ever returns None) |
| 1107 |
if int(): |
| 1108 |
o = A.g(a, a) # E: "g" of "A" does not return a value (it only ever returns None) |
| 1109 |
A().g(f()) # E: "f" does not return a value (it only ever returns None) |
| 1110 |
x: A = f() # E: "f" does not return a value (it only ever returns None) \ |
| 1111 |
# E: Incompatible types in assignment (expression has type "None", variable has type "A") |
| 1112 |
f() |
| 1113 |
A().g(a) |
| 1114 |
[builtins fixtures/tuple.pyi] |
| 1115 |
|
| 1116 |
[case testNoneReturnTypeWithStatements] |
| 1117 |
import typing |
| 1118 |
def f() -> None: pass |
| 1119 |
|
| 1120 |
if f(): # E: "f" does not return a value (it only ever returns None) |
| 1121 |
pass |
| 1122 |
elif f(): # E: "f" does not return a value (it only ever returns None) |
| 1123 |
pass |
| 1124 |
while f(): # E: "f" does not return a value (it only ever returns None) |
| 1125 |
pass |
| 1126 |
def g() -> object: |
| 1127 |
return f() # E: "f" does not return a value (it only ever returns None) |
| 1128 |
raise f() # E: Exception must be derived from BaseException \ |
| 1129 |
# E: "f" does not return a value (it only ever returns None) |
| 1130 |
[builtins fixtures/exception.pyi] |
| 1131 |
|
| 1132 |
[case testNoneReturnTypeWithExpressions] |
| 1133 |
from typing import cast |
| 1134 |
|
| 1135 |
def f() -> None: pass |
| 1136 |
class A: |
| 1137 |
def __add__(self, x: 'A') -> 'A': pass |
| 1138 |
|
| 1139 |
a: A |
| 1140 |
[f()] # E: "f" does not return a value (it only ever returns None) |
| 1141 |
f() + a # E: "f" does not return a value (it only ever returns None) \ |
| 1142 |
# E: Unsupported left operand type for + ("None") |
| 1143 |
a + f() # E: "f" does not return a value (it only ever returns None) \ |
| 1144 |
# E: Unsupported operand types for + ("A" and "None") |
| 1145 |
f() == a # E: "f" does not return a value (it only ever returns None) |
| 1146 |
a != f() # E: Unsupported left operand type for != ("A") \ |
| 1147 |
# E: "f" does not return a value (it only ever returns None) |
| 1148 |
cast(A, f()) |
| 1149 |
f().foo # E: "f" does not return a value (it only ever returns None) \ |
| 1150 |
# E: "None" has no attribute "foo" |
| 1151 |
[builtins fixtures/list.pyi] |
| 1152 |
|
| 1153 |
[case testNoneReturnTypeWithExpressions2] |
| 1154 |
import typing |
| 1155 |
|
| 1156 |
def f() -> None: pass |
| 1157 |
class A: |
| 1158 |
def __add__(self, x: 'A') -> 'A': |
| 1159 |
pass |
| 1160 |
|
| 1161 |
a: A |
| 1162 |
b: bool |
| 1163 |
f() in a # E: "f" does not return a value (it only ever returns None) \ |
| 1164 |
# E: Unsupported right operand type for in ("A") |
| 1165 |
a < f() # E: Unsupported left operand type for < ("A") \ |
| 1166 |
# E: "f" does not return a value (it only ever returns None) |
| 1167 |
f() <= a # E: "f" does not return a value (it only ever returns None) \ |
| 1168 |
# E: Unsupported left operand type for <= ("None") |
| 1169 |
a in f() # E: Unsupported right operand type for in ("None") \ |
| 1170 |
# E: "f" does not return a value (it only ever returns None) |
| 1171 |
-f() # E: Unsupported operand type for unary - ("None") \ |
| 1172 |
# E: "f" does not return a value (it only ever returns None) |
| 1173 |
not f() # E: "f" does not return a value (it only ever returns None) |
| 1174 |
f() and b # E: "f" does not return a value (it only ever returns None) |
| 1175 |
b or f() # E: "f" does not return a value (it only ever returns None) |
| 1176 |
[builtins fixtures/bool.pyi] |
| 1177 |
|
| 1178 |
|
| 1179 |
-- Slicing |
| 1180 |
-- ------- |
| 1181 |
|
| 1182 |
|
| 1183 |
[case testGetSlice] |
| 1184 |
a: A |
| 1185 |
b: B |
| 1186 |
if int(): |
| 1187 |
a = a[1:2] # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 1188 |
if int(): |
| 1189 |
a = a[1:] # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 1190 |
if int(): |
| 1191 |
a = a[:2] # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 1192 |
if int(): |
| 1193 |
a = a[:] # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 1194 |
|
| 1195 |
if int(): |
| 1196 |
b = a[1:2] |
| 1197 |
if int(): |
| 1198 |
b = a[1:] |
| 1199 |
if int(): |
| 1200 |
b = a[:2] |
| 1201 |
if int(): |
| 1202 |
b = a[:] |
| 1203 |
|
| 1204 |
class A: |
| 1205 |
def __getitem__(self, s: slice) -> 'B': pass |
| 1206 |
class B: pass |
| 1207 |
[builtins fixtures/slice.pyi] |
| 1208 |
|
| 1209 |
[case testSlicingWithInvalidBase] |
| 1210 |
|
| 1211 |
a: A |
| 1212 |
a[1:2] # E: Invalid index type "slice[int, int, None]" for "A"; expected type "int" |
| 1213 |
a[:] # E: Invalid index type "slice[None, None, None]" for "A"; expected type "int" |
| 1214 |
class A: |
| 1215 |
def __getitem__(self, n: int) -> 'A': pass |
| 1216 |
[builtins fixtures/slice.pyi] |
| 1217 |
|
| 1218 |
[case testSlicingWithNonindexable] |
| 1219 |
|
| 1220 |
o: object |
| 1221 |
o[1:2] # E: Value of type "object" is not indexable |
| 1222 |
o[:] # E: Value of type "object" is not indexable |
| 1223 |
[builtins fixtures/slice.pyi] |
| 1224 |
|
| 1225 |
[case testNonIntSliceBounds] |
| 1226 |
from typing import Any |
| 1227 |
a: Any |
| 1228 |
o: object |
| 1229 |
a[o:1] |
| 1230 |
a[1:o] |
| 1231 |
a[o:] |
| 1232 |
a[:o] |
| 1233 |
[builtins fixtures/slice.pyi] |
| 1234 |
|
| 1235 |
[case testSliceSupportsIndex] |
| 1236 |
import typing_extensions |
| 1237 |
class Index: |
| 1238 |
def __init__(self, value: int) -> None: |
| 1239 |
self.value = value |
| 1240 |
def __index__(self) -> int: |
| 1241 |
return self.value |
| 1242 |
|
| 1243 |
c = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 1244 |
reveal_type(c[Index(0):Index(5)]) # N: Revealed type is "builtins.list[builtins.int]" |
| 1245 |
[file typing_extensions.pyi] |
| 1246 |
from typing import Protocol |
| 1247 |
class SupportsIndex(Protocol): |
| 1248 |
def __index__(self) -> int: ... |
| 1249 |
[builtins fixtures/slice.pyi] |
| 1250 |
|
| 1251 |
[case testNoneSliceBounds] |
| 1252 |
from typing import Any |
| 1253 |
a: Any |
| 1254 |
a[None:1] |
| 1255 |
a[1:None] |
| 1256 |
a[None:] |
| 1257 |
a[:None] |
| 1258 |
[builtins fixtures/slice.pyi] |
| 1259 |
|
| 1260 |
[case testNoneSliceBoundsWithStrictOptional] |
| 1261 |
from typing import Any |
| 1262 |
a: Any |
| 1263 |
a[None:1] |
| 1264 |
a[1:None] |
| 1265 |
a[None:] |
| 1266 |
a[:None] |
| 1267 |
[builtins fixtures/slice.pyi] |
| 1268 |
|
| 1269 |
|
| 1270 |
-- Lambdas |
| 1271 |
-- ------- |
| 1272 |
|
| 1273 |
|
| 1274 |
[case testTrivialLambda] |
| 1275 |
from typing import Callable |
| 1276 |
f = lambda: 1 # type: Callable[[], int] |
| 1277 |
if int(): |
| 1278 |
f = lambda: ''.x # E: "str" has no attribute "x" |
| 1279 |
if int(): |
| 1280 |
f = lambda: '' \ |
| 1281 |
# E: Incompatible types in assignment (expression has type "Callable[[], str]", variable has type "Callable[[], int]") \ |
| 1282 |
# E: Incompatible return value type (got "str", expected "int") |
| 1283 |
|
| 1284 |
[case testVoidLambda] |
| 1285 |
import typing |
| 1286 |
def void() -> None: |
| 1287 |
pass |
| 1288 |
x = lambda: void() # type: typing.Callable[[], None] |
| 1289 |
|
| 1290 |
[case testNoCrashOnLambdaGenerator] |
| 1291 |
# flags: --no-strict-optional |
| 1292 |
from typing import Iterator, Callable |
| 1293 |
|
| 1294 |
# These should not crash |
| 1295 |
lambda: (yield) |
| 1296 |
|
| 1297 |
gen: Callable[[], Iterator[str]] |
| 1298 |
gen = (lambda: (yield 1)) # E: Incompatible types in "yield" (actual type "int", expected type "str") |
| 1299 |
|
| 1300 |
def fun(cb: Callable[[], Iterator[str]]) -> None: |
| 1301 |
pass |
| 1302 |
fun(lambda: (yield from [1])) # E: Incompatible types in "yield from" (actual type "int", expected type "str") |
| 1303 |
[builtins fixtures/list.pyi] |
| 1304 |
[out] |
| 1305 |
|
| 1306 |
[case testLambdaAndReachability] |
| 1307 |
def f() -> None: |
| 1308 |
aa = [] |
| 1309 |
y = lambda x: 1 |
| 1310 |
aa.append(1) |
| 1311 |
1() # E: "int" not callable |
| 1312 |
[builtins fixtures/list.pyi] |
| 1313 |
|
| 1314 |
|
| 1315 |
-- List comprehensions |
| 1316 |
-- ------------------- |
| 1317 |
|
| 1318 |
|
| 1319 |
[case testSimpleListComprehension] |
| 1320 |
from typing import List |
| 1321 |
a: List[A] |
| 1322 |
a = [x for x in a] |
| 1323 |
b = [x for x in a] # type: List[B] # E: List comprehension has incompatible type List[A]; expected List[B] |
| 1324 |
class A: pass |
| 1325 |
class B: pass |
| 1326 |
[builtins fixtures/for.pyi] |
| 1327 |
|
| 1328 |
[case testSimpleListComprehensionNestedTuples] |
| 1329 |
from typing import List, Tuple |
| 1330 |
l: List[Tuple[A, Tuple[A, B]]] |
| 1331 |
a = [a2 for a1, (a2, b1) in l] # type: List[A] |
| 1332 |
b = [a2 for a1, (a2, b1) in l] # type: List[B] # E: List comprehension has incompatible type List[A]; expected List[B] |
| 1333 |
class A: pass |
| 1334 |
class B: pass |
| 1335 |
[builtins fixtures/for.pyi] |
| 1336 |
|
| 1337 |
[case testSimpleListComprehensionNestedTuples2] |
| 1338 |
from typing import List, Tuple |
| 1339 |
l: List[Tuple[int, Tuple[int, str]]] |
| 1340 |
a = [f(d) for d, (i, s) in l] |
| 1341 |
b = [f(s) for d, (i, s) in l] # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1342 |
|
| 1343 |
def f(x: int): pass |
| 1344 |
[builtins fixtures/for.pyi] |
| 1345 |
|
| 1346 |
[case testListComprehensionWithNonDirectMapping] |
| 1347 |
from typing import List |
| 1348 |
a: List[A] |
| 1349 |
b: List[B] |
| 1350 |
if int(): |
| 1351 |
b = [f(x) for x in a] |
| 1352 |
if int(): |
| 1353 |
a = [f(x) for x in a] # E: List comprehension has incompatible type List[B]; expected List[A] |
| 1354 |
([f(x) for x in b]) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 1355 |
class A: pass |
| 1356 |
class B: pass |
| 1357 |
def f(a: A) -> B: pass |
| 1358 |
[builtins fixtures/for.pyi] |
| 1359 |
|
| 1360 |
[case testErrorInListComprehensionCondition] |
| 1361 |
from typing import List |
| 1362 |
a: List[A] |
| 1363 |
a = [x for x in a if x()] # E: "A" not callable |
| 1364 |
class A: pass |
| 1365 |
[builtins fixtures/for.pyi] |
| 1366 |
|
| 1367 |
[case testTypeInferenceOfListComprehension] |
| 1368 |
from typing import List |
| 1369 |
a: List[A] |
| 1370 |
o = [x for x in a] # type: List[object] |
| 1371 |
class A: pass |
| 1372 |
[builtins fixtures/for.pyi] |
| 1373 |
|
| 1374 |
[case testSimpleListComprehensionInClassBody] |
| 1375 |
from typing import List |
| 1376 |
class A: |
| 1377 |
a: List[A] |
| 1378 |
a = [x for x in a] |
| 1379 |
b = [x for x in a] # type: List[B] # E: List comprehension has incompatible type List[A]; expected List[B] |
| 1380 |
class B: pass |
| 1381 |
[builtins fixtures/for.pyi] |
| 1382 |
[out] |
| 1383 |
|
| 1384 |
|
| 1385 |
-- Set comprehension |
| 1386 |
-- ----------------- |
| 1387 |
|
| 1388 |
|
| 1389 |
[case testSimpleSetComprehension] |
| 1390 |
from typing import Set |
| 1391 |
a: Set[A] |
| 1392 |
a = {x for x in a} |
| 1393 |
b = {x for x in a} # type: Set[B] # E: Set comprehension has incompatible type Set[A]; expected Set[B] |
| 1394 |
class A: pass |
| 1395 |
class B: pass |
| 1396 |
[builtins fixtures/set.pyi] |
| 1397 |
|
| 1398 |
|
| 1399 |
-- Dictionary comprehension |
| 1400 |
-- ------------------------ |
| 1401 |
|
| 1402 |
|
| 1403 |
[case testSimpleDictionaryComprehension] |
| 1404 |
from typing import Dict, List, Tuple |
| 1405 |
abd: Dict[A, B] |
| 1406 |
abl: List[Tuple[A, B]] |
| 1407 |
abd = {a: b for a, b in abl} |
| 1408 |
x = {a: b for a, b in abl} # type: Dict[B, A] |
| 1409 |
y = {a: b for a, b in abl} # type: A |
| 1410 |
class A: pass |
| 1411 |
class B: pass |
| 1412 |
[builtins fixtures/dict.pyi] |
| 1413 |
[out] |
| 1414 |
main:5: error: Key expression in dictionary comprehension has incompatible type "A"; expected type "B" |
| 1415 |
main:5: error: Value expression in dictionary comprehension has incompatible type "B"; expected type "A" |
| 1416 |
main:6: error: Incompatible types in assignment (expression has type "dict[A, B]", variable has type "A") |
| 1417 |
|
| 1418 |
|
| 1419 |
[case testDictionaryComprehensionWithNonDirectMapping] |
| 1420 |
from typing import Dict, List, Tuple |
| 1421 |
abd: Dict[A, B] |
| 1422 |
abl: List[Tuple[A, B]] |
| 1423 |
abd = {a: f(b) for a, b in abl} |
| 1424 |
class A: pass |
| 1425 |
class B: pass |
| 1426 |
class C: pass |
| 1427 |
def f(b: A) -> C: pass |
| 1428 |
[builtins fixtures/dict.pyi] |
| 1429 |
[out] |
| 1430 |
main:4: error: Value expression in dictionary comprehension has incompatible type "C"; expected type "B" |
| 1431 |
main:4: error: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 1432 |
|
| 1433 |
|
| 1434 |
-- Generator expressions |
| 1435 |
-- --------------------- |
| 1436 |
|
| 1437 |
|
| 1438 |
[case testSimpleGeneratorExpression] |
| 1439 |
from typing import Iterator |
| 1440 |
# The implementation is mostly identical to list comprehensions, so only a few |
| 1441 |
# test cases is ok. |
| 1442 |
a: Iterator[int] |
| 1443 |
if int(): |
| 1444 |
a = (x for x in a) |
| 1445 |
b: Iterator[str] |
| 1446 |
if int(): |
| 1447 |
b = (x for x in a) # E: Generator has incompatible item type "int"; expected "str" |
| 1448 |
[builtins fixtures/for.pyi] |
| 1449 |
|
| 1450 |
[case testGeneratorIncompatibleErrorMessage] |
| 1451 |
from typing import Callable, Iterator, List |
| 1452 |
|
| 1453 |
a = [] # type: List[Callable[[], str]] |
| 1454 |
b: Iterator[Callable[[], int]] |
| 1455 |
if int(): |
| 1456 |
b = (x for x in a) # E: Generator has incompatible item type "Callable[[], str]"; expected "Callable[[], int]" |
| 1457 |
[builtins fixtures/list.pyi] |
| 1458 |
|
| 1459 |
[case testGeneratorNoSpuriousError_no_native_parse] |
| 1460 |
from typing import Iterable, overload |
| 1461 |
|
| 1462 |
@overload |
| 1463 |
def take_iterable(iterable: Iterable[bool], /) -> None: ... |
| 1464 |
@overload |
| 1465 |
def take_iterable(iterable: Iterable[int], /) -> None: ... |
| 1466 |
def take_iterable(iterable): pass |
| 1467 |
|
| 1468 |
take_iterable(1 for _ in []) |
| 1469 |
take_iterable(reveal_type(1 for _ in [])) # N: Revealed type is "typing.Generator[builtins.int, None, None]" |
| 1470 |
|
| 1471 |
# NOTE: Type is revealed for every overload tried |
| 1472 |
# TODO: Overload shouldn't fail if expression contains an error that shouldn't affect the inferred type. |
| 1473 |
take_iterable(reveal_type(1 if (-"") else 1 for _ in [])) # N: Revealed type is "typing.Generator[builtins.int, None, None]" \ |
| 1474 |
# N: Revealed type is "typing.Generator[builtins.bool, None, None]" \ |
| 1475 |
# E: Generator has incompatible item type "int"; expected "bool" \ |
| 1476 |
# E: Unsupported operand type for unary - ("str") |
| 1477 |
|
| 1478 |
[builtins fixtures/for.pyi] |
| 1479 |
|
| 1480 |
-- Conditional expressions |
| 1481 |
-- ----------------------- |
| 1482 |
|
| 1483 |
|
| 1484 |
[case testSimpleConditionalExpression] |
| 1485 |
import typing |
| 1486 |
y = '' |
| 1487 |
x = 1 if y else 2 |
| 1488 |
if int(): |
| 1489 |
x = 3 |
| 1490 |
if int(): |
| 1491 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 1492 |
|
| 1493 |
[case testConditionalExpressionWithEmptyCondition] |
| 1494 |
import typing |
| 1495 |
def f() -> None: pass |
| 1496 |
x = 1 if f() else 2 # E: "f" does not return a value (it only ever returns None) |
| 1497 |
|
| 1498 |
[case testConditionalExpressionWithSubtyping] |
| 1499 |
import typing |
| 1500 |
class A: pass |
| 1501 |
class B(A): pass |
| 1502 |
x = B() if bool() else A() |
| 1503 |
if int(): |
| 1504 |
x = A() |
| 1505 |
if int(): |
| 1506 |
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "A") |
| 1507 |
y = A() if bool() else B() |
| 1508 |
if int(): |
| 1509 |
y = A() |
| 1510 |
if int(): |
| 1511 |
y = '' # E: Incompatible types in assignment (expression has type "str", variable has type "A") |
| 1512 |
[builtins fixtures/bool.pyi] |
| 1513 |
|
| 1514 |
[case testConditionalExpressionAndTypeContext] |
| 1515 |
import typing |
| 1516 |
x = [1] if bool() else [] |
| 1517 |
if int(): |
| 1518 |
x = [1] |
| 1519 |
if int(): |
| 1520 |
x = ['x'] # E: List item 0 has incompatible type "str"; expected "int" |
| 1521 |
[builtins fixtures/list.pyi] |
| 1522 |
|
| 1523 |
[case testConditionalExpressionUnion] |
| 1524 |
from typing import Union |
| 1525 |
reveal_type(1 if bool() else 2) # N: Revealed type is "Literal[1]? | Literal[2]?" |
| 1526 |
reveal_type(1 if bool() else '') # N: Revealed type is "Literal[1]? | Literal['']?" |
| 1527 |
x: Union[int, str] = reveal_type(1 if bool() else '') # N: Revealed type is "Literal[1]? | Literal['']?" |
| 1528 |
class A: |
| 1529 |
pass |
| 1530 |
class B(A): |
| 1531 |
pass |
| 1532 |
class C: |
| 1533 |
pass |
| 1534 |
class D(A): |
| 1535 |
pass |
| 1536 |
a = A() |
| 1537 |
b = B() |
| 1538 |
c = C() |
| 1539 |
d = D() |
| 1540 |
reveal_type(a if bool() else b) # N: Revealed type is "__main__.A" |
| 1541 |
reveal_type(b if bool() else c) # N: Revealed type is "__main__.B | __main__.C" |
| 1542 |
reveal_type(c if bool() else b) # N: Revealed type is "__main__.C | __main__.B" |
| 1543 |
reveal_type(c if bool() else a) # N: Revealed type is "__main__.C | __main__.A" |
| 1544 |
reveal_type(d if bool() else b) # N: Revealed type is "__main__.D | __main__.B" |
| 1545 |
[builtins fixtures/bool.pyi] |
| 1546 |
|
| 1547 |
[case testConditionalExpressionUnionWithAny] |
| 1548 |
from typing import Union, Any |
| 1549 |
a: Any |
| 1550 |
x: Union[int, str] = reveal_type(a if int() else 1) # N: Revealed type is "Any | Literal[1]?" |
| 1551 |
reveal_type(a if int() else 1) # N: Revealed type is "Any | Literal[1]?" |
| 1552 |
|
| 1553 |
[case testConditionalExpressionStatementNoReturn] |
| 1554 |
from typing import List, Union |
| 1555 |
x = [] |
| 1556 |
y = "" |
| 1557 |
x.append(y) if bool() else x.append(y) |
| 1558 |
z = x.append(y) if bool() else x.append(y) # E: "append" of "list" does not return a value (it only ever returns None) |
| 1559 |
[builtins fixtures/list.pyi] |
| 1560 |
|
| 1561 |
[case testConditionalExpressionWithUnreachableBranches] |
| 1562 |
from typing import TypeVar |
| 1563 |
T = TypeVar("T", int, str) |
| 1564 |
def foo(x: T) -> T: |
| 1565 |
return x + 1 if isinstance(x, int) else x + "a" |
| 1566 |
[builtins fixtures/isinstancelist.pyi] |
| 1567 |
|
| 1568 |
-- Special cases |
| 1569 |
-- ------------- |
| 1570 |
|
| 1571 |
|
| 1572 |
[case testOperationsWithNonInstanceTypes] |
| 1573 |
from typing import cast |
| 1574 |
class A: |
| 1575 |
def __add__(self, a: 'A') -> 'A': pass |
| 1576 |
def f() -> None: |
| 1577 |
pass |
| 1578 |
a: A |
| 1579 |
None + a # E: Unsupported left operand type for + ("None") |
| 1580 |
f + a # E: Unsupported left operand type for + ("Callable[[], None]") |
| 1581 |
a + f # E: Unsupported operand types for + ("A" and "Callable[[], None]") |
| 1582 |
cast(A, f) |
| 1583 |
[case testOperatorMethodWithInvalidArgCount] |
| 1584 |
|
| 1585 |
a: A |
| 1586 |
a + a # Fail |
| 1587 |
|
| 1588 |
class A: |
| 1589 |
def __add__(self) -> 'A': |
| 1590 |
pass |
| 1591 |
[out] |
| 1592 |
main:3: error: Too many arguments for "__add__" of "A" |
| 1593 |
|
| 1594 |
[case testOperatorMethodAsVar] |
| 1595 |
from typing import Any |
| 1596 |
class A: |
| 1597 |
def __init__(self, _add: Any) -> None: |
| 1598 |
self.__add__ = _add |
| 1599 |
a: A |
| 1600 |
a + a |
| 1601 |
[out] |
| 1602 |
|
| 1603 |
[case testOperatorMethodAsVar2] |
| 1604 |
|
| 1605 |
class A: |
| 1606 |
def f(self, x: int) -> str: pass |
| 1607 |
__add__ = f |
| 1608 |
s: str |
| 1609 |
s = A() + 1 |
| 1610 |
A() + (A() + 1) |
| 1611 |
[out] |
| 1612 |
main:7: error: Argument 1 has incompatible type "str"; expected "int" |
| 1613 |
|
| 1614 |
[case testIndexedLvalueWithSubtypes] |
| 1615 |
a: A |
| 1616 |
b: B |
| 1617 |
c: C |
| 1618 |
a[c] = c |
| 1619 |
a[b] = c |
| 1620 |
a[c] = b |
| 1621 |
|
| 1622 |
class A: |
| 1623 |
def __setitem__(self, x: 'B', y: 'B') -> None: |
| 1624 |
pass |
| 1625 |
class B: |
| 1626 |
pass |
| 1627 |
class C(B): |
| 1628 |
pass |
| 1629 |
[builtins fixtures/tuple.pyi] |
| 1630 |
[out] |
| 1631 |
|
| 1632 |
|
| 1633 |
-- Ellipsis |
| 1634 |
-- -------- |
| 1635 |
|
| 1636 |
|
| 1637 |
[case testEllipsis] |
| 1638 |
|
| 1639 |
a: A |
| 1640 |
if str(): |
| 1641 |
a = ... # E: Incompatible types in assignment (expression has type "ellipsis", variable has type "A") |
| 1642 |
b = ... |
| 1643 |
c = ... |
| 1644 |
if str(): |
| 1645 |
b = c |
| 1646 |
....__class__ |
| 1647 |
....a # E: "ellipsis" has no attribute "a" |
| 1648 |
|
| 1649 |
class A: pass |
| 1650 |
[builtins fixtures/dict-full.pyi] |
| 1651 |
|
| 1652 |
|
| 1653 |
-- Yield expression |
| 1654 |
-- ---------------- |
| 1655 |
|
| 1656 |
|
| 1657 |
[case testYieldExpression] |
| 1658 |
def f(x: int) -> None: |
| 1659 |
x = yield f('') |
| 1660 |
x = 1 |
| 1661 |
[builtins fixtures/for.pyi] |
| 1662 |
[out] |
| 1663 |
main:1: error: The return type of a generator function should be "Generator" or one of its supertypes |
| 1664 |
main:2: error: "f" does not return a value (it only ever returns None) |
| 1665 |
main:2: error: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 1666 |
|
| 1667 |
[case testYieldExpressionWithNone] |
| 1668 |
from typing import Iterator |
| 1669 |
def f(x: int) -> Iterator[None]: |
| 1670 |
(yield) |
| 1671 |
[builtins fixtures/for.pyi] |
| 1672 |
[out] |
| 1673 |
|
| 1674 |
|
| 1675 |
-- Yield from expression |
| 1676 |
-- ---------------- |
| 1677 |
|
| 1678 |
|
| 1679 |
[case testYieldFromIteratorHasNoValue] |
| 1680 |
from typing import Iterator |
| 1681 |
def f() -> Iterator[int]: |
| 1682 |
yield 5 |
| 1683 |
def g() -> Iterator[int]: |
| 1684 |
a = yield from f() # E: Function does not return a value (it only ever returns None) |
| 1685 |
|
| 1686 |
[case testYieldFromGeneratorHasValue] |
| 1687 |
from typing import Iterator, Generator |
| 1688 |
def f() -> Generator[int, None, str]: |
| 1689 |
yield 5 |
| 1690 |
return "ham" |
| 1691 |
def g() -> Iterator[int]: |
| 1692 |
a = "string" |
| 1693 |
a = yield from f() |
| 1694 |
[out] |
| 1695 |
|
| 1696 |
[case testYieldFromTupleExpression] |
| 1697 |
from typing import Generator |
| 1698 |
def g() -> Generator[int, None, None]: |
| 1699 |
x = yield from () # E: Function does not return a value (it only ever returns None) |
| 1700 |
x = yield from (0, 1, 2) # E: Function does not return a value (it only ever returns None) |
| 1701 |
x = yield from (0, "ERROR") # E: Incompatible types in "yield from" (actual type "int | str", expected type "int") \ |
| 1702 |
# E: Function does not return a value (it only ever returns None) |
| 1703 |
x = yield from ("ERROR",) # E: Incompatible types in "yield from" (actual type "str", expected type "int") \ |
| 1704 |
# E: Function does not return a value (it only ever returns None) |
| 1705 |
[builtins fixtures/tuple.pyi] |
| 1706 |
|
| 1707 |
-- dict(...) |
| 1708 |
-- --------- |
| 1709 |
|
| 1710 |
|
| 1711 |
-- Note that the stub used in unit tests does not have all overload |
| 1712 |
-- variants, but it should not matter. |
| 1713 |
|
| 1714 |
[case testDictWithKeywordArgsOnly] |
| 1715 |
from typing import Dict, Any |
| 1716 |
d1 = dict(a=1, b=2) # type: Dict[str, int] |
| 1717 |
d2 = dict(a=1, b='') # type: Dict[str, int] # E: Dict entry 1 has incompatible type "str": "str"; expected "str": "int" |
| 1718 |
d3 = dict(a=1) # type: Dict[int, int] # E: Dict entry 0 has incompatible type "str": "int"; expected "int": "int" |
| 1719 |
d4 = dict(a=1, b=1) |
| 1720 |
d4.xyz # E: "dict[str, int]" has no attribute "xyz" |
| 1721 |
d5 = dict(a=1, b='') # type: Dict[str, Any] |
| 1722 |
[builtins fixtures/dict.pyi] |
| 1723 |
|
| 1724 |
[case testDictWithoutKeywordArgs] |
| 1725 |
from typing import Dict |
| 1726 |
d = dict() # E: Need type annotation for "d" (hint: "d: dict[<type>, <type>] = ...") |
| 1727 |
d2 = dict() # type: Dict[int, str] |
| 1728 |
dict(undefined) # E: Name "undefined" is not defined |
| 1729 |
[builtins fixtures/dict.pyi] |
| 1730 |
|
| 1731 |
[case testDictFromList] |
| 1732 |
from typing import Dict |
| 1733 |
d = dict([(1, 'x'), (2, 'y')]) |
| 1734 |
d() # E: "dict[int, str]" not callable |
| 1735 |
d2 = dict([(1, 'x')]) # type: Dict[str, str] # E: List item 0 has incompatible type "tuple[int, str]"; expected "tuple[str, str]" |
| 1736 |
[builtins fixtures/dict.pyi] |
| 1737 |
|
| 1738 |
[case testDictFromIterableAndKeywordArg] |
| 1739 |
from typing import Dict |
| 1740 |
it = [('x', 1)] |
| 1741 |
|
| 1742 |
d = dict(it, x=1) |
| 1743 |
d() # E: "dict[str, int]" not callable |
| 1744 |
|
| 1745 |
d2 = dict(it, x='') |
| 1746 |
d2() # E: "dict[str, object]" not callable |
| 1747 |
|
| 1748 |
d3 = dict(it, x='') # type: Dict[str, int] # E: Argument "x" to "dict" has incompatible type "str"; expected "int" |
| 1749 |
[builtins fixtures/dict.pyi] |
| 1750 |
|
| 1751 |
[case testDictFromIterableAndKeywordArg2] |
| 1752 |
it = [(1, 'x')] |
| 1753 |
dict(it, x='y') # E: Keyword argument only valid with "str" key type in call to "dict" |
| 1754 |
[builtins fixtures/dict.pyi] |
| 1755 |
|
| 1756 |
[case testDictFromIterableAndKeywordArg3] |
| 1757 |
d = dict([], x=1) |
| 1758 |
d() # E: "dict[str, int]" not callable |
| 1759 |
[builtins fixtures/dict.pyi] |
| 1760 |
|
| 1761 |
[case testDictFromIterableAndStarStarArgs] |
| 1762 |
from typing import Dict |
| 1763 |
it = [('x', 1)] |
| 1764 |
|
| 1765 |
kw = {'x': 1} |
| 1766 |
d = dict(it, **kw) |
| 1767 |
d() # E: "dict[str, int]" not callable |
| 1768 |
|
| 1769 |
kw2 = {'x': ''} |
| 1770 |
d2 = dict(it, **kw2) |
| 1771 |
d2() # E: "dict[str, object]" not callable |
| 1772 |
|
| 1773 |
d3 = dict(it, **kw2) # type: Dict[str, int] # E: Argument 2 to "dict" has incompatible type "**dict[str, str]"; expected "int" |
| 1774 |
[builtins fixtures/dict.pyi] |
| 1775 |
|
| 1776 |
[case testDictFromIterableAndStarStarArgs2] |
| 1777 |
it = [(1, 'x')] |
| 1778 |
kw = {'x': 'y'} |
| 1779 |
d = dict(it, **kw) # E: Keyword argument only valid with "str" key type in call to "dict" |
| 1780 |
d() # E: "dict[int, str]" not callable |
| 1781 |
[builtins fixtures/dict.pyi] |
| 1782 |
|
| 1783 |
[case testUserDefinedClassNamedDict] |
| 1784 |
from typing import Generic, TypeVar |
| 1785 |
T = TypeVar('T') |
| 1786 |
S = TypeVar('S') |
| 1787 |
class dict(Generic[T, S]): |
| 1788 |
def __init__(self, x: T, **kwargs: T) -> None: pass |
| 1789 |
dict(1, y=1) |
| 1790 |
[builtins fixtures/dict.pyi] |
| 1791 |
|
| 1792 |
[case testSpecialSignatureForSubclassOfDict] |
| 1793 |
from typing import TypeVar, Dict, Generic |
| 1794 |
T = TypeVar('T') |
| 1795 |
S = TypeVar('S') |
| 1796 |
class D1(dict): pass # Implicit base class Dict[Any, Any] |
| 1797 |
D1([(1, 2)], x=1) |
| 1798 |
class D2(Dict[T, S], Generic[T, S]): pass |
| 1799 |
da = D2([('x', 2)], x=1) |
| 1800 |
da() # E: "D2[str, int]" not callable |
| 1801 |
D2([(1, 2)], x=1) # E: Keyword argument only valid with "str" key type in call to "dict" |
| 1802 |
db = D2(x=1) |
| 1803 |
db() # E: "D2[str, int]" not callable |
| 1804 |
[builtins fixtures/dict.pyi] |
| 1805 |
|
| 1806 |
[case testSpecialSignatureForSubclassOfDict2] |
| 1807 |
from typing import TypeVar, Dict, Generic |
| 1808 |
T = TypeVar('T') |
| 1809 |
class D(Dict[str, T], Generic[T]): pass |
| 1810 |
D([('x', 1)], x=1) |
| 1811 |
[builtins fixtures/dict.pyi] |
| 1812 |
|
| 1813 |
[case testOverridingSpecialSignatureInSubclassOfDict] |
| 1814 |
from typing import TypeVar, Dict, Generic |
| 1815 |
T = TypeVar('T') |
| 1816 |
S = TypeVar('S') |
| 1817 |
class D(Dict[T, S], Generic[T, S]): |
| 1818 |
def __init__(self, x: S, y: T) -> None: pass |
| 1819 |
d = D(1, y='') |
| 1820 |
d() # E: "D[str, int]" not callable |
| 1821 |
[builtins fixtures/dict.pyi] |
| 1822 |
|
| 1823 |
[case testRevealType] |
| 1824 |
reveal_type(1) # N: Revealed type is "Literal[1]?" |
| 1825 |
|
| 1826 |
[case testRevealLocals] |
| 1827 |
x = 1 |
| 1828 |
y = 2 |
| 1829 |
z = x + y |
| 1830 |
reveal_locals() |
| 1831 |
[out] |
| 1832 |
main:4: note: Revealed local types are: |
| 1833 |
main:4: note: x: builtins.int |
| 1834 |
main:4: note: y: builtins.int |
| 1835 |
main:4: note: z: builtins.int |
| 1836 |
|
| 1837 |
[case testUndefinedRevealType] |
| 1838 |
reveal_type(x) |
| 1839 |
[out] |
| 1840 |
main:1: error: Name "x" is not defined |
| 1841 |
main:1: note: Revealed type is "Any" |
| 1842 |
|
| 1843 |
[case testUserDefinedRevealType] |
| 1844 |
def reveal_type(x: int) -> None: pass |
| 1845 |
reveal_type("foo") # E: Argument 1 to "reveal_type" has incompatible type "str"; expected "int" |
| 1846 |
|
| 1847 |
[case testTypingRevealType] |
| 1848 |
from typing import reveal_type |
| 1849 |
from typing import reveal_type as show_me_the_type |
| 1850 |
|
| 1851 |
reveal_type(1) # N: Revealed type is "Literal[1]?" |
| 1852 |
show_me_the_type(1) # N: Revealed type is "Literal[1]?" |
| 1853 |
|
| 1854 |
[case testTypingExtensionsRevealType] |
| 1855 |
from typing_extensions import reveal_type |
| 1856 |
from typing_extensions import reveal_type as show_me_the_type |
| 1857 |
|
| 1858 |
reveal_type(1) # N: Revealed type is "Literal[1]?" |
| 1859 |
show_me_the_type(1) # N: Revealed type is "Literal[1]?" |
| 1860 |
|
| 1861 |
[builtins fixtures/tuple.pyi] |
| 1862 |
|
| 1863 |
[case testRevealTypeVar] |
| 1864 |
reveal_type = 1 |
| 1865 |
1 + "foo" # E: Unsupported operand types for + ("int" and "str") |
| 1866 |
|
| 1867 |
[case testRevealForward] |
| 1868 |
def f() -> None: |
| 1869 |
reveal_type(x) |
| 1870 |
x = 1 + int() |
| 1871 |
[out] |
| 1872 |
main:2: note: Revealed type is "builtins.int" |
| 1873 |
|
| 1874 |
[case testRevealUncheckedFunction] |
| 1875 |
def f(): |
| 1876 |
x = 42 |
| 1877 |
reveal_type(x) |
| 1878 |
[out] |
| 1879 |
main:3: note: Revealed type is "Any" |
| 1880 |
main:3: note: 'reveal_type' always outputs 'Any' in unchecked functions |
| 1881 |
|
| 1882 |
[case testRevealCheckUntypedDefs] |
| 1883 |
# flags: --check-untyped-defs |
| 1884 |
def f(): |
| 1885 |
x = 42 |
| 1886 |
reveal_type(x) |
| 1887 |
[out] |
| 1888 |
main:4: note: Revealed type is "builtins.int" |
| 1889 |
|
| 1890 |
[case testRevealTypedDef] |
| 1891 |
def f() -> None: |
| 1892 |
x = 42 |
| 1893 |
reveal_type(x) |
| 1894 |
[out] |
| 1895 |
main:3: note: Revealed type is "builtins.int" |
| 1896 |
|
| 1897 |
[case testLambdaTypedContext] |
| 1898 |
def f() -> None: |
| 1899 |
lambda: 'a'.missing() # E: "str" has no attribute "missing" |
| 1900 |
|
| 1901 |
[case testLambdaUnypedContext] |
| 1902 |
def f(): |
| 1903 |
lambda: 'a'.missing() |
| 1904 |
|
| 1905 |
[case testLambdaCheckUnypedContext] |
| 1906 |
# flags: --check-untyped-defs |
| 1907 |
def f(): |
| 1908 |
lambda: 'a'.missing() # E: "str" has no attribute "missing" |
| 1909 |
|
| 1910 |
[case testEqNone] |
| 1911 |
None == None |
| 1912 |
[builtins fixtures/ops.pyi] |
| 1913 |
|
| 1914 |
[case testLtNone] |
| 1915 |
None < None # E: Unsupported left operand type for < ("None") |
| 1916 |
[builtins fixtures/ops.pyi] |
| 1917 |
|
| 1918 |
[case testDictWithStarExpr_no_native_parse] |
| 1919 |
|
| 1920 |
b = {'z': 26, *a} # E: Invalid syntax |
| 1921 |
[builtins fixtures/dict.pyi] |
| 1922 |
|
| 1923 |
[case testDictWithStarStarExpr] |
| 1924 |
|
| 1925 |
from typing import Dict, Iterable |
| 1926 |
|
| 1927 |
class Thing: |
| 1928 |
def keys(self) -> Iterable[str]: |
| 1929 |
... |
| 1930 |
def __getitem__(self, key: str) -> int: |
| 1931 |
... |
| 1932 |
|
| 1933 |
a = {'a': 1} |
| 1934 |
b = {'z': 26, **a} |
| 1935 |
c = {**b} |
| 1936 |
d = {**a, **b, 'c': 3} |
| 1937 |
e = {1: 'a', **a} # E: Cannot infer value of type parameter "KT" of <dict> \ |
| 1938 |
# N: Try assigning the literal to a variable annotated as dict[<key>, <val>] |
| 1939 |
f = {**b} # type: Dict[int, int] # E: Unpacked dict entry 0 has incompatible type "dict[str, int]"; expected "SupportsKeysAndGetItem[int, int]" |
| 1940 |
g = {**Thing()} |
| 1941 |
h = {**a, **Thing()} |
| 1942 |
i = {**Thing()} # type: Dict[int, int] # E: Unpacked dict entry 0 has incompatible type "Thing"; expected "SupportsKeysAndGetItem[int, int]" \ |
| 1943 |
# N: Following member(s) of "Thing" have conflicts: \ |
| 1944 |
# N: Expected: \ |
| 1945 |
# N: def __getitem__(self, int, /) -> int \ |
| 1946 |
# N: Got: \ |
| 1947 |
# N: def __getitem__(self, str, /) -> int \ |
| 1948 |
# N: Expected: \ |
| 1949 |
# N: def keys(self) -> Iterable[int] \ |
| 1950 |
# N: Got: \ |
| 1951 |
# N: def keys(self) -> Iterable[str] |
| 1952 |
j = {1: 'a', **Thing()} # E: Cannot infer value of type parameter "KT" of <dict> \ |
| 1953 |
# N: Try assigning the literal to a variable annotated as dict[<key>, <val>] |
| 1954 |
[builtins fixtures/dict.pyi] |
| 1955 |
[typing fixtures/typing-medium.pyi] |
| 1956 |
|
| 1957 |
[case testDictIncompatibleTypeErrorMessage] |
| 1958 |
from typing import Dict, Callable |
| 1959 |
|
| 1960 |
def things() -> int: |
| 1961 |
return 42 |
| 1962 |
|
| 1963 |
stuff: Dict[int, Callable[[], str]] = { |
| 1964 |
1: things # E: Dict entry 0 has incompatible type "int": "Callable[[], int]"; expected "int": "Callable[[], str]" |
| 1965 |
} |
| 1966 |
[builtins fixtures/dict.pyi] |
| 1967 |
|
| 1968 |
[case testDictIncompatibleKeyVerbosity] |
| 1969 |
from typing import Dict |
| 1970 |
import mod |
| 1971 |
|
| 1972 |
class A: ... |
| 1973 |
class B(A): ... |
| 1974 |
|
| 1975 |
d: Dict[A, B] = {A(): mod.B()} # E: Dict entry 0 has incompatible type "A": "mod.B"; expected "A": "__main__.B" |
| 1976 |
|
| 1977 |
[file mod.py] |
| 1978 |
class B: ... |
| 1979 |
|
| 1980 |
[builtins fixtures/dict.pyi] |
| 1981 |
|
| 1982 |
[case testDictIncompatibleValueVerbosity] |
| 1983 |
from typing import Dict |
| 1984 |
import mod |
| 1985 |
|
| 1986 |
class A: ... |
| 1987 |
class B(A): ... |
| 1988 |
|
| 1989 |
d: Dict[B, A] = {mod.B(): A()} # E: Dict entry 0 has incompatible type "mod.B": "A"; expected "__main__.B": "A" |
| 1990 |
|
| 1991 |
[file mod.py] |
| 1992 |
class B: ... |
| 1993 |
|
| 1994 |
[builtins fixtures/dict.pyi] |
| 1995 |
|
| 1996 |
[case testTypeAnnotationNeededMultipleAssignment] |
| 1997 |
x, y = [], [] # E: Need type annotation for "x" (hint: "x: list[<type>] = ...") \ |
| 1998 |
# E: Need type annotation for "y" (hint: "y: list[<type>] = ...") |
| 1999 |
[builtins fixtures/list.pyi] |
| 2000 |
|
| 2001 |
[case testStrictEqualityEq] |
| 2002 |
# flags: --strict-equality |
| 2003 |
class A: ... |
| 2004 |
class B: ... |
| 2005 |
class C(B): ... |
| 2006 |
|
| 2007 |
A() == B() # E: Non-overlapping equality check (left operand type: "A", right operand type: "B") |
| 2008 |
B() == C() |
| 2009 |
C() == B() |
| 2010 |
A() != B() # E: Non-overlapping equality check (left operand type: "A", right operand type: "B") |
| 2011 |
B() != C() |
| 2012 |
C() != B() |
| 2013 |
[builtins fixtures/bool.pyi] |
| 2014 |
|
| 2015 |
[case testStrictEqualityIs] |
| 2016 |
# flags: --strict-equality |
| 2017 |
class A: ... |
| 2018 |
class B: ... |
| 2019 |
class C(B): ... |
| 2020 |
|
| 2021 |
A() is B() # E: Non-overlapping identity check (left operand type: "A", right operand type: "B") |
| 2022 |
B() is C() |
| 2023 |
C() is B() |
| 2024 |
A() is not B() # E: Non-overlapping identity check (left operand type: "A", right operand type: "B") |
| 2025 |
B() is not C() |
| 2026 |
C() is not B() |
| 2027 |
[builtins fixtures/bool.pyi] |
| 2028 |
|
| 2029 |
[case testStrictEqualityContains] |
| 2030 |
# flags: --strict-equality |
| 2031 |
class A: ... |
| 2032 |
class B: ... |
| 2033 |
class C(B): ... |
| 2034 |
|
| 2035 |
A() in [B()] # E: Non-overlapping container check (element type: "A", container item type: "B") |
| 2036 |
B() in [C()] |
| 2037 |
C() in [B()] |
| 2038 |
A() not in [B()] # E: Non-overlapping container check (element type: "A", container item type: "B") |
| 2039 |
B() not in [C()] |
| 2040 |
C() not in [B()] |
| 2041 |
[builtins fixtures/list.pyi] |
| 2042 |
[typing fixtures/typing-full.pyi] |
| 2043 |
|
| 2044 |
[case testStrictEqualityUnions] |
| 2045 |
# flags: --strict-equality |
| 2046 |
from typing import Container, Union |
| 2047 |
|
| 2048 |
class A: ... |
| 2049 |
class B: ... |
| 2050 |
|
| 2051 |
a: Union[int, str] |
| 2052 |
b: Union[A, B] |
| 2053 |
|
| 2054 |
a == int() |
| 2055 |
b == int() # E: Non-overlapping equality check (left operand type: "A | B", right operand type: "int") |
| 2056 |
|
| 2057 |
a is int() |
| 2058 |
b is int() # E: Non-overlapping identity check (left operand type: "A | B", right operand type: "int") |
| 2059 |
|
| 2060 |
ca: Union[Container[int], Container[str]] |
| 2061 |
cb: Union[Container[A], Container[B]] |
| 2062 |
|
| 2063 |
42 in ca |
| 2064 |
42 in cb # E: Non-overlapping container check (element type: "int", container item type: "A | B") |
| 2065 |
[builtins fixtures/bool.pyi] |
| 2066 |
[typing fixtures/typing-full.pyi] |
| 2067 |
|
| 2068 |
[case testStrictEqualityBytesSpecial] |
| 2069 |
# flags: --strict-equality |
| 2070 |
b'abc' in b'abcde' |
| 2071 |
[builtins fixtures/primitives.pyi] |
| 2072 |
[typing fixtures/typing-medium.pyi] |
| 2073 |
|
| 2074 |
[case testStrictEqualityBytesSpecialUnion] |
| 2075 |
# flags: --strict-equality |
| 2076 |
from typing import Union |
| 2077 |
x: Union[bytes, str] |
| 2078 |
|
| 2079 |
b'abc' in x |
| 2080 |
x in b'abc' |
| 2081 |
[builtins fixtures/primitives.pyi] |
| 2082 |
[typing fixtures/typing-medium.pyi] |
| 2083 |
|
| 2084 |
[case testStrictEqualityByteArraySpecial] |
| 2085 |
# flags: --strict-equality |
| 2086 |
b'abc' in bytearray(b'abcde') |
| 2087 |
bytearray(b'abc') in b'abcde' # OK on Python 3 |
| 2088 |
[builtins fixtures/primitives.pyi] |
| 2089 |
[typing fixtures/typing-medium.pyi] |
| 2090 |
|
| 2091 |
[case testStrictEqualityNoPromotePy3] |
| 2092 |
# flags: --strict-equality |
| 2093 |
'a' == b'a' # E: Non-overlapping equality check (left operand type: "Literal['a']", right operand type: "Literal[b'a']") |
| 2094 |
b'a' in 'abc' # E: Non-overlapping container check (element type: "bytes", container item type: "str") |
| 2095 |
|
| 2096 |
x: str |
| 2097 |
y: bytes |
| 2098 |
x != y # E: Non-overlapping equality check (left operand type: "str", right operand type: "bytes") |
| 2099 |
[builtins fixtures/primitives.pyi] |
| 2100 |
[typing fixtures/typing-full.pyi] |
| 2101 |
|
| 2102 |
[case testStrictEqualityOkPromote] |
| 2103 |
# flags: --strict-equality |
| 2104 |
from typing import Container |
| 2105 |
c: Container[int] |
| 2106 |
|
| 2107 |
1 == 1.0 # OK |
| 2108 |
1.0 in c # OK |
| 2109 |
[builtins fixtures/primitives.pyi] |
| 2110 |
[typing fixtures/typing-full.pyi] |
| 2111 |
|
| 2112 |
[case testStrictEqualityAny] |
| 2113 |
# flags: --strict-equality |
| 2114 |
from typing import Any, Container |
| 2115 |
|
| 2116 |
x: Any |
| 2117 |
c: Container[str] |
| 2118 |
x in c |
| 2119 |
x == 42 |
| 2120 |
x is 42 |
| 2121 |
[builtins fixtures/bool.pyi] |
| 2122 |
[typing fixtures/typing-full.pyi] |
| 2123 |
|
| 2124 |
[case testStrictEqualityStrictOptional] |
| 2125 |
# flags: --strict-equality |
| 2126 |
|
| 2127 |
x: str |
| 2128 |
if x is not None: # OK even with strict-optional |
| 2129 |
pass |
| 2130 |
[builtins fixtures/bool.pyi] |
| 2131 |
|
| 2132 |
[case testStrictEqualityNoStrictOptional] |
| 2133 |
# flags: --strict-equality --no-strict-optional |
| 2134 |
|
| 2135 |
x: str |
| 2136 |
if x is not None: # OK without strict-optional |
| 2137 |
pass |
| 2138 |
[builtins fixtures/bool.pyi] |
| 2139 |
|
| 2140 |
[case testStrictEqualityEqNoOptionalOverlap] |
| 2141 |
# flags: --strict-equality |
| 2142 |
from typing import Optional |
| 2143 |
|
| 2144 |
x: Optional[str] |
| 2145 |
y: Optional[int] |
| 2146 |
if x == y: # E: Non-overlapping equality check (left operand type: "str | None", right operand type: "int | None") |
| 2147 |
... |
| 2148 |
[builtins fixtures/bool.pyi] |
| 2149 |
|
| 2150 |
[case testCustomEqCheckStrictEquality] |
| 2151 |
# flags: --strict-equality |
| 2152 |
class A: |
| 2153 |
def __eq__(self, other: A) -> bool: # type: ignore |
| 2154 |
... |
| 2155 |
class B: |
| 2156 |
def __eq__(self, other: B) -> bool: # type: ignore |
| 2157 |
... |
| 2158 |
|
| 2159 |
# Don't report non-overlapping check if there is already and error. |
| 2160 |
A() == B() # E: Unsupported operand types for == ("A" and "B") |
| 2161 |
[builtins fixtures/bool.pyi] |
| 2162 |
|
| 2163 |
[case testStrictEqualitySequenceAndCustomEq] |
| 2164 |
# flags: --strict-equality |
| 2165 |
from typing import Tuple |
| 2166 |
|
| 2167 |
class C: pass |
| 2168 |
class D: |
| 2169 |
def __eq__(self, other): return True |
| 2170 |
|
| 2171 |
a = [C()] |
| 2172 |
b = [D()] |
| 2173 |
a == b |
| 2174 |
b == a |
| 2175 |
t1: Tuple[C, ...] |
| 2176 |
t2: Tuple[D, ...] |
| 2177 |
t1 == t2 |
| 2178 |
t2 == t1 |
| 2179 |
[builtins fixtures/bool.pyi] |
| 2180 |
|
| 2181 |
[case testCustomEqCheckStrictEqualityOKInstance] |
| 2182 |
# flags: --strict-equality |
| 2183 |
class A: |
| 2184 |
def __eq__(self, other: object) -> bool: |
| 2185 |
... |
| 2186 |
class B: |
| 2187 |
def __eq__(self, other: object) -> bool: |
| 2188 |
... |
| 2189 |
|
| 2190 |
A() == int() # OK |
| 2191 |
int() != B() # OK |
| 2192 |
[builtins fixtures/bool.pyi] |
| 2193 |
|
| 2194 |
[case testCustomEqCheckStrictEqualityOKUnion] |
| 2195 |
# flags: --strict-equality |
| 2196 |
from typing import Union |
| 2197 |
class A: |
| 2198 |
def __eq__(self, other: object) -> bool: |
| 2199 |
... |
| 2200 |
|
| 2201 |
x: Union[A, str] |
| 2202 |
x == int() |
| 2203 |
[builtins fixtures/bool.pyi] |
| 2204 |
|
| 2205 |
[case testCustomEqCheckStrictEqualityTuple] |
| 2206 |
# flags: --strict-equality |
| 2207 |
from typing import NamedTuple |
| 2208 |
|
| 2209 |
class Base(NamedTuple): |
| 2210 |
attr: int |
| 2211 |
|
| 2212 |
class Custom(Base): |
| 2213 |
def __eq__(self, other: object) -> bool: ... |
| 2214 |
|
| 2215 |
Base(int()) == int() # E: Non-overlapping equality check (left operand type: "Base", right operand type: "int") |
| 2216 |
Base(int()) == tuple() |
| 2217 |
Custom(int()) == int() |
| 2218 |
[builtins fixtures/bool.pyi] |
| 2219 |
|
| 2220 |
[case testCustomEqCheckStrictEqualityMeta] |
| 2221 |
# flags: --strict-equality |
| 2222 |
class CustomMeta(type): |
| 2223 |
def __eq__(self, other: object) -> bool: ... |
| 2224 |
|
| 2225 |
class Normal: ... |
| 2226 |
class Custom(metaclass=CustomMeta): ... |
| 2227 |
|
| 2228 |
Normal == int() # E: Non-overlapping equality check (left operand type: "type[Normal]", right operand type: "int") |
| 2229 |
Normal == Normal |
| 2230 |
Custom == int() |
| 2231 |
|
| 2232 |
n: type[Normal] = Normal |
| 2233 |
c: type[Custom] = Custom |
| 2234 |
|
| 2235 |
n == int() # E: Non-overlapping equality check (left operand type: "type[Normal]", right operand type: "int") |
| 2236 |
n == n |
| 2237 |
c == int() |
| 2238 |
|
| 2239 |
[builtins fixtures/bool.pyi] |
| 2240 |
|
| 2241 |
[case testCustomContainsCheckStrictEquality] |
| 2242 |
# flags: --strict-equality |
| 2243 |
class A: |
| 2244 |
def __contains__(self, other: A) -> bool: |
| 2245 |
... |
| 2246 |
|
| 2247 |
# Don't report non-overlapping check if there is already and error. |
| 2248 |
42 in A() # E: Unsupported operand types for in ("int" and "A") |
| 2249 |
[builtins fixtures/bool.pyi] |
| 2250 |
|
| 2251 |
[case testStrictEqualityTypeVsCallable] |
| 2252 |
# flags: --strict-equality |
| 2253 |
from typing import Type, List |
| 2254 |
class C: ... |
| 2255 |
class D(C): ... |
| 2256 |
class Bad: ... |
| 2257 |
|
| 2258 |
subclasses: List[Type[C]] |
| 2259 |
object in subclasses |
| 2260 |
D in subclasses |
| 2261 |
Bad in subclasses # E: Non-overlapping container check (element type: "type[Bad]", container item type: "type[C]") |
| 2262 |
[builtins fixtures/list.pyi] |
| 2263 |
[typing fixtures/typing-full.pyi] |
| 2264 |
|
| 2265 |
[case testStrictEqualityMetaclass] |
| 2266 |
# flags: --strict-equality |
| 2267 |
from typing import List, Type, Any |
| 2268 |
|
| 2269 |
class Meta(type): ... |
| 2270 |
class OtherMeta(type): ... |
| 2271 |
|
| 2272 |
class A(metaclass=Meta): ... |
| 2273 |
class B(metaclass=Meta): ... |
| 2274 |
class C(metaclass=OtherMeta): ... |
| 2275 |
|
| 2276 |
o: Type[object] |
| 2277 |
a: Type[Any] |
| 2278 |
aa: type |
| 2279 |
exp: List[Meta] |
| 2280 |
|
| 2281 |
A in exp |
| 2282 |
B in exp |
| 2283 |
C in exp # E: Non-overlapping container check (element type: "type[C]", container item type: "Meta") |
| 2284 |
|
| 2285 |
o in exp |
| 2286 |
a in exp |
| 2287 |
aa in exp |
| 2288 |
|
| 2289 |
a in [A, B] |
| 2290 |
aa in [A, B] |
| 2291 |
|
| 2292 |
class AA: ... |
| 2293 |
class BB: ... |
| 2294 |
a in [AA, BB] |
| 2295 |
aa in [AA, BB] |
| 2296 |
[builtins fixtures/list.pyi] |
| 2297 |
[typing fixtures/typing-full.pyi] |
| 2298 |
|
| 2299 |
[case testEmptyListOverlap] |
| 2300 |
# mypy: strict-equality |
| 2301 |
from typing import List |
| 2302 |
|
| 2303 |
x: List[int] |
| 2304 |
x == [] |
| 2305 |
[builtins fixtures/isinstancelist.pyi] |
| 2306 |
|
| 2307 |
[case testCustomEqDecoratedStrictEquality] |
| 2308 |
# flags: --strict-equality |
| 2309 |
from typing import TypeVar, Callable, Any |
| 2310 |
|
| 2311 |
F = TypeVar('F', bound=Callable[..., Any]) |
| 2312 |
|
| 2313 |
def deco(f: F) -> F: ... |
| 2314 |
|
| 2315 |
class Custom: |
| 2316 |
@deco |
| 2317 |
def __eq__(self, other: object) -> bool: ... |
| 2318 |
|
| 2319 |
Custom() == int() |
| 2320 |
[builtins fixtures/bool.pyi] |
| 2321 |
|
| 2322 |
[case testCustomEqVarStrictEquality] |
| 2323 |
# flags: --strict-equality |
| 2324 |
|
| 2325 |
class Custom: |
| 2326 |
def compare(self, other: object) -> bool: ... |
| 2327 |
__eq__ = compare |
| 2328 |
|
| 2329 |
Custom() == int() |
| 2330 |
[builtins fixtures/bool.pyi] |
| 2331 |
|
| 2332 |
[case testStrictEqualityDisabledWithTypeVarRestrictions] |
| 2333 |
# flags: --strict-equality |
| 2334 |
from typing import TypeVar |
| 2335 |
|
| 2336 |
T = TypeVar('T', str, int) |
| 2337 |
|
| 2338 |
def f(x: T) -> T: |
| 2339 |
if x == int(): # OK |
| 2340 |
... |
| 2341 |
return x |
| 2342 |
[builtins fixtures/bool.pyi] |
| 2343 |
|
| 2344 |
[case testStrictEqualityWithALiteral] |
| 2345 |
# flags: --strict-equality |
| 2346 |
from typing import Final, Literal |
| 2347 |
|
| 2348 |
def returns_a_or_b() -> Literal['a', 'b']: |
| 2349 |
... |
| 2350 |
def returns_1_or_2() -> Literal[1, 2]: |
| 2351 |
... |
| 2352 |
THREE: Final = 3 |
| 2353 |
|
| 2354 |
if returns_a_or_b() == 'c': # E: Non-overlapping equality check (left operand type: "Literal['a', 'b']", right operand type: "Literal['c']") |
| 2355 |
... |
| 2356 |
if returns_1_or_2() is THREE: # E: Non-overlapping identity check (left operand type: "Literal[1, 2]", right operand type: "Literal[3]") |
| 2357 |
... |
| 2358 |
[builtins fixtures/bool.pyi] |
| 2359 |
|
| 2360 |
[case testStrictEqualityWithALiteralNewType] |
| 2361 |
# flags: --strict-equality |
| 2362 |
from typing import NewType |
| 2363 |
|
| 2364 |
UserId = NewType('UserId', int) |
| 2365 |
FileId = NewType('FileId', str) |
| 2366 |
|
| 2367 |
u: UserId |
| 2368 |
f: FileId |
| 2369 |
|
| 2370 |
if u == 0: # OK |
| 2371 |
... |
| 2372 |
if f == 0: # E: Non-overlapping equality check (left operand type: "FileId", right operand type: "Literal[0]") |
| 2373 |
... |
| 2374 |
[builtins fixtures/bool.pyi] |
| 2375 |
|
| 2376 |
[case testStrictEqualityWithFixedLengthTupleInCheck] |
| 2377 |
# flags: --strict-equality |
| 2378 |
if 1 in ('x', 'y'): # E: Non-overlapping container check (element type: "int", container item type: "str") |
| 2379 |
pass |
| 2380 |
[builtins fixtures/tuple.pyi] |
| 2381 |
[typing fixtures/typing-full.pyi] |
| 2382 |
|
| 2383 |
[case testStrictEqualityWithAnySentinel] |
| 2384 |
# flags: --strict-equality |
| 2385 |
from __future__ import annotations |
| 2386 |
from typing import Any, cast |
| 2387 |
|
| 2388 |
class A: ... |
| 2389 |
class B: ... |
| 2390 |
|
| 2391 |
sentinel: Any = object() |
| 2392 |
|
| 2393 |
def f1(a: A = sentinel, b: B = sentinel): |
| 2394 |
if a is sentinel and b is sentinel: |
| 2395 |
raise |
| 2396 |
|
| 2397 |
|
| 2398 |
def f2(a: A | None = sentinel, b: B | None = sentinel): |
| 2399 |
if a is sentinel and b is sentinel: |
| 2400 |
raise |
| 2401 |
|
| 2402 |
|
| 2403 |
sentinel_strict = object() |
| 2404 |
|
| 2405 |
def f3(a: A = sentinel_strict, b: B = sentinel_strict): # E: Incompatible default for parameter "a" (default has type "object", parameter has type "A") \ |
| 2406 |
# E: Incompatible default for parameter "b" (default has type "object", parameter has type "B") |
| 2407 |
if a is sentinel_strict and b is sentinel_strict: # E: Non-overlapping identity check (left operand type: "B", right operand type: "A") |
| 2408 |
raise |
| 2409 |
|
| 2410 |
|
| 2411 |
def f4(a: A | None = sentinel_strict, b: B | None = sentinel_strict): # E: Incompatible default for parameter "a" (default has type "object", parameter has type "A | None") \ |
| 2412 |
# E: Incompatible default for parameter "b" (default has type "object", parameter has type "B | None") |
| 2413 |
if a is sentinel_strict and b is sentinel_strict: # E: Non-overlapping identity check (left operand type: "B | None", right operand type: "A | None") |
| 2414 |
raise |
| 2415 |
|
| 2416 |
|
| 2417 |
sentinel_inferred = cast(Any, object()) |
| 2418 |
|
| 2419 |
def f5(a: A = sentinel_inferred, b: B = sentinel_inferred): |
| 2420 |
if a is sentinel_inferred and b is sentinel_inferred: # E: Non-overlapping identity check (left operand type: "B", right operand type: "A") |
| 2421 |
raise |
| 2422 |
|
| 2423 |
def f6(a: A | None = sentinel_inferred, b: B | None = sentinel_inferred): |
| 2424 |
if a is sentinel_inferred and b is sentinel_inferred: # E: Non-overlapping identity check (left operand type: "B | None", right operand type: "A | None") |
| 2425 |
raise |
| 2426 |
[builtins fixtures/bool.pyi] |
| 2427 |
|
| 2428 |
[case testOverlappingAnyTypeWithoutStrictOptional] |
| 2429 |
# flags: --no-strict-optional --strict-equality |
| 2430 |
from typing import Any, Optional |
| 2431 |
|
| 2432 |
x: Optional[Any] |
| 2433 |
|
| 2434 |
if x in (1, 2): |
| 2435 |
pass |
| 2436 |
[builtins fixtures/tuple.pyi] |
| 2437 |
[typing fixtures/typing-full.pyi] |
| 2438 |
|
| 2439 |
|
| 2440 |
[case testOverlappingClassCallables] |
| 2441 |
# flags: --strict-equality |
| 2442 |
from typing import Any, Callable, Type |
| 2443 |
|
| 2444 |
x: Type[int] |
| 2445 |
y: Callable[[], Any] |
| 2446 |
x == y |
| 2447 |
y == x |
| 2448 |
int == y |
| 2449 |
y == int |
| 2450 |
[builtins fixtures/bool.pyi] |
| 2451 |
|
| 2452 |
[case testStrictEqualityAndEnumWithCustomEq] |
| 2453 |
# flags: --strict-equality |
| 2454 |
from enum import Enum |
| 2455 |
|
| 2456 |
class E1(Enum): |
| 2457 |
X = 0 |
| 2458 |
Y = 1 |
| 2459 |
|
| 2460 |
class E2(Enum): |
| 2461 |
X = 0 |
| 2462 |
Y = 1 |
| 2463 |
|
| 2464 |
def __eq__(self, other: object) -> bool: |
| 2465 |
return bool() |
| 2466 |
|
| 2467 |
E1.X == E1.Y # E: Non-overlapping equality check (left operand type: "Literal[E1.X]", right operand type: "Literal[E1.Y]") |
| 2468 |
E2.X == E2.Y |
| 2469 |
[builtins fixtures/bool.pyi] |
| 2470 |
|
| 2471 |
[case testStrictEqualityWithBytesContains] |
| 2472 |
# flags: --strict-equality |
| 2473 |
data = b"xy" |
| 2474 |
b"x" in data |
| 2475 |
[builtins fixtures/primitives.pyi] |
| 2476 |
[typing fixtures/typing-full.pyi] |
| 2477 |
|
| 2478 |
[case testStrictEqualityWithDifferentMapTypes] |
| 2479 |
# flags: --strict-equality |
| 2480 |
from typing import Mapping |
| 2481 |
|
| 2482 |
class A(Mapping[int, str]): ... |
| 2483 |
class B(Mapping[int, str]): ... |
| 2484 |
|
| 2485 |
a: A |
| 2486 |
b: B |
| 2487 |
assert a == b |
| 2488 |
[builtins fixtures/dict.pyi] |
| 2489 |
[typing fixtures/typing-full.pyi] |
| 2490 |
|
| 2491 |
[case testStrictEqualityWithRecursiveMapTypes] |
| 2492 |
# flags: --strict-equality |
| 2493 |
from typing import Dict |
| 2494 |
|
| 2495 |
R = Dict[str, R] |
| 2496 |
|
| 2497 |
a: R |
| 2498 |
b: R |
| 2499 |
assert a == b |
| 2500 |
|
| 2501 |
R2 = Dict[int, R2] |
| 2502 |
c: R2 |
| 2503 |
assert a == c # E: Non-overlapping equality check (left operand type: "dict[str, R]", right operand type: "dict[int, R2]") |
| 2504 |
[builtins fixtures/dict.pyi] |
| 2505 |
[typing fixtures/typing-full.pyi] |
| 2506 |
|
| 2507 |
[case testStrictEqualityWithRecursiveListTypes] |
| 2508 |
# flags: --strict-equality |
| 2509 |
from typing import List, Union |
| 2510 |
|
| 2511 |
R = List[Union[str, R]] |
| 2512 |
|
| 2513 |
a: R |
| 2514 |
b: R |
| 2515 |
assert a == b |
| 2516 |
|
| 2517 |
R2 = List[Union[int, R2]] |
| 2518 |
c: R2 |
| 2519 |
assert a == c |
| 2520 |
[builtins fixtures/list.pyi] |
| 2521 |
[typing fixtures/typing-full.pyi] |
| 2522 |
|
| 2523 |
[case testStrictEqualityForNone] |
| 2524 |
# flags: --strict-equality --strict-equality-for-none |
| 2525 |
|
| 2526 |
class A: ... |
| 2527 |
|
| 2528 |
def a1(x: A) -> None: |
| 2529 |
assert x is None # E: Non-overlapping identity check (left operand type: "A", right operand type: "None") |
| 2530 |
def a2(x: A) -> None: |
| 2531 |
x is not None # E: Non-overlapping identity check (left operand type: "A", right operand type: "None") |
| 2532 |
def a3(x: A) -> None: |
| 2533 |
None == x # E: Non-overlapping equality check (left operand type: "None", right operand type: "A") |
| 2534 |
def a4(x: list[A]) -> None: |
| 2535 |
None in x # E: Non-overlapping container check (element type: "None", container item type: "A") |
| 2536 |
|
| 2537 |
class B: |
| 2538 |
def __eq__(self, x: object) -> bool: ... |
| 2539 |
|
| 2540 |
def b1(x: B) -> None: |
| 2541 |
assert x is None # E: Non-overlapping identity check (left operand type: "B", right operand type: "None") |
| 2542 |
def b2(x: B) -> None: |
| 2543 |
x is not None # E: Non-overlapping identity check (left operand type: "B", right operand type: "None") |
| 2544 |
def b3(x: B) -> None: |
| 2545 |
x == None |
| 2546 |
def b4(x: list[B]) -> None: |
| 2547 |
None in x |
| 2548 |
|
| 2549 |
[builtins fixtures/list.pyi] |
| 2550 |
[typing fixtures/typing-full.pyi] |
| 2551 |
|
| 2552 |
[case testUnimportedHintAny] |
| 2553 |
def f(x: Any) -> None: # E: Name "Any" is not defined \ |
| 2554 |
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Any") |
| 2555 |
pass |
| 2556 |
|
| 2557 |
|
| 2558 |
[case testUnimportedHintAnyLower] |
| 2559 |
def f(x: any) -> None: # E: Name "any" is not defined \ |
| 2560 |
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Any") |
| 2561 |
pass |
| 2562 |
|
| 2563 |
|
| 2564 |
[case testUnimportedHintOptional] |
| 2565 |
def f(x: Optional[str]) -> None: # E: Name "Optional" is not defined \ |
| 2566 |
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional") |
| 2567 |
pass |
| 2568 |
|
| 2569 |
[case testAssertionLazilyWithIsNone] |
| 2570 |
from typing import Optional, List |
| 2571 |
li: Optional[List] = [] |
| 2572 |
assert li is None, li[0] |
| 2573 |
[builtins fixtures/list.pyi] |
| 2574 |
|
| 2575 |
|
| 2576 |
[case testAssertionLazilyWithIsInstance] |
| 2577 |
from typing import Optional, List |
| 2578 |
li: Optional[List] = [] |
| 2579 |
assert not isinstance(li,list), li[0] |
| 2580 |
[builtins fixtures/isinstancelist.pyi] |
| 2581 |
|
| 2582 |
[case testAssertCurrentFrameIsNotUnreachable] |
| 2583 |
def f() -> int: # E: Missing return statement |
| 2584 |
x: int |
| 2585 |
assert isinstance(x, int), '...' |
| 2586 |
[builtins fixtures/isinstance.pyi] |
| 2587 |
|
| 2588 |
[case testTypeVarAsValue] |
| 2589 |
from typing import TypeVar |
| 2590 |
T = TypeVar("T") |
| 2591 |
x: int |
| 2592 |
x + T # E: Unsupported left operand type for + ("int") |
| 2593 |
T() # E: "TypeVar" not callable |
| 2594 |
[builtins fixtures/tuple.pyi] |
| 2595 |
[typing fixtures/typing-full.pyi] |
| 2596 |
|
| 2597 |
[case testStringDisallowedUnpacking] |
| 2598 |
d: dict[str, str] |
| 2599 |
|
| 2600 |
for a1, b1 in d: # E: Unpacking a string is disallowed |
| 2601 |
reveal_type(a1) # N: Revealed type is "builtins.str" |
| 2602 |
reveal_type(b1) # N: Revealed type is "builtins.str" |
| 2603 |
|
| 2604 |
s = "foo" |
| 2605 |
a2, b2 = s # E: Unpacking a string is disallowed |
| 2606 |
reveal_type(a2) # N: Revealed type is "builtins.str" |
| 2607 |
reveal_type(b2) # N: Revealed type is "builtins.str" |
| 2608 |
[builtins fixtures/primitives.pyi] |
| 2609 |
|
| 2610 |
[case testStringLiteralUnpacking] |
| 2611 |
from typing import Literal, Final |
| 2612 |
|
| 2613 |
def takes_literal(xy: Literal["xy"]): |
| 2614 |
x, y = xy |
| 2615 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2616 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 2617 |
|
| 2618 |
x, y, z = xy # E: Unpacking a string is disallowed |
| 2619 |
reveal_type(z) # N: Revealed type is "builtins.str" |
| 2620 |
|
| 2621 |
def last_known_value() -> None: |
| 2622 |
xy: Final = "xy" |
| 2623 |
x, y = xy |
| 2624 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 2625 |
reveal_type(y) # N: Revealed type is "builtins.str" |
| 2626 |
|
| 2627 |
x, y, z = xy # E: Unpacking a string is disallowed |
| 2628 |
reveal_type(z) # N: Revealed type is "builtins.str" |
| 2629 |
[builtins fixtures/primitives.pyi] |
| 2630 |
|
| 2631 |
[case testOverloadUnexpectedKeywordArgWithTypoSuggestion] |
| 2632 |
from typing import overload, Union |
| 2633 |
|
| 2634 |
@overload |
| 2635 |
def f(foobar: int) -> None: ... |
| 2636 |
|
| 2637 |
@overload |
| 2638 |
def f(foobar: str) -> None: ... |
| 2639 |
|
| 2640 |
def f(foobar: Union[int, str]) -> None: pass |
| 2641 |
|
| 2642 |
f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? |
| 2643 |
[builtins fixtures/list.pyi] |
| 2644 |
|
| 2645 |
[case testOverloadUnexpectedKeywordArgNoMatch] |
| 2646 |
from typing import overload, Union |
| 2647 |
|
| 2648 |
@overload |
| 2649 |
def f(foobar: int) -> None: ... |
| 2650 |
|
| 2651 |
@overload |
| 2652 |
def f(foobar: str) -> None: ... |
| 2653 |
|
| 2654 |
def f(foobar: Union[int, str]) -> None: pass |
| 2655 |
|
| 2656 |
f(random=[1,2,3]) # E: Unexpected keyword argument "random" for overloaded function "f" \ |
| 2657 |
# E: No overload variant of "f" matches argument type "list[int]" \ |
| 2658 |
# N: Possible overload variants: \ |
| 2659 |
# N: def f(foobar: int) -> None \ |
| 2660 |
# N: def f(foobar: str) -> None |
| 2661 |
[builtins fixtures/list.pyi] |
| 2662 |
|
| 2663 |
[case testOverloadMultipleUnexpectedKeywordArgs] |
| 2664 |
from typing import overload, Union |
| 2665 |
|
| 2666 |
@overload |
| 2667 |
def f(foobar: int) -> None: ... |
| 2668 |
|
| 2669 |
@overload |
| 2670 |
def f(foobar: str) -> None: ... |
| 2671 |
|
| 2672 |
def f(foobar: Union[int, str]) -> None: pass |
| 2673 |
|
| 2674 |
f(fobar=1, baz=2) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? \ |
| 2675 |
# E: Unexpected keyword argument "baz" for overloaded function "f" |
| 2676 |
[builtins fixtures/list.pyi] |
| 2677 |
|
| 2678 |
[case testOverloadManyUnexpectedKeywordArgs] |
| 2679 |
from typing import overload, Union |
| 2680 |
|
| 2681 |
@overload |
| 2682 |
def f(foobar: int) -> None: ... |
| 2683 |
|
| 2684 |
@overload |
| 2685 |
def f(foobar: str) -> None: ... |
| 2686 |
|
| 2687 |
def f(foobar: Union[int, str]) -> None: pass |
| 2688 |
|
| 2689 |
f(foobar=1, a=2, b=3, c=4, d=5, e=6) # E: Unexpected keyword arguments "a", "b", "c", "d", "e" for overloaded function "f" \ |
| 2690 |
# E: No overload variant of "f" matches argument types "int", "int", "int", "int", "int", "int" \ |
| 2691 |
# N: Possible overload variants: \ |
| 2692 |
# N: def f(foobar: int) -> None \ |
| 2693 |
# N: def f(foobar: str) -> None |
| 2694 |
[builtins fixtures/list.pyi] |
| 2695 |
|
| 2696 |
[case testOverloadUnexpectedKeywordArgsWithTypeMismatch] |
| 2697 |
from typing import overload, Union |
| 2698 |
|
| 2699 |
@overload |
| 2700 |
def f(foobar: int) -> None: ... |
| 2701 |
|
| 2702 |
@overload |
| 2703 |
def f(foobar: str) -> None: ... |
| 2704 |
|
| 2705 |
def f(foobar: Union[int, str]) -> None: pass |
| 2706 |
|
| 2707 |
f(fobar=1, other=[1,2,3]) # E: Unexpected keyword argument "fobar" for overloaded function "f"; did you mean "foobar"? \ |
| 2708 |
# E: Unexpected keyword argument "other" for overloaded function "f" \ |
| 2709 |
# E: No overload variant of "f" matches argument types "int", "list[int]" \ |
| 2710 |
# N: Possible overload variants: \ |
| 2711 |
# N: def f(foobar: int) -> None \ |
| 2712 |
# N: def f(foobar: str) -> None |
| 2713 |
[builtins fixtures/list.pyi] |
| 2714 |
|
| 2715 |
[case testOverloadPositionalArgTypeMismatch] |
| 2716 |
from typing import overload, Union |
| 2717 |
|
| 2718 |
@overload |
| 2719 |
def g(x: int, y: int) -> int: ... |
| 2720 |
|
| 2721 |
@overload |
| 2722 |
def g(x: str, y: str) -> str: ... |
| 2723 |
|
| 2724 |
def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: |
| 2725 |
return x |
| 2726 |
|
| 2727 |
g([1, 2], 3) # E: No overload variant of "g" matches argument types "list[int]", "int" \ |
| 2728 |
# N: Possible overload variants: \ |
| 2729 |
# N: def g(x: int, y: int) -> int \ |
| 2730 |
# N: def g(x: str, y: str) -> str |
| 2731 |
[builtins fixtures/list.pyi] |
| 2732 |
|
| 2733 |
[case testOverloadUnexpectedKeywordWithPositionalMismatch] |
| 2734 |
from typing import overload, Union |
| 2735 |
|
| 2736 |
@overload |
| 2737 |
def g(x: int, y: int) -> int: ... |
| 2738 |
|
| 2739 |
@overload |
| 2740 |
def g(x: str, y: str) -> str: ... |
| 2741 |
|
| 2742 |
def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: |
| 2743 |
return x |
| 2744 |
|
| 2745 |
g([1, 2], z=3) # E: Unexpected keyword argument "z" for overloaded function "g" \ |
| 2746 |
# E: No overload variant of "g" matches argument types "list[int]", "int" \ |
| 2747 |
# N: Possible overload variants: \ |
| 2748 |
# N: def g(x: int, y: int) -> int \ |
| 2749 |
# N: def g(x: str, y: str) -> str |
| 2750 |
[builtins fixtures/list.pyi] |
| 2751 |
|
| 2752 |
[case testOverloadNamedArgTypeMismatch] |
| 2753 |
from typing import overload, Union |
| 2754 |
|
| 2755 |
@overload |
| 2756 |
def g(x: int, y: int) -> int: ... |
| 2757 |
|
| 2758 |
@overload |
| 2759 |
def g(x: str, y: str) -> str: ... |
| 2760 |
|
| 2761 |
def g(x: Union[int, str], y: Union[int, str]) -> Union[int, str]: |
| 2762 |
return x |
| 2763 |
|
| 2764 |
g(x="hello", y=1) # E: No overload variant of "g" matches argument types "str", "int" \ |
| 2765 |
# N: Possible overload variants: \ |
| 2766 |
# N: def g(x: int, y: int) -> int \ |
| 2767 |
# N: def g(x: str, y: str) -> str |
| 2768 |
[builtins fixtures/list.pyi] |
| 2769 |
|
| 2770 |
[case testOverloadMixedArgsWithNonFirstKeywordTypo] |
| 2771 |
from typing import overload, Union |
| 2772 |
|
| 2773 |
@overload |
| 2774 |
def f(x: int, name: int, value: int) -> None: ... |
| 2775 |
|
| 2776 |
@overload |
| 2777 |
def f(x: str, name: str, value: str) -> None: ... |
| 2778 |
|
| 2779 |
def f(x: Union[int, str], name: Union[int, str], value: Union[int, str]) -> None: pass |
| 2780 |
|
| 2781 |
f(1, name=2, valeu=3) # E: Unexpected keyword argument "valeu" for overloaded function "f"; did you mean "value"? \ |
| 2782 |
# E: No overload variant of "f" matches argument types "int", "int", "int" \ |
| 2783 |
# N: Possible overload variants: \ |
| 2784 |
# N: def f(x: int, name: int, value: int) -> None \ |
| 2785 |
# N: def f(x: str, name: str, value: str) -> None |
| 2786 |
[builtins fixtures/list.pyi] |
| 2787 |
|
| 2788 |
[case testOverloadTwoMisspelledKeywordsDifferentTargets] |
| 2789 |
from typing import overload, Union |
| 2790 |
|
| 2791 |
@overload |
| 2792 |
def f(name: int, value: int) -> None: ... |
| 2793 |
|
| 2794 |
@overload |
| 2795 |
def f(name: str, value: str) -> None: ... |
| 2796 |
|
| 2797 |
def f(name: Union[int, str], value: Union[int, str]) -> None: pass |
| 2798 |
|
| 2799 |
f(nme=1, valeu=2) # E: Unexpected keyword argument "nme" for overloaded function "f"; did you mean "name"? \ |
| 2800 |
# E: Unexpected keyword argument "valeu" for overloaded function "f"; did you mean "value"? |
| 2801 |
[builtins fixtures/list.pyi] |
| 2802 |
|
| 2803 |
[case testOverloadMethodKeywordTypo] |
| 2804 |
from typing import overload, Union |
| 2805 |
|
| 2806 |
class A: |
| 2807 |
@overload |
| 2808 |
def f(self, foobar: int) -> None: ... |
| 2809 |
|
| 2810 |
@overload |
| 2811 |
def f(self, foobar: str) -> None: ... |
| 2812 |
|
| 2813 |
def f(self, foobar: Union[int, str]) -> None: pass |
| 2814 |
|
| 2815 |
A().f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" of "A"; did you mean "foobar"? |
| 2816 |
[builtins fixtures/list.pyi] |