| 1 |
-- Test cases for the type checker related to varargs. |
| 2 |
|
| 3 |
|
| 4 |
-- Varargs within body |
| 5 |
-- ------------------- |
| 6 |
|
| 7 |
|
| 8 |
[case testVarArgsWithinFunction] |
| 9 |
from typing import Tuple |
| 10 |
def f( *b: 'B') -> None: |
| 11 |
ab: Tuple[B, ...] |
| 12 |
ac: Tuple[C, ...] |
| 13 |
if int(): |
| 14 |
b = ac # E: Incompatible types in assignment (expression has type "tuple[C, ...]", variable has type "tuple[B, ...]") |
| 15 |
ac = b # E: Incompatible types in assignment (expression has type "tuple[B, ...]", variable has type "tuple[C, ...]") |
| 16 |
b = ab |
| 17 |
ab = b |
| 18 |
|
| 19 |
class B: pass |
| 20 |
class C: pass |
| 21 |
[builtins fixtures/tuple.pyi] |
| 22 |
[out] |
| 23 |
|
| 24 |
|
| 25 |
[case testVarArgsAreTuple] |
| 26 |
from typing import Tuple, Sequence |
| 27 |
def want_tuple(types: Tuple[type, ...]): pass |
| 28 |
def want_sequence(types: Sequence[type]): pass |
| 29 |
def test(*t: type) -> None: |
| 30 |
want_tuple(t) |
| 31 |
want_sequence(t) |
| 32 |
[builtins fixtures/tuple.pyi] |
| 33 |
[out] |
| 34 |
|
| 35 |
|
| 36 |
-- Calling varargs function |
| 37 |
-- ------------------------ |
| 38 |
|
| 39 |
|
| 40 |
[case testCallingVarArgsFunction] |
| 41 |
def f( *a: 'A') -> None: pass |
| 42 |
|
| 43 |
def g() -> None: pass |
| 44 |
|
| 45 |
class A: pass |
| 46 |
class B(A): pass |
| 47 |
class C: pass |
| 48 |
|
| 49 |
a: A |
| 50 |
b: B |
| 51 |
c: C |
| 52 |
|
| 53 |
f(c) # E: Argument 1 to "f" has incompatible type "C"; expected "A" |
| 54 |
f(a, b, c) # E: Argument 3 to "f" has incompatible type "C"; expected "A" |
| 55 |
f(g()) # E: "g" does not return a value (it only ever returns None) \ |
| 56 |
# E: Argument 1 to "f" has incompatible type "None"; expected "A" |
| 57 |
f(a, g()) # E: "g" does not return a value (it only ever returns None) \ |
| 58 |
# E: Argument 2 to "f" has incompatible type "None"; expected "A" |
| 59 |
f() |
| 60 |
f(a) |
| 61 |
f(b) |
| 62 |
f(a, b, a, b) |
| 63 |
[builtins fixtures/list.pyi] |
| 64 |
|
| 65 |
[case testCallingVarArgsFunctionWithAlsoNormalArgs] |
| 66 |
def f(a: 'C', *b: 'A') -> None: pass |
| 67 |
|
| 68 |
class A: pass |
| 69 |
class B(A): pass |
| 70 |
class C: pass |
| 71 |
|
| 72 |
a: A |
| 73 |
b: B |
| 74 |
c: C |
| 75 |
|
| 76 |
f(a) # E: Argument 1 to "f" has incompatible type "A"; expected "C" |
| 77 |
f(c, c) # E: Argument 2 to "f" has incompatible type "C"; expected "A" |
| 78 |
f(c, a, b, c) # E: Argument 4 to "f" has incompatible type "C"; expected "A" |
| 79 |
f(c) |
| 80 |
f(c, a) |
| 81 |
f(c, b, b, a, b) |
| 82 |
[builtins fixtures/list.pyi] |
| 83 |
|
| 84 |
[case testCallingVarArgsFunctionWithDefaultArgs] |
| 85 |
# flags: --implicit-optional --no-strict-optional |
| 86 |
def f(a: 'C' = None, *b: 'A') -> None: |
| 87 |
pass |
| 88 |
|
| 89 |
class A: pass |
| 90 |
class B(A): pass |
| 91 |
class C: pass |
| 92 |
|
| 93 |
a: A |
| 94 |
b: B |
| 95 |
c: C |
| 96 |
|
| 97 |
f(a) # E: Argument 1 to "f" has incompatible type "A"; expected "C | None" |
| 98 |
f(c, c) # E: Argument 2 to "f" has incompatible type "C"; expected "A" |
| 99 |
f(c, a, b, c) # E: Argument 4 to "f" has incompatible type "C"; expected "A" |
| 100 |
f() |
| 101 |
f(c) |
| 102 |
f(c, a) |
| 103 |
f(c, b, b, a, b) |
| 104 |
[builtins fixtures/list.pyi] |
| 105 |
|
| 106 |
[case testCallVarargsFunctionWithIterable] |
| 107 |
from typing import Iterable |
| 108 |
it1: Iterable[int] |
| 109 |
it2: Iterable[str] |
| 110 |
def f(*x: int) -> None: pass |
| 111 |
f(*it1) |
| 112 |
f(*it2) # E: Argument 1 to "f" has incompatible type "*Iterable[str]"; expected "int" |
| 113 |
[builtins fixtures/for.pyi] |
| 114 |
|
| 115 |
[case testCallVarargsFunctionWithTwoTupleStarArgs] |
| 116 |
from typing import TypeVar, Tuple |
| 117 |
|
| 118 |
T1 = TypeVar('T1') |
| 119 |
T2 = TypeVar('T2') |
| 120 |
T3 = TypeVar('T3') |
| 121 |
T4 = TypeVar('T4') |
| 122 |
|
| 123 |
def f(a: T1, b: T2, c: T3, d: T4) -> Tuple[T1, T2, T3, T4]: ... |
| 124 |
x: Tuple[int, str] |
| 125 |
y: Tuple[float, bool] |
| 126 |
reveal_type(f(*x, *y)) # N: Revealed type is "tuple[builtins.int, builtins.str, builtins.float, builtins.bool]" |
| 127 |
[builtins fixtures/list.pyi] |
| 128 |
|
| 129 |
[case testCallVarargsFunctionWithIterableAndPositional] |
| 130 |
|
| 131 |
from typing import Iterable |
| 132 |
it1: Iterable[int] |
| 133 |
def f(*x: int) -> None: pass |
| 134 |
f(*it1, 1, 2) |
| 135 |
f(*it1, 1, *it1, 2) |
| 136 |
f(*it1, '') # E: Argument 2 to "f" has incompatible type "str"; expected "int" |
| 137 |
[builtins fixtures/for.pyi] |
| 138 |
|
| 139 |
[case testCallVarargsFunctionWithTupleAndPositional] |
| 140 |
|
| 141 |
def f(*x: int) -> None: pass |
| 142 |
it1 = (1, 2) |
| 143 |
it2 = ('',) |
| 144 |
f(*it1, 1, 2) |
| 145 |
f(*it1, 1, *it1, 2) |
| 146 |
f(*it1, 1, *it2, 2) # E: Argument 3 to "f" has incompatible type "*tuple[str]"; expected "int" |
| 147 |
f(*it1, '') # E: Argument 2 to "f" has incompatible type "str"; expected "int" |
| 148 |
[builtins fixtures/for.pyi] |
| 149 |
|
| 150 |
[case testCallVarArgsWithMatchingNamedArgument] |
| 151 |
def foo(*args: int) -> None: ... |
| 152 |
foo(args=1) # E: Unexpected keyword argument "args" for "foo" |
| 153 |
|
| 154 |
def bar(*args: int, **kwargs: str) -> None: ... |
| 155 |
bar(args=1) # E: Argument "args" to "bar" has incompatible type "int"; expected "str" |
| 156 |
[builtins fixtures/for.pyi] |
| 157 |
|
| 158 |
|
| 159 |
-- Calling varargs function + type inference |
| 160 |
-- ----------------------------------------- |
| 161 |
|
| 162 |
|
| 163 |
[case testTypeInferenceWithCalleeVarArgs] |
| 164 |
from typing import TypeVar |
| 165 |
T = TypeVar('T') |
| 166 |
|
| 167 |
def f( *a: T) -> T: |
| 168 |
pass |
| 169 |
|
| 170 |
class A: pass |
| 171 |
class B(A): pass |
| 172 |
class C: pass |
| 173 |
|
| 174 |
a: A |
| 175 |
b: B |
| 176 |
c: C |
| 177 |
o: object |
| 178 |
|
| 179 |
if int(): |
| 180 |
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A") |
| 181 |
if int(): |
| 182 |
b = f(b, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 183 |
if int(): |
| 184 |
b = f(a, b) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 185 |
|
| 186 |
if int(): |
| 187 |
o = f() |
| 188 |
if int(): |
| 189 |
a = f(a) |
| 190 |
if int(): |
| 191 |
a = f(b) |
| 192 |
if int(): |
| 193 |
a = f(a, b, a) |
| 194 |
if int(): |
| 195 |
o = f(a, b, o) |
| 196 |
if int(): |
| 197 |
c = f(c) |
| 198 |
[builtins fixtures/list.pyi] |
| 199 |
|
| 200 |
[case testTypeInferenceWithCalleeVarArgsAndDefaultArgs] |
| 201 |
# flags: --no-strict-optional |
| 202 |
from typing import TypeVar |
| 203 |
T = TypeVar('T') |
| 204 |
a = None # type: A |
| 205 |
o = None # type: object |
| 206 |
|
| 207 |
def f(a: T, b: T = None, *c: T) -> T: |
| 208 |
pass |
| 209 |
|
| 210 |
class A: pass |
| 211 |
|
| 212 |
if int(): |
| 213 |
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A") |
| 214 |
if int(): |
| 215 |
a = f(a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A") |
| 216 |
if int(): |
| 217 |
a = f(a, a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A") |
| 218 |
if int(): |
| 219 |
a = f(a, a, a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A") |
| 220 |
|
| 221 |
if int(): |
| 222 |
a = f(a) |
| 223 |
if int(): |
| 224 |
a = f(a, a) |
| 225 |
if int(): |
| 226 |
a = f(a, a, a) |
| 227 |
[builtins fixtures/list.pyi] |
| 228 |
|
| 229 |
|
| 230 |
-- Calling normal function with varargs |
| 231 |
-- ------------------------------------ |
| 232 |
|
| 233 |
|
| 234 |
[case testCallingWithListVarArgs] |
| 235 |
from typing import List, Any, cast |
| 236 |
|
| 237 |
def f(a: 'A', b: 'B') -> None: |
| 238 |
pass |
| 239 |
|
| 240 |
class A: pass |
| 241 |
class B: pass |
| 242 |
|
| 243 |
aa: List[A] |
| 244 |
ab: List[B] |
| 245 |
a: A |
| 246 |
b: B |
| 247 |
|
| 248 |
f(*aa) # E: Argument 1 to "f" has incompatible type "*list[A]"; expected "B" |
| 249 |
f(a, *ab) # Ok |
| 250 |
f(a, b) |
| 251 |
(cast(Any, f))(*aa) # IDEA: Move to check-dynamic? |
| 252 |
(cast(Any, f))(a, *ab) # IDEA: Move to check-dynamic? |
| 253 |
[builtins fixtures/list.pyi] |
| 254 |
[case testCallingWithTupleVarArgs] |
| 255 |
def f(a: 'A', b: 'B', c: 'C') -> None: pass |
| 256 |
|
| 257 |
class A: pass |
| 258 |
class B: pass |
| 259 |
class C: pass |
| 260 |
class CC(C): pass |
| 261 |
|
| 262 |
a: A |
| 263 |
b: B |
| 264 |
c: C |
| 265 |
cc: CC |
| 266 |
|
| 267 |
f(*(a, b, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, B, B]"; expected "C" |
| 268 |
f(*(b, b, c)) # E: Argument 1 to "f" has incompatible type "*tuple[B, B, C]"; expected "A" |
| 269 |
f(a, *(b, b)) # E: Argument 2 to "f" has incompatible type "*tuple[B, B]"; expected "C" |
| 270 |
f(b, *(b, c)) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 271 |
f(*(a, b)) # E: Missing positional arguments "b", "c" in call to "f" |
| 272 |
f(*(a, b, c, c)) # E: Too many arguments for "f" |
| 273 |
f(a, *(b, c, c)) # E: Too many arguments for "f" |
| 274 |
f(*(a, b, c)) |
| 275 |
f(a, *(b, c)) |
| 276 |
f(a, b, *(c,)) |
| 277 |
f(a, *(b, cc)) |
| 278 |
[builtins fixtures/tuple.pyi] |
| 279 |
|
| 280 |
[case testInvalidVarArg] |
| 281 |
from typing import Any |
| 282 |
|
| 283 |
def f(a: 'A') -> None: |
| 284 |
pass |
| 285 |
|
| 286 |
class A: pass |
| 287 |
|
| 288 |
a = A() |
| 289 |
|
| 290 |
f(*None) # E: Expected iterable as variadic argument |
| 291 |
f(*a) # E: Expected iterable as variadic argument |
| 292 |
f(*(a,)) |
| 293 |
|
| 294 |
f(*4) # E: Expected iterable as variadic argument |
| 295 |
f(a, *4) # E: Expected iterable as variadic argument |
| 296 |
|
| 297 |
def main(f: Any) -> None: |
| 298 |
f(*3) # E: Expected iterable as variadic argument |
| 299 |
|
| 300 |
[builtins fixtures/tuple.pyi] |
| 301 |
|
| 302 |
|
| 303 |
-- Calling varargs function with varargs |
| 304 |
-- ------------------------------------- |
| 305 |
|
| 306 |
|
| 307 |
[case testCallingVarArgsFunctionWithListVarArgs] |
| 308 |
from typing import List |
| 309 |
|
| 310 |
def f(a: 'A', *b: 'B') -> None: pass |
| 311 |
def g(a: 'A', *b: 'A') -> None: pass |
| 312 |
class A: pass |
| 313 |
class B: pass |
| 314 |
|
| 315 |
aa: List[A] |
| 316 |
ab: List[B] |
| 317 |
a: A |
| 318 |
b: B |
| 319 |
f(*aa) # E: Argument 1 to "f" has incompatible type "*list[A]"; expected "B" |
| 320 |
f(a, *aa) # E: Argument 2 to "f" has incompatible type "*list[A]"; expected "B" |
| 321 |
f(b, *ab) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 322 |
f(a, a, *ab) # E: Argument 2 to "f" has incompatible type "A"; expected "B" |
| 323 |
f(a, b, *aa) # E: Argument 3 to "f" has incompatible type "*list[A]"; expected "B" |
| 324 |
f(b, b, *ab) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 325 |
g(*ab) # E: Argument 1 to "g" has incompatible type "*list[B]"; expected "A" |
| 326 |
f(a, *ab) |
| 327 |
f(a, b, *ab) |
| 328 |
f(a, b, b, *ab) |
| 329 |
g(*aa) |
| 330 |
[builtins fixtures/list.pyi] |
| 331 |
[case testCallingVarArgsFunctionWithTupleVarArgs] |
| 332 |
def f(a: 'A', *b: 'B') -> None: |
| 333 |
pass |
| 334 |
|
| 335 |
class A: pass |
| 336 |
class B: pass |
| 337 |
class C: pass |
| 338 |
class CC(C): pass |
| 339 |
|
| 340 |
a: A |
| 341 |
b: B |
| 342 |
c: C |
| 343 |
cc: CC |
| 344 |
|
| 345 |
f(*(b, b, b)) # E: Argument 1 to "f" has incompatible type "*tuple[B, B, B]"; expected "A" |
| 346 |
f(*(a, a, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, A, B]"; expected "B" |
| 347 |
f(*(a, b, a)) # E: Argument 1 to "f" has incompatible type "*tuple[A, B, A]"; expected "B" |
| 348 |
f(a, *(a, b)) # E: Argument 2 to "f" has incompatible type "*tuple[A, B]"; expected "B" |
| 349 |
f(b, *(b, b)) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 350 |
f(b, b, *(b,)) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 351 |
f(a, a, *(b,)) # E: Argument 2 to "f" has incompatible type "A"; expected "B" |
| 352 |
f(a, b, *(a,)) # E: Argument 3 to "f" has incompatible type "*tuple[A]"; expected "B" |
| 353 |
f(*()) # E: Too few arguments for "f" |
| 354 |
f(*(a, b, b)) |
| 355 |
f(a, *(b, b)) |
| 356 |
f(a, b, *(b,)) |
| 357 |
[builtins fixtures/list.pyi] |
| 358 |
|
| 359 |
|
| 360 |
-- Varargs special cases |
| 361 |
-- --------------------- |
| 362 |
|
| 363 |
|
| 364 |
[case testDynamicVarArg] |
| 365 |
from typing import Any |
| 366 |
def f(a: 'A') -> None: pass |
| 367 |
def g(a: 'A', *b: 'A') -> None: pass |
| 368 |
class A: pass |
| 369 |
|
| 370 |
d: Any |
| 371 |
a: A |
| 372 |
f(a, a, *d) # E: Too many arguments for "f" |
| 373 |
f(a, *d) # Ok |
| 374 |
f(*d) # Ok |
| 375 |
|
| 376 |
g(*d) |
| 377 |
g(a, *d) |
| 378 |
g(a, a, *d) |
| 379 |
[builtins fixtures/list.pyi] |
| 380 |
|
| 381 |
[case testListVarArgsAndSubtyping] |
| 382 |
from typing import List |
| 383 |
def f( *a: 'A') -> None: |
| 384 |
pass |
| 385 |
|
| 386 |
def g( *a: 'B') -> None: |
| 387 |
pass |
| 388 |
|
| 389 |
class A: pass |
| 390 |
class B(A): pass |
| 391 |
|
| 392 |
aa: List[A] |
| 393 |
ab: List[B] |
| 394 |
|
| 395 |
g(*aa) # E: Argument 1 to "g" has incompatible type "*list[A]"; expected "B" |
| 396 |
f(*aa) |
| 397 |
f(*ab) |
| 398 |
g(*ab) |
| 399 |
[builtins fixtures/list.pyi] |
| 400 |
|
| 401 |
[case testCallerVarArgsAndDefaultArgs] |
| 402 |
# flags: --implicit-optional --no-strict-optional |
| 403 |
|
| 404 |
def f(a: 'A', b: 'B' = None, *c: 'B') -> None: |
| 405 |
pass |
| 406 |
|
| 407 |
class A: pass |
| 408 |
class B: pass |
| 409 |
|
| 410 |
a, b = None, None # type: (A, B) |
| 411 |
f(*()) # E: Too few arguments for "f" |
| 412 |
f(a, *[a]) # E: Argument 2 to "f" has incompatible type "*list[A]"; expected "B | None" \ |
| 413 |
# E: Argument 2 to "f" has incompatible type "*list[A]"; expected "B" |
| 414 |
f(a, b, *[a]) # E: Argument 3 to "f" has incompatible type "*list[A]"; expected "B" |
| 415 |
f(*(a, a, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, A, B]"; expected "B | None" |
| 416 |
f(*(a,)) |
| 417 |
f(*(a, b)) |
| 418 |
f(*(a, b, b, b)) |
| 419 |
f(a, *[]) |
| 420 |
f(a, *[b]) |
| 421 |
f(a, *[b, b]) |
| 422 |
[builtins fixtures/list.pyi] |
| 423 |
|
| 424 |
[case testVarArgsAfterKeywordArgInCall1] |
| 425 |
# see: mypy issue #2729 |
| 426 |
def f(x: int, y: str) -> None: pass |
| 427 |
f(x=1, *[2]) |
| 428 |
[builtins fixtures/list.pyi] |
| 429 |
[out] |
| 430 |
main:3: error: "f" gets multiple values for keyword argument "x" |
| 431 |
main:3: error: Argument 1 to "f" has incompatible type "*list[int]"; expected "str" |
| 432 |
|
| 433 |
[case testVarArgsAfterKeywordArgInCall2] |
| 434 |
# see: mypy issue #2729 |
| 435 |
def f(x: int, y: str) -> None: pass |
| 436 |
f(y='x', *[1]) |
| 437 |
[builtins fixtures/list.pyi] |
| 438 |
[out] |
| 439 |
main:3: error: "f" gets multiple values for keyword argument "y" |
| 440 |
main:3: error: Argument 1 to "f" has incompatible type "*list[int]"; expected "str" |
| 441 |
|
| 442 |
[case testVarArgsAfterKeywordArgInCall3] |
| 443 |
def f(x: int, y: str) -> None: pass |
| 444 |
f(y='x', *(1,)) |
| 445 |
[builtins fixtures/list.pyi] |
| 446 |
|
| 447 |
[case testVarArgsAfterKeywordArgInCall4] |
| 448 |
def f(x: int, *, y: str) -> None: pass |
| 449 |
f(y='x', *[1]) |
| 450 |
[builtins fixtures/list.pyi] |
| 451 |
|
| 452 |
[case testVarArgsAfterKeywordArgInCall5] |
| 453 |
def f(x: int, *, y: str) -> None: pass |
| 454 |
f(y='x', *(1,)) |
| 455 |
[builtins fixtures/list.pyi] |
| 456 |
|
| 457 |
[case testVarArgsEmptyList] |
| 458 |
from typing import List |
| 459 |
|
| 460 |
def foo() -> None: |
| 461 |
pass |
| 462 |
|
| 463 |
lst: List[int] = [] |
| 464 |
foo(*lst) |
| 465 |
[builtins fixtures/list.pyi] |
| 466 |
|
| 467 |
[case testVarArgsEmptyTuple] |
| 468 |
def foo() -> None: |
| 469 |
pass |
| 470 |
|
| 471 |
foo(*()) |
| 472 |
[builtins fixtures/tuple.pyi] |
| 473 |
|
| 474 |
-- Overloads + varargs |
| 475 |
-- ------------------- |
| 476 |
|
| 477 |
|
| 478 |
[case testIntersectionTypesAndVarArgs] |
| 479 |
# flags: --no-strict-optional |
| 480 |
from foo import * |
| 481 |
[file foo.pyi] |
| 482 |
from typing import overload |
| 483 |
a, b = None, None # type: (A, B) |
| 484 |
|
| 485 |
if int(): |
| 486 |
b = f() # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 487 |
if int(): |
| 488 |
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 489 |
if int(): |
| 490 |
b = f(a, b) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 491 |
if int(): |
| 492 |
a = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 493 |
if int(): |
| 494 |
a = f(b, b) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 495 |
if int(): |
| 496 |
b = f(a, *[b]) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 497 |
if int(): |
| 498 |
b = f(*()) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 499 |
if int(): |
| 500 |
b = f(*(a,)) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 501 |
if int(): |
| 502 |
b = f(*(a, b)) # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 503 |
if int(): |
| 504 |
a = f(*(b,)) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 505 |
if int(): |
| 506 |
a = f(*(b, b)) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 507 |
if int(): |
| 508 |
a = f(*[b]) # E: Incompatible types in assignment (expression has type "B", variable has type "A") |
| 509 |
|
| 510 |
a = f() |
| 511 |
a = f(a) |
| 512 |
a = f(a, b) |
| 513 |
b = f(b) |
| 514 |
b = f(b, b) |
| 515 |
a = f(a, *[b]) |
| 516 |
a = f(*()) |
| 517 |
a = f(*(a,)) |
| 518 |
a = f(*(a, b)) |
| 519 |
b = f(*(b,)) |
| 520 |
b = f(*(b, b)) |
| 521 |
b = f(*[b]) |
| 522 |
|
| 523 |
class A: pass |
| 524 |
class B: pass |
| 525 |
|
| 526 |
@overload |
| 527 |
def f(a: A = None, *b: B) -> A: pass |
| 528 |
|
| 529 |
@overload |
| 530 |
def f(a: B, *b: B) -> B: pass |
| 531 |
[builtins fixtures/list.pyi] |
| 532 |
|
| 533 |
|
| 534 |
-- Caller varargs + type inference |
| 535 |
-- ------------------------------- |
| 536 |
|
| 537 |
|
| 538 |
[case testCallerVarArgsListWithTypeInference] |
| 539 |
from typing import List, TypeVar, Tuple |
| 540 |
S = TypeVar('S') |
| 541 |
T = TypeVar('T') |
| 542 |
|
| 543 |
def f(a: S, *b: T) -> Tuple[S, T]: |
| 544 |
pass |
| 545 |
|
| 546 |
class A: pass |
| 547 |
class B: pass |
| 548 |
|
| 549 |
a: A |
| 550 |
b: B |
| 551 |
aa: List[A] |
| 552 |
|
| 553 |
if int(): |
| 554 |
a, b = f(*aa) # E: Argument 1 to "f" has incompatible type "*list[A]"; expected "B" |
| 555 |
if int(): |
| 556 |
b, b = f(*aa) # E: Argument 1 to "f" has incompatible type "*list[A]"; expected "B" |
| 557 |
if int(): |
| 558 |
a, a = f(b, *aa) # E: Argument 1 to "f" has incompatible type "B"; expected "A" |
| 559 |
if int(): |
| 560 |
b, b = f(b, *aa) # E: Argument 2 to "f" has incompatible type "*list[A]"; expected "B" |
| 561 |
if int(): |
| 562 |
b, b = f(b, b, *aa) # E: Argument 3 to "f" has incompatible type "*list[A]"; expected "B" |
| 563 |
if int(): |
| 564 |
a, b = f(a, *a) # E: Expected iterable as variadic argument |
| 565 |
if int(): |
| 566 |
a, b = f(*a) # E: Expected iterable as variadic argument |
| 567 |
|
| 568 |
if int(): |
| 569 |
a, a = f(*aa) |
| 570 |
if int(): |
| 571 |
b, a = f(b, *aa) |
| 572 |
if int(): |
| 573 |
b, a = f(b, a, *aa) |
| 574 |
[builtins fixtures/list.pyi] |
| 575 |
|
| 576 |
[case testCallerVarArgsTupleWithTypeInference] |
| 577 |
from typing import TypeVar, Tuple |
| 578 |
S = TypeVar('S') |
| 579 |
T = TypeVar('T') |
| 580 |
|
| 581 |
def f(a: S, b: T) -> Tuple[S, T]: pass |
| 582 |
|
| 583 |
class A: pass |
| 584 |
class B: pass |
| 585 |
|
| 586 |
a: A |
| 587 |
b: B |
| 588 |
|
| 589 |
if int(): |
| 590 |
a, a = f(*(a, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, B]"; expected "A" |
| 591 |
if int(): |
| 592 |
b, b = f(a, *(b,)) # E: Argument 1 to "f" has incompatible type "A"; expected "B" |
| 593 |
if int(): |
| 594 |
a, a = f(*(a, b)) # E: Argument 1 to "f" has incompatible type "*tuple[A, B]"; expected "A" |
| 595 |
if int(): |
| 596 |
b, b = f(a, *(b,)) # E: Argument 1 to "f" has incompatible type "A"; expected "B" |
| 597 |
if int(): |
| 598 |
a, b = f(*(a, b, b)) # E: Too many arguments for "f" |
| 599 |
if int(): |
| 600 |
a, b = f(*(a, b)) |
| 601 |
if int(): |
| 602 |
a, b = f(a, *(b,)) |
| 603 |
[builtins fixtures/list.pyi] |
| 604 |
|
| 605 |
[case testCallerVarargsAndComplexTypeInference] |
| 606 |
from typing import List, TypeVar, Generic, Tuple |
| 607 |
T = TypeVar('T') |
| 608 |
S = TypeVar('S') |
| 609 |
a: A |
| 610 |
b: B |
| 611 |
ao: List[object] |
| 612 |
aa: List[A] |
| 613 |
ab: List[B] |
| 614 |
|
| 615 |
class G(Generic[T]): |
| 616 |
def f(self, *a: S) -> Tuple[List[S], List[T]]: |
| 617 |
pass |
| 618 |
|
| 619 |
class A: pass |
| 620 |
class B: pass |
| 621 |
|
| 622 |
if int(): |
| 623 |
a, aa = G().f(*[a]) # E: Incompatible types in assignment (expression has type "list[A]", variable has type "A") |
| 624 |
if int(): |
| 625 |
aa, a = G().f(*[a]) # E: Incompatible types in assignment (expression has type "list[Never]", variable has type "A") |
| 626 |
if int(): |
| 627 |
ab, aa = G().f(*[a]) # E: Argument 1 to "f" of "G" has incompatible type "*list[A]"; expected "B" |
| 628 |
if int(): |
| 629 |
ao, ao = G().f(*[a]) |
| 630 |
if int(): |
| 631 |
aa, aa = G().f(*[a]) |
| 632 |
[builtins fixtures/list.pyi] |
| 633 |
|
| 634 |
[case testCallerTupleVarArgsAndGenericCalleeVarArg] |
| 635 |
from typing import TypeVar |
| 636 |
|
| 637 |
T = TypeVar('T') |
| 638 |
|
| 639 |
def f(*args: T) -> T: ... |
| 640 |
reveal_type(f(*(1, None))) # N: Revealed type is "Literal[1]? | None" |
| 641 |
reveal_type(f(1, *(None, 1))) # N: Revealed type is "Literal[1]? | None" |
| 642 |
reveal_type(f(1, *(1, None))) # N: Revealed type is "Literal[1]? | None" |
| 643 |
[builtins fixtures/tuple.pyi] |
| 644 |
|
| 645 |
|
| 646 |
-- Comment signatures |
| 647 |
-- ------------------ |
| 648 |
|
| 649 |
|
| 650 |
[case testVarArgsAndCommentSignature] |
| 651 |
import typing |
| 652 |
def f(*x): # type: (*int) -> None |
| 653 |
pass |
| 654 |
f(1) |
| 655 |
f(1, 2) |
| 656 |
f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int" |
| 657 |
f(1, '') # E: Argument 2 to "f" has incompatible type "str"; expected "int" |
| 658 |
[builtins fixtures/list.pyi] |
| 659 |
|
| 660 |
|
| 661 |
-- Subtyping |
| 662 |
-- --------- |
| 663 |
|
| 664 |
|
| 665 |
[case testVarArgsFunctionSubtyping] |
| 666 |
from typing import Callable |
| 667 |
x: Callable[[int], None] |
| 668 |
def f(*x: int) -> None: pass |
| 669 |
def g(*x: str) -> None: pass |
| 670 |
x = f |
| 671 |
x = g # E: Incompatible types in assignment (expression has type "def g(*x: str) -> None", variable has type "Callable[[int], None]") |
| 672 |
[builtins fixtures/list.pyi] |
| 673 |
[out] |
| 674 |
|
| 675 |
|
| 676 |
-- Decorated method where self is implied by *args |
| 677 |
-- ----------------------------------------------- |
| 678 |
|
| 679 |
[case testVarArgsCallableSelf] |
| 680 |
from typing import Callable |
| 681 |
def cm(func) -> Callable[..., None]: pass |
| 682 |
class C: |
| 683 |
@cm |
| 684 |
def foo(self) -> None: pass |
| 685 |
C().foo() |
| 686 |
C().foo(1) # The decorator's return type says this should be okay |
| 687 |
|
| 688 |
[case testInvariantDictArgNote] |
| 689 |
from typing import Dict, Sequence |
| 690 |
def f(x: Dict[str, Sequence[int]]) -> None: pass |
| 691 |
def g(x: Dict[str, float]) -> None: pass |
| 692 |
def h(x: Dict[str, int]) -> None: pass |
| 693 |
a = {'a': [1, 2]} |
| 694 |
b = {'b': ['c', 'd']} |
| 695 |
c = {'c': 1.0} |
| 696 |
d = {'d': 1} |
| 697 |
f(a) # E: Argument 1 to "f" has incompatible type "dict[str, list[int]]"; expected "dict[str, Sequence[int]]" \ |
| 698 |
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 699 |
# N: Consider using "Mapping" instead, which is covariant in the value type |
| 700 |
f(b) # E: Argument 1 to "f" has incompatible type "dict[str, list[str]]"; expected "dict[str, Sequence[int]]" |
| 701 |
g(c) |
| 702 |
g(d) # E: Argument 1 to "g" has incompatible type "dict[str, int]"; expected "dict[str, float]" \ |
| 703 |
# N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 704 |
# N: Consider using "Mapping" instead, which is covariant in the value type |
| 705 |
h(c) # E: Argument 1 to "h" has incompatible type "dict[str, float]"; expected "dict[str, int]" |
| 706 |
h(d) |
| 707 |
[builtins fixtures/dict.pyi] |
| 708 |
[typing fixtures/typing-medium.pyi] |
| 709 |
|
| 710 |
[case testInvariantListArgNote] |
| 711 |
from typing import List, Union |
| 712 |
def f(numbers: List[Union[int, float]]) -> None: pass |
| 713 |
a = [1, 2] |
| 714 |
f(a) # E: Argument 1 to "f" has incompatible type "list[int]"; expected "list[int | float]" \ |
| 715 |
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 716 |
# N: Consider using "Sequence" instead, which is covariant |
| 717 |
x = [1] |
| 718 |
y = ['a'] |
| 719 |
if int(): |
| 720 |
x = y # E: Incompatible types in assignment (expression has type "list[str]", variable has type "list[int]") |
| 721 |
[builtins fixtures/list.pyi] |
| 722 |
|
| 723 |
[case testInvariantTypeConfusingNames] |
| 724 |
from typing import TypeVar |
| 725 |
class Listener: pass |
| 726 |
class DictReader: pass |
| 727 |
def f(x: Listener) -> None: pass |
| 728 |
def g(y: DictReader) -> None: pass |
| 729 |
a = [1, 2] |
| 730 |
b = {'b': 1} |
| 731 |
f(a) # E: Argument 1 to "f" has incompatible type "list[int]"; expected "Listener" |
| 732 |
g(b) # E: Argument 1 to "g" has incompatible type "dict[str, int]"; expected "DictReader" |
| 733 |
[builtins fixtures/dict.pyi] |
| 734 |
|
| 735 |
[case testInvariantTypeConfusingNames2] |
| 736 |
from typing import Iterable, Generic, TypeVar, List |
| 737 |
|
| 738 |
T = TypeVar('T') |
| 739 |
|
| 740 |
class I(Iterable[T]): |
| 741 |
... |
| 742 |
|
| 743 |
class Bad(Generic[T]): |
| 744 |
... |
| 745 |
|
| 746 |
def bar(*args: float) -> float: |
| 747 |
... |
| 748 |
|
| 749 |
good1: Iterable[float] |
| 750 |
good2: List[float] |
| 751 |
good3: I[float] |
| 752 |
bad1: I[str] |
| 753 |
bad2: Bad[float] |
| 754 |
bar(*good1) |
| 755 |
bar(*good2) |
| 756 |
bar(*good3) |
| 757 |
bar(*bad1) # E: Argument 1 to "bar" has incompatible type "*I[str]"; expected "float" |
| 758 |
bar(*bad2) # E: Expected iterable as variadic argument |
| 759 |
[builtins fixtures/dict.pyi] |
| 760 |
|
| 761 |
-- Keyword arguments unpacking |
| 762 |
|
| 763 |
[case testUnpackKwargsReveal] |
| 764 |
from typing import TypedDict |
| 765 |
from typing_extensions import Unpack |
| 766 |
|
| 767 |
class Person(TypedDict): |
| 768 |
name: str |
| 769 |
age: int |
| 770 |
def foo(arg: bool, **kwargs: Unpack[Person]) -> None: ... |
| 771 |
|
| 772 |
reveal_type(foo) # N: Revealed type is "def (arg: builtins.bool, **kwargs: Unpack[TypedDict('__main__.Person', {'name': builtins.str, 'age': builtins.int})])" |
| 773 |
[builtins fixtures/dict.pyi] |
| 774 |
[typing fixtures/typing-typeddict.pyi] |
| 775 |
|
| 776 |
[case testUnpackOutsideOfKwargs] |
| 777 |
from typing import TypedDict |
| 778 |
from typing_extensions import Unpack |
| 779 |
class Person(TypedDict): |
| 780 |
name: str |
| 781 |
age: int |
| 782 |
|
| 783 |
def foo(x: Unpack[Person]) -> None: # E: Unpack is only valid in a variadic position |
| 784 |
... |
| 785 |
def bar(x: int, *args: Unpack[Person]) -> None: # E: "Person" cannot be unpacked (must be tuple or TypeVarTuple) |
| 786 |
... |
| 787 |
def baz(**kwargs: Unpack[Person]) -> None: # OK |
| 788 |
... |
| 789 |
[builtins fixtures/dict.pyi] |
| 790 |
[typing fixtures/typing-typeddict.pyi] |
| 791 |
|
| 792 |
[case testUnpackWithoutTypedDict] |
| 793 |
from typing_extensions import Unpack |
| 794 |
|
| 795 |
def foo(**kwargs: Unpack[dict]) -> None: # E: Unpack item in ** parameter must be a TypedDict |
| 796 |
... |
| 797 |
[builtins fixtures/dict.pyi] |
| 798 |
|
| 799 |
[case testUnpackWithDuplicateKeywords] |
| 800 |
from typing import TypedDict |
| 801 |
from typing_extensions import Unpack |
| 802 |
|
| 803 |
class Person(TypedDict): |
| 804 |
name: str |
| 805 |
age: int |
| 806 |
def foo(name: str, **kwargs: Unpack[Person]) -> None: # E: Overlap between parameter names and ** TypedDict items: "name" |
| 807 |
... |
| 808 |
[builtins fixtures/dict.pyi] |
| 809 |
[typing fixtures/typing-typeddict.pyi] |
| 810 |
|
| 811 |
[case testUnpackWithDuplicateKeywordKwargs] |
| 812 |
from typing_extensions import Unpack |
| 813 |
from typing import Dict, List, TypedDict |
| 814 |
|
| 815 |
class Spec(TypedDict): |
| 816 |
args: List[int] |
| 817 |
kwargs: Dict[int, int] |
| 818 |
def foo(**kwargs: Unpack[Spec]) -> None: # Allowed |
| 819 |
... |
| 820 |
foo(args=[1], kwargs={"2": 3}) # E: Dict entry 0 has incompatible type "str": "int"; expected "int": "int" |
| 821 |
[builtins fixtures/dict.pyi] |
| 822 |
[typing fixtures/typing-typeddict.pyi] |
| 823 |
|
| 824 |
[case testUnpackKwargsNonIdentifier] |
| 825 |
from typing import TypedDict |
| 826 |
from typing_extensions import Unpack |
| 827 |
|
| 828 |
Weird = TypedDict("Weird", {"@": int}) |
| 829 |
|
| 830 |
def foo(**kwargs: Unpack[Weird]) -> None: |
| 831 |
reveal_type(kwargs["@"]) # N: Revealed type is "builtins.int" |
| 832 |
foo(**{"@": 42}) |
| 833 |
foo(**{"no": "way"}) # E: Argument 1 to "foo" has incompatible type "**dict[str, str]"; expected "int" |
| 834 |
[builtins fixtures/dict.pyi] |
| 835 |
[typing fixtures/typing-typeddict.pyi] |
| 836 |
|
| 837 |
[case testUnpackKwargsEmpty] |
| 838 |
from typing import TypedDict |
| 839 |
from typing_extensions import Unpack |
| 840 |
|
| 841 |
Empty = TypedDict("Empty", {}) |
| 842 |
|
| 843 |
def foo(**kwargs: Unpack[Empty]) -> None: |
| 844 |
reveal_type(kwargs) # N: Revealed type is "TypedDict('__main__.Empty', {})" |
| 845 |
foo() |
| 846 |
foo(x=1) # E: Unexpected keyword argument "x" for "foo" |
| 847 |
[builtins fixtures/dict.pyi] |
| 848 |
[typing fixtures/typing-typeddict.pyi] |
| 849 |
|
| 850 |
[case testUnpackTypedDictTotality] |
| 851 |
from typing import TypedDict |
| 852 |
from typing_extensions import Unpack |
| 853 |
|
| 854 |
class Circle(TypedDict, total=True): |
| 855 |
radius: int |
| 856 |
color: str |
| 857 |
x: int |
| 858 |
y: int |
| 859 |
|
| 860 |
def foo(**kwargs: Unpack[Circle]): |
| 861 |
... |
| 862 |
foo(x=0, y=0, color='orange') # E: Missing named argument "radius" for "foo" |
| 863 |
|
| 864 |
class Square(TypedDict, total=False): |
| 865 |
side: int |
| 866 |
color: str |
| 867 |
|
| 868 |
def bar(**kwargs: Unpack[Square]): |
| 869 |
... |
| 870 |
bar(side=12) |
| 871 |
[builtins fixtures/dict.pyi] |
| 872 |
[typing fixtures/typing-typeddict.pyi] |
| 873 |
|
| 874 |
[case testUnpackUnexpectedKeyword] |
| 875 |
from typing import TypedDict |
| 876 |
from typing_extensions import Unpack |
| 877 |
|
| 878 |
class Person(TypedDict, total=False): |
| 879 |
name: str |
| 880 |
age: int |
| 881 |
|
| 882 |
def foo(**kwargs: Unpack[Person]) -> None: |
| 883 |
... |
| 884 |
foo(name='John', age=42, department='Sales') # E: Unexpected keyword argument "department" for "foo" |
| 885 |
foo(name='Jennifer', age=38) |
| 886 |
[builtins fixtures/dict.pyi] |
| 887 |
[typing fixtures/typing-typeddict.pyi] |
| 888 |
|
| 889 |
[case testUnpackKeywordTypes] |
| 890 |
from typing import TypedDict |
| 891 |
from typing_extensions import Unpack |
| 892 |
|
| 893 |
class Person(TypedDict): |
| 894 |
name: str |
| 895 |
age: int |
| 896 |
|
| 897 |
def foo(**kwargs: Unpack[Person]): |
| 898 |
... |
| 899 |
foo(name='John', age='42') # E: Argument "age" to "foo" has incompatible type "str"; expected "int" |
| 900 |
foo(name='Jennifer', age=38) |
| 901 |
[builtins fixtures/dict.pyi] |
| 902 |
[typing fixtures/typing-typeddict.pyi] |
| 903 |
|
| 904 |
[case testUnpackKeywordTypesTypedDict] |
| 905 |
from typing import TypedDict |
| 906 |
from typing_extensions import Unpack |
| 907 |
|
| 908 |
class Person(TypedDict): |
| 909 |
name: str |
| 910 |
age: int |
| 911 |
|
| 912 |
class LegacyPerson(TypedDict): |
| 913 |
name: str |
| 914 |
age: str |
| 915 |
|
| 916 |
def foo(**kwargs: Unpack[Person]) -> None: |
| 917 |
... |
| 918 |
lp = LegacyPerson(name="test", age="42") |
| 919 |
foo(**lp) # E: Argument "age" to "foo" has incompatible type "str"; expected "int" |
| 920 |
[builtins fixtures/dict.pyi] |
| 921 |
[typing fixtures/typing-typeddict.pyi] |
| 922 |
|
| 923 |
[case testFunctionBodyWithUnpackedKwargs] |
| 924 |
from typing import TypedDict |
| 925 |
from typing_extensions import Unpack |
| 926 |
|
| 927 |
class Person(TypedDict): |
| 928 |
name: str |
| 929 |
age: int |
| 930 |
|
| 931 |
def foo(**kwargs: Unpack[Person]) -> int: |
| 932 |
name: str = kwargs['name'] |
| 933 |
age: str = kwargs['age'] # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 934 |
department: str = kwargs['department'] # E: TypedDict "Person" has no key "department" |
| 935 |
return kwargs['age'] |
| 936 |
[builtins fixtures/dict.pyi] |
| 937 |
[typing fixtures/typing-typeddict.pyi] |
| 938 |
|
| 939 |
[case testUnpackKwargsOverrides] |
| 940 |
from typing import TypedDict |
| 941 |
from typing_extensions import Unpack |
| 942 |
|
| 943 |
class Person(TypedDict): |
| 944 |
name: str |
| 945 |
age: int |
| 946 |
|
| 947 |
class Base: |
| 948 |
def foo(self, **kwargs: Unpack[Person]) -> None: ... |
| 949 |
class SubGood(Base): |
| 950 |
def foo(self, *, name: str, age: int, extra: bool = False) -> None: ... |
| 951 |
class SubBad(Base): |
| 952 |
def foo(self, *, name: str, age: str) -> None: ... # E: Argument 2 of "foo" is incompatible with supertype "Base"; supertype defines the argument type as "int" \ |
| 953 |
# N: This violates the Liskov substitution principle \ |
| 954 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides |
| 955 |
[builtins fixtures/dict.pyi] |
| 956 |
[typing fixtures/typing-typeddict.pyi] |
| 957 |
|
| 958 |
[case testUnpackKwargsOverridesTypedDict] |
| 959 |
from typing import TypedDict |
| 960 |
from typing_extensions import Unpack |
| 961 |
|
| 962 |
class Person(TypedDict): |
| 963 |
name: str |
| 964 |
age: int |
| 965 |
|
| 966 |
class PersonExtra(Person, total=False): |
| 967 |
extra: bool |
| 968 |
|
| 969 |
class Unrelated(TypedDict): |
| 970 |
baz: int |
| 971 |
|
| 972 |
class Base: |
| 973 |
def foo(self, **kwargs: Unpack[Person]) -> None: ... |
| 974 |
class SubGood(Base): |
| 975 |
def foo(self, **kwargs: Unpack[PersonExtra]) -> None: ... |
| 976 |
class SubBad(Base): |
| 977 |
def foo(self, **kwargs: Unpack[Unrelated]) -> None: ... # E: Signature of "foo" incompatible with supertype "Base" \ |
| 978 |
# N: Superclass: \ |
| 979 |
# N: def foo(*, name: str, age: int) -> None \ |
| 980 |
# N: Subclass: \ |
| 981 |
# N: def foo(self, *, baz: int) -> None |
| 982 |
[builtins fixtures/dict.pyi] |
| 983 |
[typing fixtures/typing-typeddict.pyi] |
| 984 |
|
| 985 |
[case testUnpackKwargsGeneric] |
| 986 |
from typing import Generic, TypedDict, TypeVar |
| 987 |
from typing_extensions import Unpack |
| 988 |
|
| 989 |
T = TypeVar("T") |
| 990 |
class Person(TypedDict, Generic[T]): |
| 991 |
name: str |
| 992 |
value: T |
| 993 |
|
| 994 |
def foo(**kwargs: Unpack[Person[T]]) -> T: ... |
| 995 |
reveal_type(foo(name="test", value=42)) # N: Revealed type is "builtins.int" |
| 996 |
[builtins fixtures/dict.pyi] |
| 997 |
[typing fixtures/typing-typeddict.pyi] |
| 998 |
|
| 999 |
[case testUnpackKwargsInference] |
| 1000 |
from typing import Generic, TypedDict, TypeVar, Protocol |
| 1001 |
from typing_extensions import Unpack |
| 1002 |
|
| 1003 |
T_contra = TypeVar("T_contra", contravariant=True) |
| 1004 |
class CBPerson(Protocol[T_contra]): |
| 1005 |
def __call__(self, **kwargs: Unpack[Person[T_contra]]) -> None: ... |
| 1006 |
|
| 1007 |
T = TypeVar("T") |
| 1008 |
class Person(TypedDict, Generic[T]): |
| 1009 |
name: str |
| 1010 |
value: T |
| 1011 |
|
| 1012 |
def test(cb: CBPerson[T]) -> T: ... |
| 1013 |
|
| 1014 |
def foo(*, name: str, value: int) -> None: ... |
| 1015 |
reveal_type(test(foo)) # N: Revealed type is "builtins.int" |
| 1016 |
[builtins fixtures/dict.pyi] |
| 1017 |
[typing fixtures/typing-typeddict.pyi] |
| 1018 |
|
| 1019 |
[case testUnpackKwargsOverload] |
| 1020 |
from typing import TypedDict, Any, overload |
| 1021 |
from typing_extensions import Unpack |
| 1022 |
|
| 1023 |
class Person(TypedDict): |
| 1024 |
name: str |
| 1025 |
age: int |
| 1026 |
|
| 1027 |
class Fruit(TypedDict): |
| 1028 |
sort: str |
| 1029 |
taste: int |
| 1030 |
|
| 1031 |
@overload |
| 1032 |
def foo(**kwargs: Unpack[Person]) -> int: ... |
| 1033 |
@overload |
| 1034 |
def foo(**kwargs: Unpack[Fruit]) -> str: ... |
| 1035 |
def foo(**kwargs: Any) -> Any: |
| 1036 |
... |
| 1037 |
|
| 1038 |
reveal_type(foo(sort="test", taste=999)) # N: Revealed type is "builtins.str" |
| 1039 |
[builtins fixtures/dict.pyi] |
| 1040 |
[typing fixtures/typing-typeddict.pyi] |
| 1041 |
|
| 1042 |
[case testUnpackKwargsJoin] |
| 1043 |
from typing import TypedDict |
| 1044 |
from typing_extensions import Unpack |
| 1045 |
|
| 1046 |
class Person(TypedDict): |
| 1047 |
name: str |
| 1048 |
age: int |
| 1049 |
|
| 1050 |
def foo(*, name: str, age: int) -> None: ... |
| 1051 |
def bar(**kwargs: Unpack[Person]) -> None: ... |
| 1052 |
|
| 1053 |
reveal_type([foo, bar]) # N: Revealed type is "builtins.list[def (*, name: builtins.str, age: builtins.int)]" |
| 1054 |
reveal_type([bar, foo]) # N: Revealed type is "builtins.list[def (*, name: builtins.str, age: builtins.int)]" |
| 1055 |
[builtins fixtures/dict.pyi] |
| 1056 |
[typing fixtures/typing-typeddict.pyi] |
| 1057 |
|
| 1058 |
[case testUnpackKwargsParamSpec] |
| 1059 |
from typing import Callable, Any, TypedDict, TypeVar, List |
| 1060 |
from typing_extensions import ParamSpec, Unpack |
| 1061 |
|
| 1062 |
class Person(TypedDict): |
| 1063 |
name: str |
| 1064 |
age: int |
| 1065 |
|
| 1066 |
P = ParamSpec('P') |
| 1067 |
T = TypeVar('T') |
| 1068 |
|
| 1069 |
def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ... |
| 1070 |
|
| 1071 |
@dec |
| 1072 |
def g(**kwargs: Unpack[Person]) -> int: ... |
| 1073 |
|
| 1074 |
reveal_type(g) # N: Revealed type is "def (*, name: builtins.str, age: builtins.int) -> builtins.list[builtins.int]" |
| 1075 |
[builtins fixtures/dict.pyi] |
| 1076 |
[typing fixtures/typing-typeddict.pyi] |
| 1077 |
|
| 1078 |
[case testUnpackGenericTypedDictImplicitAnyEnabled] |
| 1079 |
from typing import Generic, TypedDict, TypeVar |
| 1080 |
from typing_extensions import Unpack |
| 1081 |
|
| 1082 |
T = TypeVar("T") |
| 1083 |
class TD(TypedDict, Generic[T]): |
| 1084 |
key: str |
| 1085 |
value: T |
| 1086 |
|
| 1087 |
def foo(**kwds: Unpack[TD]) -> None: ... # Same as `TD[Any]` |
| 1088 |
foo(key="yes", value=42) |
| 1089 |
foo(key="yes", value="ok") |
| 1090 |
[builtins fixtures/dict.pyi] |
| 1091 |
[typing fixtures/typing-typeddict.pyi] |
| 1092 |
|
| 1093 |
[case testUnpackGenericTypedDictImplicitAnyDisabled] |
| 1094 |
# flags: --disallow-any-generics |
| 1095 |
from typing import Generic, TypedDict, TypeVar |
| 1096 |
from typing_extensions import Unpack |
| 1097 |
|
| 1098 |
T = TypeVar("T") |
| 1099 |
class TD(TypedDict, Generic[T]): |
| 1100 |
key: str |
| 1101 |
value: T |
| 1102 |
|
| 1103 |
def foo(**kwds: Unpack[TD]) -> None: ... # E: Missing type arguments for generic type "TD" |
| 1104 |
foo(key="yes", value=42) |
| 1105 |
foo(key="yes", value="ok") |
| 1106 |
[builtins fixtures/dict.pyi] |
| 1107 |
[typing fixtures/typing-typeddict.pyi] |
| 1108 |
|
| 1109 |
[case testUnpackNoCrashOnEmpty] |
| 1110 |
from typing_extensions import Unpack |
| 1111 |
|
| 1112 |
class C: |
| 1113 |
def __init__(self, **kwds: Unpack) -> None: ... # E: Unpack[...] requires exactly one type argument |
| 1114 |
class D: |
| 1115 |
def __init__(self, **kwds: Unpack[int, str]) -> None: ... # E: Unpack[...] requires exactly one type argument |
| 1116 |
[builtins fixtures/dict.pyi] |
| 1117 |
|
| 1118 |
[case testUnpackInCallableType] |
| 1119 |
# flags: --python-version 3.11 |
| 1120 |
from typing import Callable, TypedDict |
| 1121 |
from typing_extensions import Unpack |
| 1122 |
|
| 1123 |
class TD(TypedDict): |
| 1124 |
key: str |
| 1125 |
value: str |
| 1126 |
|
| 1127 |
foo: Callable[[Unpack[TD]], None] |
| 1128 |
foo(key="yes", value=42) # E: Argument "value" has incompatible type "int"; expected "str" |
| 1129 |
foo(key="yes", value="ok") |
| 1130 |
|
| 1131 |
bad: Callable[[*TD], None] # E: "TD" cannot be unpacked (must be tuple or TypeVarTuple) |
| 1132 |
[builtins fixtures/dict.pyi] |
| 1133 |
[typing fixtures/typing-typeddict.pyi] |