| 1 |
|
| 2 |
|
| 3 |
-- Basic test cases |
| 4 |
-- ---------------- |
| 5 |
|
| 6 |
|
| 7 |
[case testBasicContextInference] |
| 8 |
from typing import TypeVar, Generic |
| 9 |
T = TypeVar('T') |
| 10 |
|
| 11 |
def f() -> 'A[T]': pass |
| 12 |
|
| 13 |
class A(Generic[T]): pass |
| 14 |
class B: pass |
| 15 |
|
| 16 |
ab: A[B] |
| 17 |
ao: A[object] |
| 18 |
b: B |
| 19 |
|
| 20 |
if int(): |
| 21 |
ao = f() |
| 22 |
if int(): |
| 23 |
ab = f() |
| 24 |
if int(): |
| 25 |
b = f() # E: Incompatible types in assignment (expression has type "A[Never]", variable has type "B") |
| 26 |
[case testBasicContextInferenceForConstructor] |
| 27 |
from typing import TypeVar, Generic |
| 28 |
T = TypeVar('T') |
| 29 |
class A(Generic[T]): pass |
| 30 |
class B: pass |
| 31 |
ab: A[B] |
| 32 |
ao: A[object] |
| 33 |
b: B |
| 34 |
|
| 35 |
if int(): |
| 36 |
ao = A() |
| 37 |
if int(): |
| 38 |
ab = A() |
| 39 |
if int(): |
| 40 |
b = A() # E: Incompatible types in assignment (expression has type "A[Never]", variable has type "B") |
| 41 |
[case testIncompatibleContextInference] |
| 42 |
from typing import TypeVar, Generic |
| 43 |
T = TypeVar('T') |
| 44 |
def f(a: T) -> 'A[T]': |
| 45 |
pass |
| 46 |
|
| 47 |
class A(Generic[T]): pass |
| 48 |
|
| 49 |
class B: pass |
| 50 |
class C: pass |
| 51 |
b: B |
| 52 |
c: C |
| 53 |
ab: A[B] |
| 54 |
ao: A[object] |
| 55 |
ac: A[C] |
| 56 |
|
| 57 |
if int(): |
| 58 |
ac = f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "C" |
| 59 |
if int(): |
| 60 |
ab = f(c) # E: Argument 1 to "f" has incompatible type "C"; expected "B" |
| 61 |
|
| 62 |
if int(): |
| 63 |
ao = f(b) |
| 64 |
if int(): |
| 65 |
ab = f(b) |
| 66 |
if int(): |
| 67 |
ao = f(c) |
| 68 |
if int(): |
| 69 |
ac = f(c) |
| 70 |
|
| 71 |
|
| 72 |
-- Local variables |
| 73 |
-- --------------- |
| 74 |
|
| 75 |
|
| 76 |
[case testInferGenericLocalVariableTypeWithEmptyContext] |
| 77 |
from typing import TypeVar, Generic |
| 78 |
T = TypeVar('T') |
| 79 |
def g() -> None: |
| 80 |
ao: A[object] |
| 81 |
ab: A[B] |
| 82 |
o: object |
| 83 |
b: B |
| 84 |
|
| 85 |
x = f(o) |
| 86 |
if int(): |
| 87 |
ab = x # E: Incompatible types in assignment (expression has type "A[object]", variable has type "A[B]") |
| 88 |
ao = x |
| 89 |
y = f(b) |
| 90 |
if int(): |
| 91 |
ao = y # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 92 |
ab = y |
| 93 |
|
| 94 |
def f(a: T) -> 'A[T]': pass |
| 95 |
|
| 96 |
class A(Generic[T]): pass |
| 97 |
class B: pass |
| 98 |
[out] |
| 99 |
|
| 100 |
[case testInferLocalVariableTypeWithUnderspecifiedGenericType] |
| 101 |
from typing import TypeVar, Generic |
| 102 |
T = TypeVar('T') |
| 103 |
def g() -> None: |
| 104 |
x = f() # E: Need type annotation for "x" |
| 105 |
|
| 106 |
def f() -> 'A[T]': pass |
| 107 |
class A(Generic[T]): pass |
| 108 |
[out] |
| 109 |
|
| 110 |
[case testInferMultipleLocalVariableTypesWithTupleRvalue] |
| 111 |
from typing import TypeVar, Generic |
| 112 |
T = TypeVar('T') |
| 113 |
def g() -> None: |
| 114 |
ao: A[object] |
| 115 |
ab: A[B] |
| 116 |
b: B |
| 117 |
x, y = f(b), f(b) |
| 118 |
if int(): |
| 119 |
ao = x # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 120 |
ao = y # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 121 |
ab = x |
| 122 |
ab = y |
| 123 |
|
| 124 |
def f(a: T) -> 'A[T]': pass |
| 125 |
class A(Generic[T]): pass |
| 126 |
class B: pass |
| 127 |
[out] |
| 128 |
|
| 129 |
[case testInferMultipleLocalVariableTypesWithArrayRvalueAndNesting] |
| 130 |
from typing import TypeVar, List, Generic |
| 131 |
T = TypeVar('T') |
| 132 |
def h() -> None: |
| 133 |
ao: A[object] |
| 134 |
ab: A[B] |
| 135 |
b: B |
| 136 |
x, y = g(f(b)) |
| 137 |
if int(): |
| 138 |
ao = x # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 139 |
ao = y # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 140 |
ab = x |
| 141 |
ab = y |
| 142 |
|
| 143 |
def f(a: T) -> 'A[T]': pass |
| 144 |
def g(a: T) -> List[T]: pass |
| 145 |
|
| 146 |
class A(Generic[T]): pass |
| 147 |
class B: pass |
| 148 |
[builtins fixtures/for.pyi] |
| 149 |
[out] |
| 150 |
|
| 151 |
|
| 152 |
-- Return types with multiple tvar instances |
| 153 |
-- ----------------------------------------- |
| 154 |
|
| 155 |
|
| 156 |
[case testInferenceWithTypeVariableTwiceInReturnType] |
| 157 |
from typing import TypeVar, Tuple, Generic |
| 158 |
T = TypeVar('T') |
| 159 |
|
| 160 |
def f(a: T) -> 'Tuple[A[T], A[T]]': pass |
| 161 |
|
| 162 |
class A(Generic[T]): pass |
| 163 |
class B: pass |
| 164 |
|
| 165 |
b: B |
| 166 |
o: object |
| 167 |
ab: A[B] |
| 168 |
ao: A[object] |
| 169 |
|
| 170 |
if int(): |
| 171 |
ab, ao = f(b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 172 |
if int(): |
| 173 |
ao, ab = f(b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 174 |
|
| 175 |
if int(): |
| 176 |
ao, ao = f(b) |
| 177 |
if int(): |
| 178 |
ab, ab = f(b) |
| 179 |
if int(): |
| 180 |
ao, ao = f(o) |
| 181 |
[builtins fixtures/tuple.pyi] |
| 182 |
|
| 183 |
[case testInferenceWithTypeVariableTwiceInReturnTypeAndMultipleVariables] |
| 184 |
from typing import TypeVar, Tuple, Generic |
| 185 |
S = TypeVar('S') |
| 186 |
T = TypeVar('T') |
| 187 |
|
| 188 |
def f(a: S, b: T) -> 'Tuple[A[S], A[T], A[T]]': pass |
| 189 |
def g(a: S, b: T) -> 'Tuple[A[S], A[S], A[T]]': pass |
| 190 |
def h(a: S, b: T) -> 'Tuple[A[S], A[S], A[T], A[T]]': pass |
| 191 |
|
| 192 |
class A(Generic[T]): pass |
| 193 |
class B: pass |
| 194 |
|
| 195 |
b: B |
| 196 |
o: object |
| 197 |
ab: A[B] |
| 198 |
ao: A[object] |
| 199 |
|
| 200 |
if int(): |
| 201 |
ao, ao, ab = f(b, b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 202 |
if int(): |
| 203 |
ao, ab, ao = g(b, b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 204 |
if int(): |
| 205 |
ao, ab, ab, ab = h(b, b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 206 |
if int(): |
| 207 |
ab, ab, ao, ab = h(b, b) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[object]") |
| 208 |
|
| 209 |
if int(): |
| 210 |
ao, ab, ab = f(b, b) |
| 211 |
if int(): |
| 212 |
ab, ab, ao = g(b, b) |
| 213 |
if int(): |
| 214 |
ab, ab, ab, ab = h(b, b) |
| 215 |
[builtins fixtures/tuple.pyi] |
| 216 |
|
| 217 |
|
| 218 |
-- Multiple tvar instances in arguments |
| 219 |
-- ------------------------------------ |
| 220 |
|
| 221 |
|
| 222 |
[case testMultipleTvatInstancesInArgs] |
| 223 |
from typing import TypeVar, Generic |
| 224 |
T = TypeVar('T') |
| 225 |
|
| 226 |
def f(a: T, b: T) -> 'A[T]': pass |
| 227 |
|
| 228 |
class A(Generic[T]): pass |
| 229 |
class B: pass |
| 230 |
class C(B): pass |
| 231 |
|
| 232 |
ac: A[C] |
| 233 |
ab: A[B] |
| 234 |
ao: A[object] |
| 235 |
b: B |
| 236 |
c: C |
| 237 |
o: object |
| 238 |
|
| 239 |
if int(): |
| 240 |
ab = f(b, o) # E: Argument 2 to "f" has incompatible type "object"; expected "B" |
| 241 |
if int(): |
| 242 |
ab = f(o, b) # E: Argument 1 to "f" has incompatible type "object"; expected "B" |
| 243 |
if int(): |
| 244 |
ac = f(b, c) # E: Argument 1 to "f" has incompatible type "B"; expected "C" |
| 245 |
if int(): |
| 246 |
ac = f(c, b) # E: Argument 2 to "f" has incompatible type "B"; expected "C" |
| 247 |
|
| 248 |
if int(): |
| 249 |
ao = f(b, c) |
| 250 |
if int(): |
| 251 |
ao = f(c, b) |
| 252 |
if int(): |
| 253 |
ab = f(c, b) |
| 254 |
|
| 255 |
|
| 256 |
-- Nested generic function calls |
| 257 |
-- ----------------------------- |
| 258 |
|
| 259 |
|
| 260 |
[case testNestedGenericFunctionCall1] |
| 261 |
from typing import TypeVar, Generic |
| 262 |
T = TypeVar('T') |
| 263 |
|
| 264 |
def f(a: T) -> 'A[T]': pass |
| 265 |
|
| 266 |
class A(Generic[T]): pass |
| 267 |
class B: pass |
| 268 |
|
| 269 |
aab: A[A[B]] |
| 270 |
aao: A[A[object]] |
| 271 |
ao: A[object] |
| 272 |
b: B |
| 273 |
o: object |
| 274 |
|
| 275 |
if int(): |
| 276 |
aab = f(f(o)) # E: Argument 1 to "f" has incompatible type "object"; expected "B" |
| 277 |
|
| 278 |
if int(): |
| 279 |
aab = f(f(b)) |
| 280 |
aao = f(f(b)) |
| 281 |
ao = f(f(b)) |
| 282 |
|
| 283 |
[case testNestedGenericFunctionCall2] |
| 284 |
from typing import TypeVar, Generic |
| 285 |
T = TypeVar('T') |
| 286 |
|
| 287 |
def f(a: T) -> T: pass |
| 288 |
def g(a: T) -> 'A[T]': pass |
| 289 |
|
| 290 |
class A(Generic[T]): pass |
| 291 |
class B: pass |
| 292 |
|
| 293 |
ab: A[B] |
| 294 |
ao: A[object] |
| 295 |
b: B |
| 296 |
o: object |
| 297 |
|
| 298 |
if int(): |
| 299 |
ab = f(g(o)) # E: Argument 1 to "g" has incompatible type "object"; expected "B" |
| 300 |
|
| 301 |
if int(): |
| 302 |
ab = f(g(b)) |
| 303 |
ao = f(g(b)) |
| 304 |
|
| 305 |
[case testNestedGenericFunctionCall3] |
| 306 |
from typing import TypeVar, Generic |
| 307 |
T = TypeVar('T') |
| 308 |
def f(a: T, b: T) -> T: |
| 309 |
pass |
| 310 |
|
| 311 |
def g(a: T) -> 'A[T]': pass |
| 312 |
|
| 313 |
class A(Generic[T]): pass |
| 314 |
class B: pass |
| 315 |
ab: A[B] |
| 316 |
ao: A[object] |
| 317 |
b: B |
| 318 |
o: object |
| 319 |
|
| 320 |
if int(): |
| 321 |
ab = f(g(o), g(b)) # E: Argument 1 to "g" has incompatible type "object"; expected "B" |
| 322 |
if int(): |
| 323 |
ab = f(g(b), g(o)) # E: Argument 1 to "g" has incompatible type "object"; expected "B" |
| 324 |
|
| 325 |
if int(): |
| 326 |
ab = f(g(b), g(b)) |
| 327 |
ao = f(g(b), g(o)) |
| 328 |
if int(): |
| 329 |
ao = f(g(o), g(b)) |
| 330 |
|
| 331 |
|
| 332 |
-- Method calls |
| 333 |
-- ------------ |
| 334 |
|
| 335 |
|
| 336 |
[case testMethodCallWithContextInference] |
| 337 |
from typing import TypeVar, Generic |
| 338 |
T = TypeVar('T') |
| 339 |
o: object |
| 340 |
b: B |
| 341 |
c: C |
| 342 |
def f(a: T) -> 'A[T]': pass |
| 343 |
|
| 344 |
class A(Generic[T]): |
| 345 |
def g(self, a: 'A[T]') -> 'A[T]': pass |
| 346 |
|
| 347 |
class B: pass |
| 348 |
class C(B): pass |
| 349 |
ao: A[object] |
| 350 |
ab: A[B] |
| 351 |
ac: A[C] |
| 352 |
|
| 353 |
ab.g(f(o)) # E: Argument 1 to "f" has incompatible type "object"; expected "B" |
| 354 |
if int(): |
| 355 |
ac = f(b).g(f(c)) # E: Incompatible types in assignment (expression has type "A[B]", variable has type "A[C]") |
| 356 |
if int(): |
| 357 |
ac = f(c).g(f(b)) # E: Argument 1 to "f" has incompatible type "B"; expected "C" |
| 358 |
|
| 359 |
if int(): |
| 360 |
ab = f(b).g(f(c)) |
| 361 |
ab.g(f(c)) |
| 362 |
|
| 363 |
|
| 364 |
-- List expressions |
| 365 |
-- ---------------- |
| 366 |
|
| 367 |
|
| 368 |
[case testEmptyListExpression] |
| 369 |
from typing import List |
| 370 |
aa: List[A] |
| 371 |
ao: List[object] |
| 372 |
a: A |
| 373 |
def f(): a, aa, ao # Prevent redefinition |
| 374 |
|
| 375 |
a = [] # E: Incompatible types in assignment (expression has type "list[Never]", variable has type "A") |
| 376 |
|
| 377 |
aa = [] |
| 378 |
ao = [] |
| 379 |
|
| 380 |
class A: pass |
| 381 |
[builtins fixtures/list.pyi] |
| 382 |
|
| 383 |
[case testSingleItemListExpressions] |
| 384 |
from typing import List, Optional |
| 385 |
aa: List[Optional[A]] |
| 386 |
ab: List[B] |
| 387 |
ao: List[object] |
| 388 |
a: A |
| 389 |
b: B |
| 390 |
def f(): aa, ab, ao # Prevent redefinition |
| 391 |
|
| 392 |
aa = [b] # E: List item 0 has incompatible type "B"; expected "A | None" |
| 393 |
ab = [a] # E: List item 0 has incompatible type "A"; expected "B" |
| 394 |
|
| 395 |
aa = [a] |
| 396 |
ab = [b] |
| 397 |
ao = [a] |
| 398 |
aa = [None] |
| 399 |
ao = [None] |
| 400 |
|
| 401 |
class A: pass |
| 402 |
class B: pass |
| 403 |
[builtins fixtures/list.pyi] |
| 404 |
|
| 405 |
[case testMultiItemListExpressions] |
| 406 |
from typing import List |
| 407 |
aa: List[A] |
| 408 |
ab: List[B] |
| 409 |
ao: List[object] |
| 410 |
a: A |
| 411 |
b: B |
| 412 |
def f(): ab, aa, ao # Prevent redefinition |
| 413 |
|
| 414 |
ab = [b, a] # E: List item 1 has incompatible type "A"; expected "B" |
| 415 |
ab = [a, b] # E: List item 0 has incompatible type "A"; expected "B" |
| 416 |
|
| 417 |
aa = [a, b, a] |
| 418 |
ao = [a, b] |
| 419 |
|
| 420 |
class A: pass |
| 421 |
class B(A): pass |
| 422 |
[builtins fixtures/list.pyi] |
| 423 |
|
| 424 |
[case testLocalVariableInferenceFromEmptyList] |
| 425 |
import typing |
| 426 |
def f() -> None: |
| 427 |
a = [] # E: Need type annotation for "a" (hint: "a: list[<type>] = ...") |
| 428 |
b = [None] |
| 429 |
c = [B()] |
| 430 |
if int(): |
| 431 |
c = [object()] # E: List item 0 has incompatible type "object"; expected "B" |
| 432 |
c = [B()] |
| 433 |
class B: pass |
| 434 |
[builtins fixtures/list.pyi] |
| 435 |
[out] |
| 436 |
|
| 437 |
[case testNestedListExpressions] |
| 438 |
# flags: --no-strict-optional |
| 439 |
from typing import List |
| 440 |
aao = None # type: list[list[object]] |
| 441 |
aab = None # type: list[list[B]] |
| 442 |
ab = None # type: list[B] |
| 443 |
b = None # type: B |
| 444 |
o = None # type: object |
| 445 |
def f(): aao, aab # Prevent redefinition |
| 446 |
|
| 447 |
aao = [[o], ab] # E: List item 1 has incompatible type "list[B]"; expected "list[object]" |
| 448 |
aab = [[], [o]] # E: List item 0 has incompatible type "object"; expected "B" |
| 449 |
|
| 450 |
aao = [[None], [b], [], [o]] |
| 451 |
aab = [[None], [b], []] |
| 452 |
aab = [ab, []] |
| 453 |
|
| 454 |
class B: pass |
| 455 |
[builtins fixtures/list.pyi] |
| 456 |
|
| 457 |
|
| 458 |
-- Complex context |
| 459 |
-- --------------- |
| 460 |
|
| 461 |
|
| 462 |
[case testParenthesesAndContext] |
| 463 |
from typing import List |
| 464 |
class A: pass |
| 465 |
l = ([A()]) # type: List[object] |
| 466 |
[builtins fixtures/list.pyi] |
| 467 |
|
| 468 |
[case testComplexTypeInferenceWithTuple] |
| 469 |
from typing import TypeVar, Tuple, Generic |
| 470 |
k = TypeVar('k') |
| 471 |
t = TypeVar('t') |
| 472 |
v = TypeVar('v') |
| 473 |
|
| 474 |
class A(Generic[t]): pass |
| 475 |
class B: pass |
| 476 |
class C: pass |
| 477 |
class D(Generic[k, v]): pass |
| 478 |
|
| 479 |
def f(x: Tuple[k]) -> 'A[k]': pass |
| 480 |
|
| 481 |
d = f((A(),)) # type: A[A[B]] |
| 482 |
[builtins fixtures/list.pyi] |
| 483 |
|
| 484 |
|
| 485 |
-- Dictionary literals |
| 486 |
-- ------------------- |
| 487 |
|
| 488 |
|
| 489 |
[case testDictionaryLiteralInContext] |
| 490 |
from typing import Dict, TypeVar, Generic |
| 491 |
t = TypeVar('t') |
| 492 |
class A(Generic[t]): pass |
| 493 |
class B: pass |
| 494 |
class C: pass |
| 495 |
a_b = A() # type: A[B] |
| 496 |
a_c = A() # type: A[C] |
| 497 |
d = {A() : a_c, |
| 498 |
a_b : A()} # type: Dict[A[B], A[C]] |
| 499 |
[builtins fixtures/dict.pyi] |
| 500 |
|
| 501 |
|
| 502 |
-- Special cases (regression tests etc.) |
| 503 |
-- ------------------------------------- |
| 504 |
|
| 505 |
|
| 506 |
[case testInitializationWithInferredGenericType] |
| 507 |
from typing import TypeVar, Generic |
| 508 |
T = TypeVar('T') |
| 509 |
|
| 510 |
def f(x: T) -> T: pass |
| 511 |
class C(Generic[T]): pass |
| 512 |
class A: pass |
| 513 |
|
| 514 |
c = f(A()) # type: C[A] # E: Argument 1 to "f" has incompatible type "A"; expected "C[A]" |
| 515 |
[case testInferredGenericTypeAsReturnValue] |
| 516 |
from typing import TypeVar, Generic |
| 517 |
T = TypeVar('T') |
| 518 |
def t() -> 'A[B]': |
| 519 |
return f(D()) # E: Argument 1 to "f" has incompatible type "D"; expected "B" |
| 520 |
return A() |
| 521 |
return f(C()) |
| 522 |
|
| 523 |
def f(a: T) -> 'A[T]': pass |
| 524 |
class A(Generic[T]): pass |
| 525 |
class B: pass |
| 526 |
class C(B): pass |
| 527 |
class D: pass |
| 528 |
[out] |
| 529 |
|
| 530 |
[case testIntersectionWithInferredGenericArgument] |
| 531 |
from foo import * |
| 532 |
[file foo.pyi] |
| 533 |
from typing import overload, TypeVar, Generic |
| 534 |
T = TypeVar('T') |
| 535 |
f(A()) |
| 536 |
|
| 537 |
@overload |
| 538 |
def f(x: 'A[B]') -> None: pass |
| 539 |
@overload |
| 540 |
def f(x: 'B') -> None: pass |
| 541 |
class A(Generic[T]): pass |
| 542 |
class B: pass |
| 543 |
|
| 544 |
[case testInferenceWithAbstractClassContext] |
| 545 |
from typing import TypeVar, Generic |
| 546 |
from abc import abstractmethod, ABCMeta |
| 547 |
t = TypeVar('t') |
| 548 |
|
| 549 |
class I(Generic[t]): |
| 550 |
@abstractmethod |
| 551 |
def f(self): pass |
| 552 |
class A(I[t], Generic[t]): |
| 553 |
def f(self): pass |
| 554 |
|
| 555 |
x = A() # type: I[int] |
| 556 |
a_object = A() # type: A[object] |
| 557 |
y = a_object # type: I[int] # E: Incompatible types in assignment (expression has type "A[object]", variable has type "I[int]") |
| 558 |
|
| 559 |
[case testInferenceWithAbstractClassContext2] |
| 560 |
from typing import TypeVar, Generic |
| 561 |
from abc import abstractmethod, ABCMeta |
| 562 |
t = TypeVar('t') |
| 563 |
class I(Generic[t]): pass |
| 564 |
class A(I[t], Generic[t]): pass |
| 565 |
def f(i: I[t]) -> A[t]: pass |
| 566 |
a = f(A()) # type: A[int] |
| 567 |
a_int = A() # type: A[int] |
| 568 |
aa = f(a_int) |
| 569 |
|
| 570 |
[case testInferenceWithAbstractClassContext3] |
| 571 |
from typing import TypeVar, Generic, Iterable |
| 572 |
t = TypeVar('t') |
| 573 |
class set(Generic[t]): |
| 574 |
def __init__(self, iterable: Iterable[t]) -> None: pass |
| 575 |
b = bool() |
| 576 |
l = set([b]) |
| 577 |
if int(): |
| 578 |
l = set([object()]) # E: List item 0 has incompatible type "object"; expected "bool" |
| 579 |
[builtins fixtures/for.pyi] |
| 580 |
|
| 581 |
|
| 582 |
-- Infer generic type in 'Any' context |
| 583 |
-- ----------------------------------- |
| 584 |
|
| 585 |
|
| 586 |
[case testInferGenericTypeInAnyContext] |
| 587 |
from typing import Any, TypeVar, Generic |
| 588 |
s = TypeVar('s') |
| 589 |
t = TypeVar('t') |
| 590 |
class C(Generic[s, t]): pass |
| 591 |
x = [] # type: Any |
| 592 |
y = C() # type: Any |
| 593 |
[builtins fixtures/list.pyi] |
| 594 |
|
| 595 |
|
| 596 |
-- Lambdas |
| 597 |
-- ------- |
| 598 |
|
| 599 |
|
| 600 |
[case testInferLambdaArgumentTypeUsingContext] |
| 601 |
from typing import Callable |
| 602 |
f: Callable[[B], A] |
| 603 |
if int(): |
| 604 |
f = lambda x: x.o |
| 605 |
f = lambda x: x.x # E: "B" has no attribute "x" |
| 606 |
class A: pass |
| 607 |
class B: |
| 608 |
o: A |
| 609 |
|
| 610 |
[case testInferLambdaReturnTypeUsingContext] |
| 611 |
from typing import List, Callable |
| 612 |
f: Callable[[], List[A]] |
| 613 |
if int(): |
| 614 |
f = lambda: [] |
| 615 |
f = lambda: [B()] # E: List item 0 has incompatible type "B"; expected "A" |
| 616 |
class A: pass |
| 617 |
class B: pass |
| 618 |
[builtins fixtures/list.pyi] |
| 619 |
|
| 620 |
[case testInferLambdaTypeUsingContext] |
| 621 |
x : str = (lambda x: x + 1)(1) # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 622 |
reveal_type((lambda x, y: x + y)(1, 2)) # N: Revealed type is "builtins.int" |
| 623 |
(lambda x, y: x + y)(1, "") # E: Unsupported operand types for + ("int" and "str") |
| 624 |
(lambda *, x, y: x + y)(x=1, y="") # E: Unsupported operand types for + ("int" and "str") |
| 625 |
reveal_type((lambda s, i: s)(i=0, s='x')) # N: Revealed type is "Literal['x']?" |
| 626 |
reveal_type((lambda s, i: i)(i=0, s='x')) # N: Revealed type is "Literal[0]?" |
| 627 |
reveal_type((lambda x, s, i: x)(1.0, i=0, s='x')) # N: Revealed type is "builtins.float" |
| 628 |
if object(): |
| 629 |
(lambda x, s, i: x)() # E: Too few arguments |
| 630 |
if object(): |
| 631 |
(lambda: 0)(1) # E: Too many arguments |
| 632 |
-- varargs are not handled, but it should not crash |
| 633 |
reveal_type((lambda *k, s, i: i)(type, i=0, s='x')) # N: Revealed type is "Any" |
| 634 |
reveal_type((lambda s, *k, i: i)(i=0, s='x')) # N: Revealed type is "Any" |
| 635 |
reveal_type((lambda s, i, **k: i)(i=0, s='x')) # N: Revealed type is "Any" |
| 636 |
[builtins fixtures/dict.pyi] |
| 637 |
|
| 638 |
[case testInferLambdaAsGenericFunctionArgument] |
| 639 |
from typing import TypeVar, List, Any, Callable |
| 640 |
t = TypeVar('t') |
| 641 |
class A: |
| 642 |
x: 'A' |
| 643 |
def f(a: List[t], fn: Callable[[t], Any]) -> None: pass |
| 644 |
list_a = [] # type: List[A] |
| 645 |
f(list_a, lambda a: a.x) |
| 646 |
[builtins fixtures/list.pyi] |
| 647 |
|
| 648 |
[case testLambdaWithoutContext] |
| 649 |
reveal_type(lambda x: x) # N: Revealed type is "def (x: Any) -> Any" |
| 650 |
reveal_type(lambda x: 1) # N: Revealed type is "def (x: Any) -> Literal[1]?" |
| 651 |
|
| 652 |
[case testLambdaContextVararg] |
| 653 |
from typing import Callable |
| 654 |
def f(t: Callable[[str], str]) -> str: '' |
| 655 |
f(lambda *_: '') |
| 656 |
[builtins fixtures/tuple.pyi] |
| 657 |
|
| 658 |
[case testInvalidContextForLambda] |
| 659 |
from typing import Callable |
| 660 |
f = lambda x: A() # type: Callable[[], A] |
| 661 |
f2 = lambda: A() # type: Callable[[A], A] |
| 662 |
class A: pass |
| 663 |
[out] |
| 664 |
main:2: error: Cannot infer type of lambda |
| 665 |
main:2: error: Incompatible types in assignment (expression has type "Callable[[Any], A]", variable has type "Callable[[], A]") |
| 666 |
main:3: error: Cannot infer type of lambda |
| 667 |
main:3: error: Incompatible types in assignment (expression has type "Callable[[], A]", variable has type "Callable[[A], A]") |
| 668 |
|
| 669 |
[case testEllipsisContextForLambda] |
| 670 |
from typing import Callable |
| 671 |
f1 = lambda x: 1 # type: Callable[..., int] |
| 672 |
f2 = lambda: 1 # type: Callable[..., int] |
| 673 |
f3 = lambda *args, **kwargs: 1 # type: Callable[..., int] |
| 674 |
f4 = lambda x: x # type: Callable[..., int] |
| 675 |
g = lambda x: 1 # type: Callable[..., str] |
| 676 |
[builtins fixtures/dict.pyi] |
| 677 |
[out] |
| 678 |
main:6: error: Incompatible types in assignment (expression has type "Callable[[Any], int]", variable has type "Callable[..., str]") |
| 679 |
main:6: error: Incompatible return value type (got "int", expected "str") |
| 680 |
|
| 681 |
[case testEllipsisContextForLambda2] |
| 682 |
from typing import TypeVar, Callable |
| 683 |
T = TypeVar('T') |
| 684 |
def foo(arg: Callable[..., T]) -> None: pass |
| 685 |
foo(lambda: 1) |
| 686 |
|
| 687 |
[case testLambdaNoneInContext] |
| 688 |
# flags: --no-strict-optional |
| 689 |
from typing import Callable |
| 690 |
def f(x: Callable[[], None]) -> None: pass |
| 691 |
def g(x: Callable[[], int]) -> None: pass |
| 692 |
f(lambda: None) |
| 693 |
g(lambda: None) |
| 694 |
|
| 695 |
[case testIsinstanceInInferredLambda] |
| 696 |
from typing import TypeVar, Callable, Optional |
| 697 |
T = TypeVar('T') |
| 698 |
S = TypeVar('S') |
| 699 |
class A: pass |
| 700 |
class B(A): pass |
| 701 |
class C(A): pass |
| 702 |
def f(func: Callable[[T], S], *z: T, r: Optional[S] = None) -> S: pass |
| 703 |
reveal_type(f(lambda x: 0 if isinstance(x, B) else 1)) # N: Revealed type is "Literal[0]? | Literal[1]?" |
| 704 |
f(lambda x: 0 if isinstance(x, B) else 1, A())() # E: "int" not callable |
| 705 |
f(lambda x: x if isinstance(x, B) else B(), A(), r=B())() # E: "B" not callable |
| 706 |
f( |
| 707 |
lambda x: # E: Argument 1 to "f" has incompatible type "Callable[[A], A]"; expected "Callable[[A], B]" |
| 708 |
B() if isinstance(x, B) else x, # E: Incompatible return value type (got "A", expected "B") |
| 709 |
A(), r=B()) |
| 710 |
[builtins fixtures/isinstance.pyi] |
| 711 |
|
| 712 |
[case testLambdaWithFastContainerType] |
| 713 |
from collections.abc import Callable |
| 714 |
from typing import Never, TypeVar |
| 715 |
|
| 716 |
T = TypeVar("T") |
| 717 |
|
| 718 |
def f(a: Callable[[], T]) -> None: ... |
| 719 |
|
| 720 |
def foo(x: str) -> Never: ... |
| 721 |
|
| 722 |
f(lambda: [foo(0)]) # E: Argument 1 to "foo" has incompatible type "int"; expected "str" |
| 723 |
f(lambda: {"x": foo(0)}) # E: Argument 1 to "foo" has incompatible type "int"; expected "str" |
| 724 |
[builtins fixtures/tuple.pyi] |
| 725 |
|
| 726 |
|
| 727 |
-- Overloads + generic functions |
| 728 |
-- ----------------------------- |
| 729 |
|
| 730 |
|
| 731 |
[case testMapWithOverloadedFunc] |
| 732 |
from foo import * |
| 733 |
[file foo.pyi] |
| 734 |
from typing import TypeVar, Callable, List, overload, Any |
| 735 |
t = TypeVar('t') |
| 736 |
s = TypeVar('s') |
| 737 |
def map(f: Callable[[t], s], seq: List[t]) -> List[s]: pass |
| 738 |
|
| 739 |
@overload |
| 740 |
def g(o: object) -> 'B': pass |
| 741 |
@overload |
| 742 |
def g(o: 'A', x: Any = None) -> 'B': pass |
| 743 |
|
| 744 |
class A: pass |
| 745 |
class B: pass |
| 746 |
|
| 747 |
m = map(g, [A()]) |
| 748 |
b = m # type: List[B] |
| 749 |
a = m # type: List[A] # E: Incompatible types in assignment (expression has type "list[B]", variable has type "list[A]") |
| 750 |
[builtins fixtures/list.pyi] |
| 751 |
|
| 752 |
|
| 753 |
-- Boolean operators |
| 754 |
-- ----------------- |
| 755 |
|
| 756 |
|
| 757 |
[case testOrOperationInferredFromContext] |
| 758 |
from typing import List |
| 759 |
class A: pass |
| 760 |
class B: pass |
| 761 |
class C(B): pass |
| 762 |
a: List[A] |
| 763 |
b: List[B] |
| 764 |
c: List[C] |
| 765 |
if int(): |
| 766 |
a = a or [] |
| 767 |
if int(): |
| 768 |
a = [] or a |
| 769 |
if int(): |
| 770 |
b = b or [C()] |
| 771 |
if int(): |
| 772 |
a = a or b # E: Incompatible types in assignment (expression has type "list[A] | list[B]", variable has type "list[A]") |
| 773 |
if int(): |
| 774 |
b = b or c # E: Incompatible types in assignment (expression has type "list[B] | list[C]", variable has type "list[B]") |
| 775 |
[builtins fixtures/list.pyi] |
| 776 |
|
| 777 |
|
| 778 |
-- Special cases |
| 779 |
-- ------------- |
| 780 |
|
| 781 |
|
| 782 |
[case testSomeTypeVarsInferredFromContext] |
| 783 |
from typing import List, TypeVar |
| 784 |
t = TypeVar('t') |
| 785 |
s = TypeVar('s') |
| 786 |
# Some type variables can be inferred using context, but not all of them. |
| 787 |
a: List[A] |
| 788 |
def f(a: s, b: t) -> List[s]: pass |
| 789 |
class A: pass |
| 790 |
class B: pass |
| 791 |
if int(): |
| 792 |
a = f(A(), B()) |
| 793 |
if int(): |
| 794 |
a = f(B(), B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 795 |
[builtins fixtures/list.pyi] |
| 796 |
|
| 797 |
[case testSomeTypeVarsInferredFromContext2] |
| 798 |
from typing import List, TypeVar |
| 799 |
s = TypeVar('s') |
| 800 |
t = TypeVar('t') |
| 801 |
def f(a: s, b: t) -> List[s]: pass |
| 802 |
class A: pass |
| 803 |
class B: pass |
| 804 |
# Like testSomeTypeVarsInferredFromContext, but tvars in different order. |
| 805 |
a: List[A] |
| 806 |
if int(): |
| 807 |
a = f(A(), B()) |
| 808 |
if int(): |
| 809 |
a = f(B(), B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 810 |
[builtins fixtures/list.pyi] |
| 811 |
|
| 812 |
[case testLambdaInListAndHigherOrderFunction] |
| 813 |
from typing import TypeVar, Callable, List |
| 814 |
t = TypeVar('t') |
| 815 |
s = TypeVar('s') |
| 816 |
def map(f: List[Callable[[t], s]], a: List[t]) -> List[s]: pass |
| 817 |
class A: pass |
| 818 |
map( |
| 819 |
[lambda x: x], []) |
| 820 |
[builtins fixtures/list.pyi] |
| 821 |
[out] |
| 822 |
|
| 823 |
[case testChainedAssignmentInferenceContexts] |
| 824 |
from typing import List |
| 825 |
i: List[int] |
| 826 |
s: List[str] |
| 827 |
if int(): |
| 828 |
i = i = [] |
| 829 |
if int(): |
| 830 |
i = s = [] # E: Incompatible types in assignment (expression has type "list[str]", variable has type "list[int]") |
| 831 |
[builtins fixtures/list.pyi] |
| 832 |
|
| 833 |
[case testContextForAttributeDeclaredInInit] |
| 834 |
from typing import List |
| 835 |
class A: |
| 836 |
def __init__(self): |
| 837 |
self.x = [] # type: List[int] # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs |
| 838 |
a = A() |
| 839 |
a.x = [] |
| 840 |
a.x = [1] |
| 841 |
a.x = [''] # E: List item 0 has incompatible type "str"; expected "int" |
| 842 |
[builtins fixtures/list.pyi] |
| 843 |
|
| 844 |
[case testListMultiplyInContext] |
| 845 |
from typing import List |
| 846 |
a: List[int] |
| 847 |
if int(): |
| 848 |
a = [None] * 3 # E: List item 0 has incompatible type "None"; expected "int" |
| 849 |
a = [''] * 3 # E: List item 0 has incompatible type "str"; expected "int" |
| 850 |
[builtins fixtures/list.pyi] |
| 851 |
|
| 852 |
[case testUnionTypeContext] |
| 853 |
from typing import Union, List, TypeVar |
| 854 |
T = TypeVar('T') |
| 855 |
def f(x: Union[List[T], str]) -> None: pass |
| 856 |
f([1]) |
| 857 |
f('') |
| 858 |
f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "list[Never] | str" |
| 859 |
[builtins fixtures/isinstancelist.pyi] |
| 860 |
|
| 861 |
[case testIgnoringInferenceContext] |
| 862 |
from typing import TypeVar, List |
| 863 |
T = TypeVar('T') |
| 864 |
def f(x: List[T]) -> T: pass |
| 865 |
def g(y: object) -> None: pass |
| 866 |
a = [1] |
| 867 |
g(f(a)) |
| 868 |
[builtins fixtures/list.pyi] |
| 869 |
|
| 870 |
[case testStar2Context] |
| 871 |
from typing import Any, Dict, Tuple, Iterable |
| 872 |
def f1(iterable: Iterable[Tuple[str, Any]] = ()) -> None: |
| 873 |
f2(**dict(iterable)) |
| 874 |
def f2(iterable: Iterable[Tuple[str, Any]], **kw: Any) -> None: |
| 875 |
pass |
| 876 |
[builtins fixtures/dict.pyi] |
| 877 |
[out] |
| 878 |
|
| 879 |
[case testInferenceInGenericFunction] |
| 880 |
from typing import TypeVar, List |
| 881 |
T = TypeVar('T') |
| 882 |
def f(a: T) -> None: |
| 883 |
l = [] # type: List[T] |
| 884 |
l.append(a) |
| 885 |
l.append(1) # E: Argument 1 to "append" of "list" has incompatible type "int"; expected "T" |
| 886 |
[builtins fixtures/list.pyi] |
| 887 |
[out] |
| 888 |
|
| 889 |
[case testInferenceInGenericClass] |
| 890 |
from typing import TypeVar, Generic, List |
| 891 |
S = TypeVar('S') |
| 892 |
T = TypeVar('T') |
| 893 |
class A(Generic[S]): |
| 894 |
def f(self, a: T, b: S) -> None: |
| 895 |
l = [] # type: List[T] |
| 896 |
l.append(a) |
| 897 |
l.append(b) # E: Argument 1 to "append" of "list" has incompatible type "S"; expected "T" |
| 898 |
[builtins fixtures/list.pyi] |
| 899 |
[out] |
| 900 |
|
| 901 |
[case testLambdaInGenericFunction] |
| 902 |
from typing import TypeVar, Callable |
| 903 |
T = TypeVar('T') |
| 904 |
S = TypeVar('S') |
| 905 |
def f(a: T, b: S) -> None: |
| 906 |
c = lambda x: x # type: Callable[[T], S] |
| 907 |
[out] |
| 908 |
main:5: error: Incompatible types in assignment (expression has type "Callable[[T], T]", variable has type "Callable[[T], S]") |
| 909 |
main:5: error: Incompatible return value type (got "T", expected "S") |
| 910 |
|
| 911 |
[case testLambdaInGenericClass] |
| 912 |
from typing import TypeVar, Callable, Generic |
| 913 |
T = TypeVar('T') |
| 914 |
S = TypeVar('S') |
| 915 |
class A(Generic[T]): |
| 916 |
def f(self, b: S) -> None: |
| 917 |
c = lambda x: x # type: Callable[[T], S] |
| 918 |
[out] |
| 919 |
main:6: error: Incompatible types in assignment (expression has type "Callable[[T], T]", variable has type "Callable[[T], S]") |
| 920 |
main:6: error: Incompatible return value type (got "T", expected "S") |
| 921 |
|
| 922 |
[case testRevealTypeContext] |
| 923 |
from typing import TypeVar, Callable, Generic |
| 924 |
T = TypeVar('T') |
| 925 |
class A(Generic[T]): |
| 926 |
pass |
| 927 |
reveal_type(A()) # N: Revealed type is "__main__.A[Never]" |
| 928 |
b = reveal_type(A()) # type: A[int] # N: Revealed type is "__main__.A[builtins.int]" |
| 929 |
|
| 930 |
[case testUnionWithGenericTypeItemContext] |
| 931 |
from typing import TypeVar, Union, List |
| 932 |
|
| 933 |
T = TypeVar('T') |
| 934 |
|
| 935 |
def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass |
| 936 |
reveal_type(f(1)) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" |
| 937 |
reveal_type(f([])) # N: Revealed type is "builtins.list[builtins.int]" |
| 938 |
reveal_type(f(None)) # N: Revealed type is "None | builtins.list[builtins.int]" |
| 939 |
[builtins fixtures/list.pyi] |
| 940 |
|
| 941 |
[case testUnionWithGenericTypeItemContextAndStrictOptional] |
| 942 |
from typing import TypeVar, Union, List |
| 943 |
|
| 944 |
T = TypeVar('T') |
| 945 |
|
| 946 |
def f(x: Union[T, List[int]]) -> Union[T, List[int]]: pass |
| 947 |
reveal_type(f(1)) # N: Revealed type is "builtins.int | builtins.list[builtins.int]" |
| 948 |
reveal_type(f([])) # N: Revealed type is "builtins.list[builtins.int]" |
| 949 |
reveal_type(f(None)) # N: Revealed type is "None | builtins.list[builtins.int]" |
| 950 |
[builtins fixtures/list.pyi] |
| 951 |
|
| 952 |
[case testUnionWithGenericTypeItemContextInMethod] |
| 953 |
from typing import TypeVar, Union, List, Generic |
| 954 |
|
| 955 |
T = TypeVar('T') |
| 956 |
S = TypeVar('S') |
| 957 |
|
| 958 |
class C(Generic[T]): |
| 959 |
def f(self, x: Union[T, S]) -> Union[T, S]: pass |
| 960 |
|
| 961 |
c = C[List[int]]() |
| 962 |
reveal_type(c.f('')) # N: Revealed type is "builtins.list[builtins.int] | builtins.str" |
| 963 |
reveal_type(c.f([1])) # N: Revealed type is "builtins.list[builtins.int]" |
| 964 |
reveal_type(c.f([])) # N: Revealed type is "builtins.list[builtins.int]" |
| 965 |
reveal_type(c.f(None)) # N: Revealed type is "builtins.list[builtins.int] | None" |
| 966 |
[builtins fixtures/list.pyi] |
| 967 |
|
| 968 |
[case testUnionWithTupleNeverBranch] |
| 969 |
# https://github.com/python/mypy/issues/20608 |
| 970 |
from __future__ import annotations |
| 971 |
from typing import Callable, Never, Generic, TypeVar |
| 972 |
|
| 973 |
T = TypeVar('T') |
| 974 |
class ValueType(Generic[T]): pass |
| 975 |
class Bundle(Generic[T]): pass |
| 976 |
|
| 977 |
F = TypeVar('F') |
| 978 |
|
| 979 |
def fail_value_type_inference(self_bundle: ValueType[F]) -> tuple[Never, Bundle[ValueType[F]]] | tuple[int, Bundle[ValueType[int]]]: |
| 980 |
raise |
| 981 |
|
| 982 |
x = fail_value_type_inference |
| 983 |
x = fail_value_type_inference |
| 984 |
[builtins fixtures/tuple.pyi] |
| 985 |
|
| 986 |
[case testGenericMethodCalledInGenericContext] |
| 987 |
from typing import TypeVar, Generic |
| 988 |
|
| 989 |
_KT = TypeVar('_KT') |
| 990 |
_VT = TypeVar('_VT') |
| 991 |
_T = TypeVar('_T') |
| 992 |
|
| 993 |
class M(Generic[_KT, _VT]): |
| 994 |
def get(self, k: _KT, default: _T) -> _T: ... |
| 995 |
|
| 996 |
def f(d: M[_KT, _VT], k: _KT) -> _VT: |
| 997 |
return d.get(k, None) # E: Incompatible return value type (got "None", expected "_VT") |
| 998 |
|
| 999 |
[case testGenericMethodCalledInGenericContext2] |
| 1000 |
from typing import TypeVar, Generic, Union |
| 1001 |
|
| 1002 |
_KT = TypeVar('_KT') |
| 1003 |
_VT = TypeVar('_VT') |
| 1004 |
_T = TypeVar('_T') |
| 1005 |
|
| 1006 |
class M(Generic[_KT, _VT]): |
| 1007 |
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... |
| 1008 |
|
| 1009 |
def f(d: M[_KT, _VT], k: _KT) -> Union[_VT, None]: |
| 1010 |
return d.get(k, None) |
| 1011 |
|
| 1012 |
[case testLambdaDeferredCrash] |
| 1013 |
from typing import Callable |
| 1014 |
|
| 1015 |
class C: |
| 1016 |
def f(self) -> None: |
| 1017 |
g: Callable[[], int] = lambda: 1 or self.x |
| 1018 |
self.x = int() |
| 1019 |
|
| 1020 |
[case testInferTypeVariableFromTwoGenericTypes1] |
| 1021 |
from typing import TypeVar, List, Sequence |
| 1022 |
|
| 1023 |
T = TypeVar('T') |
| 1024 |
|
| 1025 |
class C: ... |
| 1026 |
class D(C): ... |
| 1027 |
|
| 1028 |
def f(x: Sequence[T], y: Sequence[T]) -> List[T]: ... |
| 1029 |
|
| 1030 |
reveal_type(f([C()], [D()])) # N: Revealed type is "builtins.list[__main__.C]" |
| 1031 |
[builtins fixtures/list.pyi] |
| 1032 |
|
| 1033 |
[case testInferTypeVariableFromTwoGenericTypes2] |
| 1034 |
from typing import TypeVar, List |
| 1035 |
|
| 1036 |
T = TypeVar('T') |
| 1037 |
|
| 1038 |
class C: ... |
| 1039 |
class D(C): ... |
| 1040 |
|
| 1041 |
def f(x: List[T], y: List[T]) -> List[T]: ... |
| 1042 |
|
| 1043 |
f([C()], [D()]) # E: Cannot infer value of type parameter "T" of "f" |
| 1044 |
[builtins fixtures/list.pyi] |
| 1045 |
|
| 1046 |
[case testInferTypeVariableFromTwoGenericTypes3] |
| 1047 |
from typing import Generic, TypeVar |
| 1048 |
|
| 1049 |
T = TypeVar('T') |
| 1050 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 1051 |
|
| 1052 |
class A(Generic[T_contra]): pass |
| 1053 |
class B(A[T]): pass |
| 1054 |
|
| 1055 |
class C: ... |
| 1056 |
class D(C): ... |
| 1057 |
|
| 1058 |
def f(x: A[T], y: A[T]) -> B[T]: ... |
| 1059 |
|
| 1060 |
c: B[C] |
| 1061 |
d: B[D] |
| 1062 |
reveal_type(f(c, d)) # N: Revealed type is "__main__.B[__main__.D]" |
| 1063 |
|
| 1064 |
[case testInferTypeVariableFromTwoGenericTypes4] |
| 1065 |
from typing import Generic, TypeVar, Callable, List |
| 1066 |
|
| 1067 |
T = TypeVar('T') |
| 1068 |
T_contra = TypeVar('T_contra', contravariant=True) |
| 1069 |
|
| 1070 |
class A(Generic[T_contra]): pass |
| 1071 |
class B(A[T_contra]): pass |
| 1072 |
|
| 1073 |
class C: ... |
| 1074 |
class D(C): ... |
| 1075 |
|
| 1076 |
def f(x: Callable[[B[T]], None], |
| 1077 |
y: Callable[[B[T]], None]) -> List[T]: ... |
| 1078 |
|
| 1079 |
def gc(x: A[C]) -> None: pass # B[C] |
| 1080 |
def gd(x: A[D]) -> None: pass # B[C] |
| 1081 |
|
| 1082 |
reveal_type(f(gc, gd)) # N: Revealed type is "builtins.list[__main__.C]" |
| 1083 |
[builtins fixtures/list.pyi] |
| 1084 |
|
| 1085 |
[case testWideOuterContextSubClassBound] |
| 1086 |
from typing import TypeVar |
| 1087 |
|
| 1088 |
class A: ... |
| 1089 |
class B(A): ... |
| 1090 |
|
| 1091 |
T = TypeVar('T', bound=B) |
| 1092 |
def f(x: T) -> T: ... |
| 1093 |
def outer(x: A) -> None: ... |
| 1094 |
|
| 1095 |
outer(f(B())) |
| 1096 |
x: A = f(B()) |
| 1097 |
|
| 1098 |
[case testWideOuterContextSubClassBoundGenericReturn] |
| 1099 |
from typing import TypeVar, Iterable, List |
| 1100 |
|
| 1101 |
class A: ... |
| 1102 |
class B(A): ... |
| 1103 |
|
| 1104 |
T = TypeVar('T', bound=B) |
| 1105 |
def f(x: T) -> List[T]: ... |
| 1106 |
def outer(x: Iterable[A]) -> None: ... |
| 1107 |
|
| 1108 |
outer(f(B())) |
| 1109 |
x: Iterable[A] = f(B()) |
| 1110 |
[builtins fixtures/list.pyi] |
| 1111 |
|
| 1112 |
[case testWideOuterContextSubClassValues] |
| 1113 |
from typing import TypeVar |
| 1114 |
|
| 1115 |
class A: ... |
| 1116 |
class B(A): ... |
| 1117 |
|
| 1118 |
T = TypeVar('T', B, int) |
| 1119 |
def f(x: T) -> T: ... |
| 1120 |
def outer(x: A) -> None: ... |
| 1121 |
|
| 1122 |
outer(f(B())) |
| 1123 |
x: A = f(B()) |
| 1124 |
|
| 1125 |
[case testWideOuterContextSubClassValuesGenericReturn] |
| 1126 |
from typing import TypeVar, Iterable, List |
| 1127 |
|
| 1128 |
class A: ... |
| 1129 |
class B(A): ... |
| 1130 |
|
| 1131 |
T = TypeVar('T', B, int) |
| 1132 |
def f(x: T) -> List[T]: ... |
| 1133 |
def outer(x: Iterable[A]) -> None: ... |
| 1134 |
|
| 1135 |
outer(f(B())) |
| 1136 |
x: Iterable[A] = f(B()) |
| 1137 |
[builtins fixtures/list.pyi] |
| 1138 |
|
| 1139 |
[case testWideOuterContextSubclassBoundGeneric] |
| 1140 |
from typing import TypeVar, Generic |
| 1141 |
|
| 1142 |
S = TypeVar('S') |
| 1143 |
class A(Generic[S]): ... |
| 1144 |
class B(A[S]): ... |
| 1145 |
|
| 1146 |
T = TypeVar('T', bound=B[int]) |
| 1147 |
def f(x: T) -> T: ... |
| 1148 |
def outer(x: A[int]) -> None: ... |
| 1149 |
|
| 1150 |
y: B[int] |
| 1151 |
outer(f(y)) |
| 1152 |
x: A[int] = f(y) |
| 1153 |
|
| 1154 |
[case testWideOuterContextSubclassBoundGenericCovariant] |
| 1155 |
from typing import TypeVar, Generic |
| 1156 |
|
| 1157 |
S_co = TypeVar('S_co', covariant=True) |
| 1158 |
class A(Generic[S_co]): ... |
| 1159 |
class B(A[S_co]): ... |
| 1160 |
|
| 1161 |
T = TypeVar('T', bound=B[int]) |
| 1162 |
def f(x: T) -> T: ... |
| 1163 |
def outer(x: A[int]) -> None: ... |
| 1164 |
|
| 1165 |
y: B[int] |
| 1166 |
outer(f(y)) |
| 1167 |
x: A[int] = f(y) |
| 1168 |
|
| 1169 |
[case testWideOuterContextSubclassValuesGeneric] |
| 1170 |
from typing import TypeVar, Generic |
| 1171 |
|
| 1172 |
S = TypeVar('S') |
| 1173 |
class A(Generic[S]): ... |
| 1174 |
class B(A[S]): ... |
| 1175 |
|
| 1176 |
T = TypeVar('T', B[int], int) |
| 1177 |
def f(x: T) -> T: ... |
| 1178 |
def outer(x: A[int]) -> None: ... |
| 1179 |
|
| 1180 |
y: B[int] |
| 1181 |
outer(f(y)) |
| 1182 |
x: A[int] = f(y) |
| 1183 |
|
| 1184 |
[case testWideOuterContextSubclassValuesGenericCovariant] |
| 1185 |
from typing import TypeVar, Generic |
| 1186 |
|
| 1187 |
S_co = TypeVar('S_co', covariant=True) |
| 1188 |
class A(Generic[S_co]): ... |
| 1189 |
class B(A[S_co]): ... |
| 1190 |
|
| 1191 |
T = TypeVar('T', B[int], int) |
| 1192 |
def f(x: T) -> T: ... |
| 1193 |
def outer(x: A[int]) -> None: ... |
| 1194 |
|
| 1195 |
y: B[int] |
| 1196 |
outer(f(y)) |
| 1197 |
x: A[int] = f(y) |
| 1198 |
|
| 1199 |
[case testWideOuterContextUnionBound] |
| 1200 |
from typing import TypeVar, Union |
| 1201 |
|
| 1202 |
class A: ... |
| 1203 |
class B: ... |
| 1204 |
|
| 1205 |
T = TypeVar('T', bound=B) |
| 1206 |
def f(x: T) -> T: ... |
| 1207 |
def outer(x: Union[A, B]) -> None: ... |
| 1208 |
|
| 1209 |
outer(f(B())) |
| 1210 |
x: Union[A, B] = f(B()) |
| 1211 |
|
| 1212 |
[case testWideOuterContextUnionBoundGenericReturn] |
| 1213 |
from typing import TypeVar, Union, Iterable, List |
| 1214 |
|
| 1215 |
class A: ... |
| 1216 |
class B: ... |
| 1217 |
|
| 1218 |
T = TypeVar('T', bound=B) |
| 1219 |
def f(x: T) -> List[T]: ... |
| 1220 |
def outer(x: Iterable[Union[A, B]]) -> None: ... |
| 1221 |
|
| 1222 |
outer(f(B())) |
| 1223 |
x: Iterable[Union[A, B]] = f(B()) |
| 1224 |
[builtins fixtures/list.pyi] |
| 1225 |
|
| 1226 |
[case testWideOuterContextUnionValues] |
| 1227 |
from typing import TypeVar, Union |
| 1228 |
|
| 1229 |
class A: ... |
| 1230 |
class B: ... |
| 1231 |
|
| 1232 |
T = TypeVar('T', B, int) |
| 1233 |
def f(x: T) -> T: ... |
| 1234 |
def outer(x: Union[A, B]) -> None: ... |
| 1235 |
|
| 1236 |
outer(f(B())) |
| 1237 |
x: Union[A, B] = f(B()) |
| 1238 |
|
| 1239 |
[case testWideOuterContextUnionValuesGenericReturn] |
| 1240 |
from typing import TypeVar, Union, Iterable, List |
| 1241 |
|
| 1242 |
class A: ... |
| 1243 |
class B: ... |
| 1244 |
|
| 1245 |
T = TypeVar('T', B, int) |
| 1246 |
def f(x: T) -> List[T]: ... |
| 1247 |
def outer(x: Iterable[Union[A, B]]) -> None: ... |
| 1248 |
|
| 1249 |
outer(f(B())) |
| 1250 |
x: Iterable[Union[A, B]] = f(B()) |
| 1251 |
[builtins fixtures/list.pyi] |
| 1252 |
|
| 1253 |
[case testWideOuterContextOptional] |
| 1254 |
from typing import Optional, Type, TypeVar |
| 1255 |
|
| 1256 |
class Custom: |
| 1257 |
pass |
| 1258 |
|
| 1259 |
T = TypeVar('T', bound=Custom) |
| 1260 |
|
| 1261 |
def a(x: T) -> Optional[T]: ... |
| 1262 |
|
| 1263 |
def b(x: T) -> Optional[T]: |
| 1264 |
return a(x) |
| 1265 |
|
| 1266 |
[case testWideOuterContextOptionalGenericReturn] |
| 1267 |
from typing import Optional, Type, TypeVar, Iterable |
| 1268 |
|
| 1269 |
class Custom: |
| 1270 |
pass |
| 1271 |
|
| 1272 |
T = TypeVar('T', bound=Custom) |
| 1273 |
|
| 1274 |
def a(x: T) -> Iterable[Optional[T]]: ... |
| 1275 |
|
| 1276 |
def b(x: T) -> Iterable[Optional[T]]: |
| 1277 |
return a(x) |
| 1278 |
|
| 1279 |
[case testWideOuterContextOptionalMethod] |
| 1280 |
from typing import Optional, Type, TypeVar |
| 1281 |
|
| 1282 |
class A: pass |
| 1283 |
class B: pass |
| 1284 |
|
| 1285 |
T = TypeVar('T', A, B) |
| 1286 |
class C: |
| 1287 |
def meth_a(self) -> Optional[A]: |
| 1288 |
return self.meth(A) |
| 1289 |
|
| 1290 |
def meth(self, cls: Type[T]) -> Optional[T]: ... |
| 1291 |
|
| 1292 |
[case testWideOuterContextValuesOverlapping] |
| 1293 |
from typing import TypeVar, List |
| 1294 |
|
| 1295 |
class A: |
| 1296 |
pass |
| 1297 |
class B(A): |
| 1298 |
pass |
| 1299 |
class C: |
| 1300 |
pass |
| 1301 |
|
| 1302 |
T = TypeVar('T', A, B, C) |
| 1303 |
def foo(xs: List[T]) -> T: ... |
| 1304 |
|
| 1305 |
S = TypeVar('S', B, C) |
| 1306 |
def bar(xs: List[S]) -> S: |
| 1307 |
foo(xs) |
| 1308 |
return xs[0] |
| 1309 |
[builtins fixtures/list.pyi] |
| 1310 |
|
| 1311 |
[case testWideOuterContextOptionalTypeVarReturn] |
| 1312 |
from typing import Callable, Iterable, List, Optional, TypeVar |
| 1313 |
|
| 1314 |
class C: |
| 1315 |
x: str |
| 1316 |
|
| 1317 |
T = TypeVar('T') |
| 1318 |
def f(i: Iterable[T], c: Callable[[T], str]) -> Optional[T]: ... |
| 1319 |
|
| 1320 |
def g(l: List[C], x: str) -> Optional[C]: |
| 1321 |
def pred(c: C) -> str: |
| 1322 |
return c.x |
| 1323 |
return f(l, pred) |
| 1324 |
[builtins fixtures/list.pyi] |
| 1325 |
|
| 1326 |
[case testWideOuterContextOptionalTypeVarReturnLambda] |
| 1327 |
from typing import Callable, Iterable, List, Optional, TypeVar |
| 1328 |
|
| 1329 |
class C: |
| 1330 |
x: str |
| 1331 |
|
| 1332 |
T = TypeVar('T') |
| 1333 |
def f(i: Iterable[T], c: Callable[[T], str]) -> Optional[T]: ... |
| 1334 |
|
| 1335 |
def g(l: List[C], x: str) -> Optional[C]: |
| 1336 |
return f(l, lambda c: reveal_type(c).x) # N: Revealed type is "__main__.C" |
| 1337 |
[builtins fixtures/list.pyi] |
| 1338 |
|
| 1339 |
[case testPartialTypeContextWithTwoLambdas] |
| 1340 |
from typing import Any, Generic, TypeVar, Callable |
| 1341 |
|
| 1342 |
def int_to_any(x: int) -> Any: ... |
| 1343 |
def any_to_int(x: Any) -> int: ... |
| 1344 |
def any_to_str(x: Any) -> str: ... |
| 1345 |
|
| 1346 |
T = TypeVar("T") |
| 1347 |
class W(Generic[T]): |
| 1348 |
def __init__( |
| 1349 |
self, serialize: Callable[[T], Any], deserialize: Callable[[Any], T] |
| 1350 |
) -> None: |
| 1351 |
... |
| 1352 |
reveal_type(W(lambda x: int_to_any(x), lambda x: any_to_int(x))) # N: Revealed type is "__main__.W[builtins.int]" |
| 1353 |
W( |
| 1354 |
lambda x: int_to_any(x), # E: Argument 1 to "int_to_any" has incompatible type "str"; expected "int" |
| 1355 |
lambda x: any_to_str(x) |
| 1356 |
) |
| 1357 |
|
| 1358 |
[case testWideOuterContextEmpty] |
| 1359 |
from typing import List, TypeVar |
| 1360 |
|
| 1361 |
T = TypeVar('T', bound=int) |
| 1362 |
def f(x: List[T]) -> T: ... |
| 1363 |
|
| 1364 |
# mypy infers List[Never] here, and Never is a subtype of str |
| 1365 |
y: str = f([]) |
| 1366 |
[builtins fixtures/list.pyi] |
| 1367 |
|
| 1368 |
[case testWideOuterContextEmptyError] |
| 1369 |
from typing import List, TypeVar |
| 1370 |
|
| 1371 |
T = TypeVar('T', bound=int) |
| 1372 |
def f(x: List[T]) -> List[T]: ... |
| 1373 |
|
| 1374 |
y: List[str] = f([]) |
| 1375 |
[builtins fixtures/list.pyi] |
| 1376 |
|
| 1377 |
[case testWideOuterContextNoArgs] |
| 1378 |
from typing import TypeVar, Optional |
| 1379 |
|
| 1380 |
T = TypeVar('T', bound=int) |
| 1381 |
def f(x: Optional[T] = None) -> T: ... |
| 1382 |
|
| 1383 |
y: str = f() |
| 1384 |
|
| 1385 |
[case testWideOuterContextNoArgsError] |
| 1386 |
from typing import TypeVar, Optional, List |
| 1387 |
|
| 1388 |
T = TypeVar('T', bound=int) |
| 1389 |
def f(x: Optional[T] = None) -> List[T]: ... |
| 1390 |
|
| 1391 |
y: List[str] = f() |
| 1392 |
[builtins fixtures/list.pyi] |
| 1393 |
|
| 1394 |
[case testUseCovariantGenericOuterContext] |
| 1395 |
from typing import TypeVar, Callable, Tuple |
| 1396 |
|
| 1397 |
T = TypeVar('T') |
| 1398 |
|
| 1399 |
def f(x: Callable[..., T]) -> T: |
| 1400 |
return x() |
| 1401 |
|
| 1402 |
x: Tuple[str, ...] = f(tuple) |
| 1403 |
[builtins fixtures/tuple.pyi] |
| 1404 |
[out] |
| 1405 |
|
| 1406 |
[case testUseCovariantGenericOuterContextUserDefined] |
| 1407 |
from typing import TypeVar, Callable, Generic |
| 1408 |
|
| 1409 |
T_co = TypeVar('T_co', covariant=True) |
| 1410 |
T = TypeVar('T') |
| 1411 |
|
| 1412 |
class G(Generic[T_co]): ... |
| 1413 |
|
| 1414 |
def f(x: Callable[..., T]) -> T: |
| 1415 |
return x() |
| 1416 |
|
| 1417 |
x: G[str] = f(G) |
| 1418 |
[out] |
| 1419 |
|
| 1420 |
[case testConditionalExpressionWithEmptyListAndUnionWithAny] |
| 1421 |
from typing import Union, List, Any |
| 1422 |
|
| 1423 |
def f(x: Union[List[str], Any]) -> None: |
| 1424 |
a = x if x else [] |
| 1425 |
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str] | Any | builtins.list[builtins.str | Any]" |
| 1426 |
[builtins fixtures/list.pyi] |
| 1427 |
|
| 1428 |
[case testConditionalExpressionWithEmptyIteableAndUnionWithAny] |
| 1429 |
from typing import Union, Iterable, Any |
| 1430 |
|
| 1431 |
def f(x: Union[Iterable[str], Any]) -> None: |
| 1432 |
a = x if x else [] |
| 1433 |
reveal_type(a) # N: Revealed type is "typing.Iterable[builtins.str] | Any | builtins.list[builtins.str | Any]" |
| 1434 |
[builtins fixtures/list.pyi] |
| 1435 |
|
| 1436 |
[case testInferMultipleAnyUnionCovariant] |
| 1437 |
from typing import Any, Mapping, Sequence, Union |
| 1438 |
|
| 1439 |
def foo(x: Union[Mapping[Any, Any], Mapping[Any, Sequence[Any]]]) -> None: |
| 1440 |
... |
| 1441 |
foo({1: 2}) |
| 1442 |
[builtins fixtures/dict.pyi] |
| 1443 |
|
| 1444 |
[case testInferMultipleAnyUnionInvariant] |
| 1445 |
from typing import Any, Dict, Sequence, Union |
| 1446 |
|
| 1447 |
def foo(x: Union[Dict[Any, Any], Dict[Any, Sequence[Any]]]) -> None: |
| 1448 |
... |
| 1449 |
foo({1: 2}) |
| 1450 |
[builtins fixtures/dict.pyi] |
| 1451 |
|
| 1452 |
[case testInferMultipleAnyUnionDifferentVariance] |
| 1453 |
from typing import Any, Dict, Mapping, Sequence, Union |
| 1454 |
|
| 1455 |
def foo(x: Union[Dict[Any, Any], Mapping[Any, Sequence[Any]]]) -> None: |
| 1456 |
... |
| 1457 |
foo({1: 2}) |
| 1458 |
|
| 1459 |
def bar(x: Union[Mapping[Any, Any], Dict[Any, Sequence[Any]]]) -> None: |
| 1460 |
... |
| 1461 |
bar({1: 2}) |
| 1462 |
[builtins fixtures/dict.pyi] |
| 1463 |
|
| 1464 |
[case testOptionalTypeNarrowedByGenericCall] |
| 1465 |
from typing import Dict, Optional |
| 1466 |
|
| 1467 |
d: Dict[str, str] = {} |
| 1468 |
|
| 1469 |
def foo(arg: Optional[str] = None) -> None: |
| 1470 |
if arg is None: |
| 1471 |
arg = d.get("a", "b") |
| 1472 |
reveal_type(arg) # N: Revealed type is "builtins.str" |
| 1473 |
[builtins fixtures/dict.pyi] |
| 1474 |
|
| 1475 |
[case testOptionalTypeNarrowedByGenericCall2] |
| 1476 |
from typing import Dict, Optional |
| 1477 |
|
| 1478 |
d: Dict[str, str] = {} |
| 1479 |
x: Optional[str] |
| 1480 |
if x: |
| 1481 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1482 |
x = d.get(x, x) |
| 1483 |
reveal_type(x) # N: Revealed type is "builtins.str" |
| 1484 |
[builtins fixtures/dict.pyi] |
| 1485 |
|
| 1486 |
[case testOptionalTypeNarrowedByGenericCall3] |
| 1487 |
from typing import Generic, TypeVar, Union |
| 1488 |
|
| 1489 |
T = TypeVar("T") |
| 1490 |
def bar(arg: Union[str, T]) -> Union[str, T]: ... |
| 1491 |
|
| 1492 |
def foo(arg: Union[str, int]) -> None: |
| 1493 |
if isinstance(arg, int): |
| 1494 |
arg = bar("default") |
| 1495 |
reveal_type(arg) # N: Revealed type is "builtins.str" |
| 1496 |
[builtins fixtures/isinstance.pyi] |
| 1497 |
|
| 1498 |
[case testOptionalTypeNarrowedByGenericCall4] |
| 1499 |
from typing import Optional, List, Generic, TypeVar |
| 1500 |
|
| 1501 |
T = TypeVar("T", covariant=True) |
| 1502 |
class C(Generic[T]): ... |
| 1503 |
|
| 1504 |
x: Optional[C[int]] = None |
| 1505 |
y = x = C() |
| 1506 |
reveal_type(y) # N: Revealed type is "__main__.C[builtins.int]" |
| 1507 |
|
| 1508 |
[case testOptionalTypeNarrowedByGenericCall5] |
| 1509 |
from typing import Any, Tuple, Union |
| 1510 |
|
| 1511 |
i: Union[Tuple[Any, ...], int] |
| 1512 |
b: Any |
| 1513 |
i = i if isinstance(i, int) else b |
| 1514 |
reveal_type(i) # N: Revealed type is "Any | builtins.int" |
| 1515 |
[builtins fixtures/isinstance.pyi] |
| 1516 |
|
| 1517 |
[case testLambdaInferenceUsesNarrowedTypes] |
| 1518 |
from typing import Optional, Callable |
| 1519 |
|
| 1520 |
def f1(key: Callable[[], str]) -> None: ... |
| 1521 |
def f2(key: object) -> None: ... |
| 1522 |
|
| 1523 |
def g(b: Optional[str]) -> None: |
| 1524 |
if b: |
| 1525 |
f1(lambda: reveal_type(b)) # N: Revealed type is "builtins.str" |
| 1526 |
z: Callable[[], str] = lambda: reveal_type(b) # N: Revealed type is "builtins.str" |
| 1527 |
f2(lambda: reveal_type(b)) # N: Revealed type is "builtins.str" |
| 1528 |
lambda: reveal_type(b) # N: Revealed type is "builtins.str" |
| 1529 |
|
| 1530 |
[case testInferenceContextReturningTypeVarUnion] |
| 1531 |
from collections.abc import Callable, Iterable |
| 1532 |
from typing import TypeVar, Union |
| 1533 |
|
| 1534 |
_T1 = TypeVar("_T1") |
| 1535 |
_T2 = TypeVar("_T2") |
| 1536 |
|
| 1537 |
def mymin( |
| 1538 |
iterable: Iterable[_T1], /, *, key: Callable[[_T1], int], default: _T2 |
| 1539 |
) -> Union[_T1, _T2]: ... |
| 1540 |
|
| 1541 |
def check(paths: Iterable[str], key: Callable[[str], int]) -> Union[str, None]: |
| 1542 |
return mymin(paths, key=key, default=None) |
| 1543 |
[builtins fixtures/tuple.pyi] |
| 1544 |
|
| 1545 |
[case testBinaryOpInferenceContext] |
| 1546 |
from typing import Literal, TypeVar |
| 1547 |
|
| 1548 |
T = TypeVar("T") |
| 1549 |
|
| 1550 |
def identity(x: T) -> T: |
| 1551 |
return x |
| 1552 |
|
| 1553 |
def check1(use: bool, val: str) -> "str | Literal[True]": |
| 1554 |
return use or identity(val) |
| 1555 |
|
| 1556 |
def check2(use: bool, val: str) -> "str | bool": |
| 1557 |
return use or identity(val) |
| 1558 |
|
| 1559 |
def check3(use: bool, val: str) -> "str | Literal[False]": |
| 1560 |
return use and identity(val) |
| 1561 |
|
| 1562 |
def check4(use: bool, val: str) -> "str | bool": |
| 1563 |
return use and identity(val) |
| 1564 |
[builtins fixtures/tuple.pyi] |
| 1565 |
|
| 1566 |
[case testDictAnyOrLiteralInContext] |
| 1567 |
from typing import Union, Optional, Any |
| 1568 |
|
| 1569 |
def f(x: dict[str, Union[str, None, int]]) -> None: |
| 1570 |
pass |
| 1571 |
|
| 1572 |
def g(x: Optional[dict[str, Any]], s: Optional[str]) -> None: |
| 1573 |
f(x or {'x': s}) |
| 1574 |
[builtins fixtures/dict.pyi] |
| 1575 |
|
| 1576 |
[case testReturnFallbackInferenceTuple] |
| 1577 |
from typing import TypeVar, Union |
| 1578 |
|
| 1579 |
T = TypeVar("T") |
| 1580 |
def foo(x: list[T]) -> tuple[T, ...]: ... |
| 1581 |
|
| 1582 |
def bar(x: list[int]) -> tuple[Union[str, int], ...]: |
| 1583 |
return foo(x) |
| 1584 |
|
| 1585 |
def bar2(x: list[int]) -> tuple[Union[str, int], ...]: |
| 1586 |
y = foo(x) |
| 1587 |
return y |
| 1588 |
[builtins fixtures/tuple.pyi] |
| 1589 |
|
| 1590 |
[case testReturnFallbackInferenceUnion] |
| 1591 |
from typing import Generic, TypeVar, Union |
| 1592 |
|
| 1593 |
T = TypeVar("T") |
| 1594 |
|
| 1595 |
class Cls(Generic[T]): |
| 1596 |
pass |
| 1597 |
|
| 1598 |
def inner(c: Cls[T]) -> Union[T, int]: |
| 1599 |
return 1 |
| 1600 |
|
| 1601 |
def outer(c: Cls[T]) -> Union[T, int]: |
| 1602 |
return inner(c) |
| 1603 |
|
| 1604 |
[case testReturnFallbackInferenceAsync] |
| 1605 |
from typing import Generic, TypeVar, Optional |
| 1606 |
|
| 1607 |
T = TypeVar("T") |
| 1608 |
|
| 1609 |
class Cls(Generic[T]): |
| 1610 |
pass |
| 1611 |
|
| 1612 |
async def inner(c: Cls[T]) -> Optional[T]: |
| 1613 |
return None |
| 1614 |
|
| 1615 |
async def outer(c: Cls[T]) -> Optional[T]: |
| 1616 |
return await inner(c) |
| 1617 |
[builtins fixtures/async_await.pyi] |
| 1618 |
[typing fixtures/typing-async.pyi] |
| 1619 |
|
| 1620 |
[case testContextFallbackNarrowingTernaryRValue] |
| 1621 |
from typing import Iterable, Union |
| 1622 |
|
| 1623 |
def foo(args: Union[Iterable[Union[str, int]], str, int]) -> Iterable[str]: |
| 1624 |
if isinstance(args, (str, int)): |
| 1625 |
args = (args,) |
| 1626 |
args = ( |
| 1627 |
arg if isinstance(arg, str) else str() |
| 1628 |
for arg in args |
| 1629 |
) |
| 1630 |
return args |
| 1631 |
[builtins fixtures/isinstancelist.pyi] |