Debian Codegraph @

test-data/unit/check-functools.test

raw
1 [case testTotalOrderingEqLt]
2 from functools import total_ordering
3
4 @total_ordering
5 class Ord:
6 def __eq__(self, other: object) -> bool:
7 return False
8
9 def __lt__(self, other: "Ord") -> bool:
10 return False
11
12 reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.bool"
13 reveal_type(Ord() <= Ord()) # N: Revealed type is "builtins.bool"
14 reveal_type(Ord() == Ord()) # N: Revealed type is "builtins.bool"
15 reveal_type(Ord() > Ord()) # N: Revealed type is "builtins.bool"
16 reveal_type(Ord() >= Ord()) # N: Revealed type is "builtins.bool"
17
18 Ord() < 1 # E: Unsupported operand types for < ("Ord" and "int")
19 Ord() <= 1 # E: Unsupported operand types for <= ("Ord" and "int")
20 Ord() == 1
21 Ord() > 1 # E: Unsupported operand types for > ("Ord" and "int")
22 Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int")
23 [builtins fixtures/dict.pyi]
24
25 [case testTotalOrderingLambda]
26 from functools import total_ordering
27 from typing import Any, Callable, ClassVar
28
29 @total_ordering
30 class Ord:
31 __eq__: Callable[[Any, object], bool] = lambda self, other: False
32 __lt__: Callable[[Any, "Ord"], bool] = lambda self, other: False
33
34 reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.bool"
35 reveal_type(Ord() <= Ord()) # N: Revealed type is "builtins.bool"
36 reveal_type(Ord() == Ord()) # N: Revealed type is "builtins.bool"
37 reveal_type(Ord() > Ord()) # N: Revealed type is "builtins.bool"
38 reveal_type(Ord() >= Ord()) # N: Revealed type is "builtins.bool"
39
40 Ord() < 1 # E: Argument 1 has incompatible type "int"; expected "Ord"
41 Ord() <= 1 # E: Unsupported operand types for <= ("Ord" and "int")
42 Ord() == 1
43 Ord() > 1 # E: Unsupported operand types for > ("Ord" and "int")
44 Ord() >= 1 # E: Unsupported operand types for >= ("Ord" and "int")
45 [builtins fixtures/dict.pyi]
46
47 [case testTotalOrderingNonCallable]
48 from functools import total_ordering
49
50 @total_ordering
51 class Ord(object):
52 def __eq__(self, other: object) -> bool:
53 return False
54
55 __lt__ = 5
56
57 Ord() <= Ord() # E: Unsupported left operand type for <= ("Ord")
58 Ord() > Ord() # E: "int" not callable
59 Ord() >= Ord() # E: Unsupported left operand type for >= ("Ord")
60 [builtins fixtures/dict.pyi]
61
62 [case testTotalOrderingReturnNotBool]
63 from functools import total_ordering
64
65 @total_ordering
66 class Ord:
67 def __eq__(self, other: object) -> bool:
68 return False
69
70 def __lt__(self, other: "Ord") -> str:
71 return "blah"
72
73 reveal_type(Ord() < Ord()) # N: Revealed type is "builtins.str"
74 reveal_type(Ord() <= Ord()) # N: Revealed type is "Any"
75 reveal_type(Ord() == Ord()) # N: Revealed type is "builtins.bool"
76 reveal_type(Ord() > Ord()) # N: Revealed type is "Any"
77 reveal_type(Ord() >= Ord()) # N: Revealed type is "Any"
78 [builtins fixtures/dict.pyi]
79
80 [case testTotalOrderingAllowsAny]
81 from functools import total_ordering
82
83 @total_ordering
84 class Ord:
85 def __eq__(self, other):
86 return False
87
88 def __gt__(self, other):
89 return False
90
91 reveal_type(Ord() < Ord()) # N: Revealed type is "Any"
92 Ord() <= Ord() # E: Unsupported left operand type for <= ("Ord")
93 reveal_type(Ord() == Ord()) # N: Revealed type is "Any"
94 reveal_type(Ord() > Ord()) # N: Revealed type is "Any"
95 Ord() >= Ord() # E: Unsupported left operand type for >= ("Ord")
96
97 Ord() < 1 # E: Unsupported left operand type for < ("Ord")
98 Ord() <= 1 # E: Unsupported left operand type for <= ("Ord")
99 Ord() == 1
100 Ord() > 1
101 Ord() >= 1 # E: Unsupported left operand type for >= ("Ord")
102 [builtins fixtures/dict.pyi]
103
104 [case testCachedProperty]
105 from functools import cached_property
106 class Parent:
107 @property
108 def f(self) -> str: pass
109 class Child(Parent):
110 @cached_property
111 def f(self) -> str: pass
112 @cached_property
113 def g(self) -> int: pass
114 @cached_property # E: Too many arguments for property
115 def h(self, arg) -> int: pass
116 reveal_type(Parent().f) # N: Revealed type is "builtins.str"
117 reveal_type(Child().f) # N: Revealed type is "builtins.str"
118 reveal_type(Child().g) # N: Revealed type is "builtins.int"
119 Child().f = "Hello World"
120 Child().g = "invalid" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
121 [file functools.pyi]
122 import sys
123 from typing import TypeVar, Generic
124 _T = TypeVar('_T')
125 class cached_property(Generic[_T]): ...
126 [builtins fixtures/property.pyi]
127
128 [case testTotalOrderingWithForwardReference]
129 from typing import Generic, Any, TypeVar
130 import functools
131
132 T = TypeVar("T", bound="C")
133
134 @functools.total_ordering
135 class D(Generic[T]):
136 def __lt__(self, other: Any) -> bool:
137 ...
138
139 class C:
140 pass
141
142 def f(d: D[C]) -> None:
143 reveal_type(d.__gt__) # N: Revealed type is "def (other: Any) -> builtins.bool"
144
145 d: D[int] # E: Type argument "int" of "D" must be a subtype of "C"
146 [builtins fixtures/dict.pyi]
147
148 [case testFunctoolsPartialBasic]
149 from typing import Callable
150 import functools
151
152 def foo(a: int, b: str, c: int = 5) -> int: ...
153
154 p1 = functools.partial(foo)
155 p1(1, "a", 3) # OK
156 p1(1, "a", c=3) # OK
157 p1(1, b="a", c=3) # OK
158
159 reveal_type(p1) # N: Revealed type is "functools.partial[builtins.int]"
160
161 def takes_callable_int(f: Callable[..., int]) -> None: ...
162 def takes_callable_str(f: Callable[..., str]) -> None: ...
163 takes_callable_int(p1)
164 takes_callable_str(p1) # E: Argument 1 to "takes_callable_str" has incompatible type "partial[int]"; expected "Callable[..., str]" \
165 # N: "partial[int].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> int"
166
167 p2 = functools.partial(foo, 1)
168 p2("a") # OK
169 p2("a", 3) # OK
170 p2("a", c=3) # OK
171 p2(1, 3) # E: Argument 1 to "foo" has incompatible type "int"; expected "str"
172 p2(1, "a", 3) # E: Too many arguments for "foo" \
173 # E: Argument 1 to "foo" has incompatible type "int"; expected "str" \
174 # E: Argument 2 to "foo" has incompatible type "str"; expected "int"
175 p2(a=1, b="a", c=3) # E: Unexpected keyword argument "a" for "foo"
176
177 p3 = functools.partial(foo, b="a")
178 p3(1) # OK
179 p3(1, c=3) # OK
180 p3(a=1) # OK
181 p3(1, b="a", c=3) # OK, keywords can be clobbered
182 p3(1, 3) # E: Too many positional arguments for "foo" \
183 # E: Argument 2 to "foo" has incompatible type "int"; expected "str"
184
185 functools.partial(foo, "a") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
186 functools.partial(foo, b=1) # E: Argument "b" to "foo" has incompatible type "int"; expected "str"
187 functools.partial(foo, a=1, b=2, c=3) # E: Argument "b" to "foo" has incompatible type "int"; expected "str"
188 functools.partial(1) # E: "int" not callable \
189 # E: Argument 1 to "partial" has incompatible type "int"; expected "Callable[..., Never]"
190 [builtins fixtures/dict.pyi]
191
192 [case testFunctoolsPartialStar]
193 import functools
194 from typing import List
195
196 def foo(a: int, b: str, *args: int, d: str, **kwargs: int) -> int: ...
197
198 p1 = functools.partial(foo, 1, d="a", x=9)
199 p1("a", 2, 3, 4) # OK
200 p1("a", 2, 3, 4, d="a") # OK
201 p1("a", 2, 3, 4, "a") # E: Argument 5 to "foo" has incompatible type "str"; expected "int"
202 p1("a", 2, 3, 4, x="a") # E: Argument "x" to "foo" has incompatible type "str"; expected "int"
203
204 p2 = functools.partial(foo, 1, "a")
205 p2(2, 3, 4, d="a") # OK
206 p2("a") # E: Missing named argument "d" for "foo" \
207 # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
208 p2(2, 3, 4) # E: Missing named argument "d" for "foo"
209
210 functools.partial(foo, 1, "a", "b", "c", d="a") # E: Argument 3 to "foo" has incompatible type "str"; expected "int" \
211 # E: Argument 4 to "foo" has incompatible type "str"; expected "int"
212
213 def bar(*a: bytes, **k: int):
214 p1("a", 2, 3, 4, d="a", **k)
215 p1("a", d="a", **k)
216 p1("a", **k) # E: Argument 2 to "foo" has incompatible type "**dict[str, int]"; expected "str"
217 p1(**k) # E: Argument 1 to "foo" has incompatible type "**dict[str, int]"; expected "str"
218 p1(*a) # E: Expected iterable as variadic argument
219
220
221 def baz(a: int, b: int) -> int: ...
222 def test_baz(xs: List[int]):
223 p3 = functools.partial(baz, *xs)
224 p3()
225 p3(1) # E: Too many arguments for "baz"
226
227
228 [builtins fixtures/dict.pyi]
229
230 [case testFunctoolsPartialGeneric]
231 from typing import TypeVar
232 import functools
233
234 T = TypeVar("T")
235 U = TypeVar("U")
236
237 def foo(a: T, b: T) -> T: ...
238
239 p1 = functools.partial(foo, 1)
240 reveal_type(p1(2)) # N: Revealed type is "builtins.int"
241 p1("a") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
242
243 p2 = functools.partial(foo, "a")
244 p2(1) # E: Argument 1 to "foo" has incompatible type "int"; expected "str"
245 reveal_type(p2("a")) # N: Revealed type is "builtins.str"
246
247 def bar(a: T, b: U) -> U: ...
248
249 p3 = functools.partial(bar, 1)
250 reveal_type(p3(2)) # N: Revealed type is "builtins.int"
251 reveal_type(p3("a")) # N: Revealed type is "builtins.str"
252 [builtins fixtures/dict.pyi]
253
254 [case testFunctoolsPartialCallable]
255 from typing import Callable
256 import functools
257
258 def main1(f: Callable[[int, str], int]) -> None:
259 p = functools.partial(f, 1)
260 p("a") # OK
261 p(1) # E: Argument 1 has incompatible type "int"; expected "str"
262
263 functools.partial(f, a=1) # E: Unexpected keyword argument "a"
264
265 class CallbackProto:
266 def __call__(self, a: int, b: str) -> int: ...
267
268 def main2(f: CallbackProto) -> None:
269 p = functools.partial(f, b="a")
270 p(1) # OK
271 p("a") # E: Argument 1 to "__call__" of "CallbackProto" has incompatible type "str"; expected "int"
272 [builtins fixtures/dict.pyi]
273
274 [case testFunctoolsPartialOverload]
275 from typing import overload
276 import functools
277
278 @overload
279 def foo(a: int, b: str) -> int: ...
280 @overload
281 def foo(a: str, b: int) -> str: ...
282 def foo(*a, **k): ...
283
284 p1 = functools.partial(foo)
285 reveal_type(p1(1, "a")) # N: Revealed type is "builtins.int"
286 reveal_type(p1("a", 1)) # N: Revealed type is "builtins.int"
287 p1(1, 2) # TODO: false negative
288 p1("a", "b") # TODO: false negative
289 [builtins fixtures/dict.pyi]
290
291 [case testFunctoolsPartialTypeGuard]
292 import functools
293 from typing_extensions import TypeGuard
294
295 def is_str_list(val: list[object]) -> TypeGuard[list[str]]: ...
296
297 reveal_type(functools.partial(is_str_list, [1, 2, 3])) # N: Revealed type is "functools.partial[builtins.bool]"
298 reveal_type(functools.partial(is_str_list, [1, 2, 3])()) # N: Revealed type is "builtins.bool"
299 [builtins fixtures/dict.pyi]
300
301 [case testFunctoolsPartialType]
302 import functools
303 from typing import Type
304
305 class A:
306 def __init__(self, a: int, b: str) -> None: ...
307
308 p = functools.partial(A, 1)
309 reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"
310
311 p("a") # OK
312 p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
313 p(z=1) # E: Unexpected keyword argument "z" for "A"
314
315 def main(t: Type[A]) -> None:
316 p = functools.partial(t, 1)
317 reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"
318
319 p("a") # OK
320 p(1) # E: Argument 1 to "A" has incompatible type "int"; expected "str"
321 p(z=1) # E: Unexpected keyword argument "z" for "A"
322
323 [builtins fixtures/dict.pyi]
324
325 [case testFunctoolsPartialTypeVarTuple]
326 import functools
327 import typing
328 Ts = typing.TypeVarTuple("Ts")
329 def foo(fn: typing.Callable[[typing.Unpack[Ts]], None], /, *arg: typing.Unpack[Ts], kwarg: str) -> None: ...
330 p = functools.partial(foo, kwarg="asdf")
331
332 def bar(a: int, b: str, c: float) -> None: ...
333 p(bar, 1, "a", 3.0) # OK
334 p(bar, 1, "a", 3.0, kwarg="asdf") # OK
335 p(bar, 1, "a", "b") # E: Argument 1 to "foo" has incompatible type "Callable[[int, str, float], None]"; expected "Callable[[int, str, str], None]"
336 [builtins fixtures/dict.pyi]
337
338 [case testFunctoolsPartialUnion]
339 import functools
340 from typing import Any, Callable, Union
341
342 cls1: Any
343 cls2: Union[Any, Any]
344 reveal_type(functools.partial(cls1, 2)()) # N: Revealed type is "Any"
345 reveal_type(functools.partial(cls2, 2)()) # N: Revealed type is "Any"
346
347 fn1: Union[Callable[[int], int], Callable[[int], int]]
348 reveal_type(functools.partial(fn1, 2)()) # N: Revealed type is "builtins.int"
349
350 fn2: Union[Callable[[int], int], Callable[[int], str]]
351 reveal_type(functools.partial(fn2, 2)()) # N: Revealed type is "builtins.int | builtins.str"
352
353 fn3: Union[Callable[[int], int], str]
354 reveal_type(functools.partial(fn3, 2)()) # E: "str" not callable \
355 # N: Revealed type is "builtins.int" \
356 # E: Argument 1 to "partial" has incompatible type "Callable[[int], int] | str"; expected "Callable[..., int]"
357 [builtins fixtures/tuple.pyi]
358
359 [case testFunctoolsPartialUnionOfTypeAndCallable]
360 import functools
361 from typing import Callable, Union, Type
362 from typing_extensions import TypeAlias
363
364 class FooBar:
365 def __init__(self, arg1: str) -> None:
366 pass
367
368 def f1(t: Union[Type[FooBar], Callable[..., 'FooBar']]) -> None:
369 val = functools.partial(t)
370
371 FooBarFunc: TypeAlias = Callable[..., 'FooBar']
372
373 def f2(t: Union[Type[FooBar], FooBarFunc]) -> None:
374 val = functools.partial(t)
375 [builtins fixtures/tuple.pyi]
376
377 [case testFunctoolsPartialExplicitType]
378 from functools import partial
379 from typing import Type, TypeVar, Callable
380
381 T = TypeVar("T")
382 def generic(string: str, integer: int, resulting_type: Type[T]) -> T: ...
383
384 p: partial[str] = partial(generic, resulting_type=str)
385 q: partial[bool] = partial(generic, resulting_type=str) # E: Argument "resulting_type" to "generic" has incompatible type "type[str]"; expected "type[bool]"
386
387 pc: Callable[..., str] = partial(generic, resulting_type=str)
388 qc: Callable[..., bool] = partial(generic, resulting_type=str) # E: Incompatible types in assignment (expression has type "partial[str]", variable has type "Callable[..., bool]") \
389 # N: "partial[str].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> str"
390 [builtins fixtures/tuple.pyi]
391
392 [case testFunctoolsPartialNestedPartial]
393 from functools import partial
394 from typing import Any
395
396 def foo(x: int) -> int: ...
397 p = partial(partial, foo)
398 reveal_type(p()(1)) # N: Revealed type is "builtins.int"
399 p()("no") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
400
401 q = partial(partial, partial, foo)
402 q()()("no") # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
403
404 r = partial(partial, foo, 1)
405 reveal_type(r()()) # N: Revealed type is "builtins.int"
406 [builtins fixtures/tuple.pyi]
407
408 [case testFunctoolsPartialTypeObject]
409 import functools
410 from typing import Type, Generic, TypeVar
411
412 class A:
413 def __init__(self, val: int) -> None: ...
414
415 cls1: Type[A]
416 reveal_type(functools.partial(cls1, 2)()) # N: Revealed type is "__main__.A"
417 functools.partial(cls1, "asdf") # E: Argument 1 to "A" has incompatible type "str"; expected "int"
418
419 T = TypeVar("T")
420 class B(Generic[T]):
421 def __init__(self, val: T) -> None: ...
422
423 cls2: Type[B[int]]
424 reveal_type(functools.partial(cls2, 2)()) # N: Revealed type is "__main__.B[builtins.int]"
425 functools.partial(cls2, "asdf") # E: Argument 1 to "B" has incompatible type "str"; expected "int"
426
427 def foo(cls3: Type[B[T]]):
428 reveal_type(functools.partial(cls3, "asdf")) # N: Revealed type is "functools.partial[__main__.B[T`-1]]" \
429 # E: Argument 1 to "B" has incompatible type "str"; expected "T"
430 reveal_type(functools.partial(cls3, 2)()) # N: Revealed type is "__main__.B[T`-1]" \
431 # E: Argument 1 to "B" has incompatible type "int"; expected "T"
432 [builtins fixtures/tuple.pyi]
433
434 [case testFunctoolsPartialTypedDictUnpack]
435 from typing import TypedDict
436 from typing_extensions import Unpack
437 from functools import partial
438
439 class D1(TypedDict, total=False):
440 a1: int
441
442 def fn1(a1: int) -> None: ...
443 def main1(**d1: Unpack[D1]) -> None:
444 partial(fn1, **d1)()
445 partial(fn1, **d1)(**d1)
446 partial(fn1, **d1)(a1=1)
447 partial(fn1, **d1)(a1="asdf") # E: Argument "a1" to "fn1" has incompatible type "str"; expected "int"
448 partial(fn1, **d1)(oops=1) # E: Unexpected keyword argument "oops" for "fn1"
449
450 def fn2(**kwargs: Unpack[D1]) -> None: ...
451 def main2(**d1: Unpack[D1]) -> None:
452 partial(fn2, **d1)()
453 partial(fn2, **d1)(**d1)
454 partial(fn2, **d1)(a1=1)
455 partial(fn2, **d1)(a1="asdf") # E: Argument "a1" to "fn2" has incompatible type "str"; expected "int"
456 partial(fn2, **d1)(oops=1) # E: Unexpected keyword argument "oops" for "fn2"
457
458 class D2(TypedDict, total=False):
459 a1: int
460 a2: str
461
462 class A2Good(TypedDict, total=False):
463 a2: str
464 class A2Bad(TypedDict, total=False):
465 a2: int
466
467 def fn3(a1: int, a2: str) -> None: ...
468 def main3(a2good: A2Good, a2bad: A2Bad, **d2: Unpack[D2]) -> None:
469 partial(fn3, **d2)()
470 partial(fn3, **d2)(a1=1, a2="asdf")
471
472 partial(fn3, **d2)(**d2)
473
474 partial(fn3, **d2)(a1="asdf") # E: Argument "a1" to "fn3" has incompatible type "str"; expected "int"
475 partial(fn3, **d2)(a1=1, a2="asdf", oops=1) # E: Unexpected keyword argument "oops" for "fn3"
476
477 partial(fn3, **d2)(**a2good)
478 partial(fn3, **d2)(**a2bad) # E: Argument "a2" to "fn3" has incompatible type "int"; expected "str"
479
480 def fn4(**kwargs: Unpack[D2]) -> None: ...
481 def main4(a2good: A2Good, a2bad: A2Bad, **d2: Unpack[D2]) -> None:
482 partial(fn4, **d2)()
483 partial(fn4, **d2)(a1=1, a2="asdf")
484
485 partial(fn4, **d2)(**d2)
486
487 partial(fn4, **d2)(a1="asdf") # E: Argument "a1" to "fn4" has incompatible type "str"; expected "int"
488 partial(fn4, **d2)(a1=1, a2="asdf", oops=1) # E: Unexpected keyword argument "oops" for "fn4"
489
490 partial(fn3, **d2)(**a2good)
491 partial(fn3, **d2)(**a2bad) # E: Argument "a2" to "fn3" has incompatible type "int"; expected "str"
492
493 def main5(**d2: Unpack[D2]) -> None:
494 partial(fn1, **d2)() # E: Extra argument "a2" from **args for "fn1"
495 partial(fn2, **d2)() # E: Extra argument "a2" from **args for "fn2"
496
497 def main6(a2good: A2Good, a2bad: A2Bad, **d1: Unpack[D1]) -> None:
498 partial(fn3, **d1)() # E: Missing positional argument "a1" in call to "fn3"
499 partial(fn3, **d1)("asdf") # E: Too many positional arguments for "fn3" \
500 # E: Too few arguments for "fn3" \
501 # E: Argument 1 to "fn3" has incompatible type "str"; expected "int"
502 partial(fn3, **d1)(a2="asdf")
503 partial(fn3, **d1)(**a2good)
504 partial(fn3, **d1)(**a2bad) # E: Argument "a2" to "fn3" has incompatible type "int"; expected "str"
505
506 partial(fn4, **d1)()
507 partial(fn4, **d1)("asdf") # E: Too many positional arguments for "fn4" \
508 # E: Argument 1 to "fn4" has incompatible type "str"; expected "int"
509 partial(fn4, **d1)(a2="asdf")
510 partial(fn4, **d1)(**a2good)
511 partial(fn4, **d1)(**a2bad) # E: Argument "a2" to "fn4" has incompatible type "int"; expected "str"
512 [builtins fixtures/dict.pyi]
513 [typing fixtures/typing-typeddict.pyi]
514
515
516 [case testFunctoolsPartialNestedGeneric]
517 from functools import partial
518 from typing import Generic, TypeVar, List
519
520 T = TypeVar("T")
521 def get(n: int, args: List[T]) -> T: ...
522 first = partial(get, 0)
523
524 x: List[str]
525 reveal_type(first(x)) # N: Revealed type is "builtins.str"
526 reveal_type(first([1])) # N: Revealed type is "builtins.int"
527
528 first_kw = partial(get, n=0)
529 reveal_type(first_kw(args=[1])) # N: Revealed type is "builtins.int"
530
531 # TODO: this is indeed invalid, but the error is incomprehensible.
532 first_kw([1]) # E: Too many positional arguments for "get" \
533 # E: Too few arguments for "get" \
534 # E: Argument 1 to "get" has incompatible type "list[int]"; expected "int"
535 [builtins fixtures/list.pyi]
536
537 [case testFunctoolsPartialHigherOrder]
538 from functools import partial
539 from typing import Callable
540
541 def fn(a: int, b: str, c: bytes) -> int: ...
542
543 def callback1(fn: Callable[[str, bytes], int]) -> None: ...
544 def callback2(fn: Callable[[str, int], int]) -> None: ...
545
546 callback1(partial(fn, 1))
547 # TODO: false negative
548 # https://github.com/python/mypy/issues/17461
549 callback2(partial(fn, 1))
550 [builtins fixtures/tuple.pyi]
551
552 [case testFunctoolsPartialClassObjectMatchingPartial]
553 from functools import partial
554
555 class A:
556 def __init__(self, var: int, b: int, c: int) -> None: ...
557
558 p = partial(A, 1)
559 reveal_type(p) # N: Revealed type is "functools.partial[__main__.A]"
560 p(1, "no") # E: Argument 2 to "A" has incompatible type "str"; expected "int"
561
562 q: partial[A] = partial(A, 1) # OK
563 [builtins fixtures/tuple.pyi]
564
565 [case testFunctoolsPartialTypeVarBound]
566 from typing import Callable, TypeVar, Type
567 import functools
568
569 T = TypeVar("T", bound=Callable[[str, int], str])
570 S = TypeVar("S", bound=Type[int])
571
572 def foo(f: T) -> T:
573 g = functools.partial(f, "foo")
574 return f
575
576 def bar(f: S) -> S:
577 g = functools.partial(f, "foo")
578 return f
579 [builtins fixtures/primitives.pyi]
580
581 [case testFunctoolsPartialAbstractType]
582 from abc import ABC, abstractmethod
583 from functools import partial
584
585 class A(ABC):
586 def __init__(self) -> None: ...
587 @abstractmethod
588 def method(self) -> None: ...
589
590 def f1(cls: type[A]) -> None:
591 cls()
592 partial_cls = partial(cls)
593 partial_cls()
594
595 def f2() -> None:
596 A() # E: Cannot instantiate abstract class "A" with abstract attribute "method"
597 partial_cls = partial(A) # E: Cannot instantiate abstract class "A" with abstract attribute "method"
598 partial_cls() # E: Cannot instantiate abstract class "A" with abstract attribute "method"
599 [builtins fixtures/tuple.pyi]
600
601 [case testFunctoolsPartialSelfType]
602 from functools import partial
603 from typing_extensions import Self
604
605 class A:
606 def __init__(self, ts: float, msg: str) -> None: ...
607
608 @classmethod
609 def from_msg(cls, msg: str) -> Self:
610 factory = partial(cls, ts=0)
611 return factory(msg=msg)
612 [builtins fixtures/tuple.pyi]
613
614 [case testFunctoolsPartialTypeVarValues]
615 from functools import partial
616 from typing import TypeVar
617
618 T = TypeVar("T", int, str)
619
620 def f(x: int, y: T) -> T:
621 return y
622
623 def g(x: T, y: int) -> T:
624 return x
625
626 def h(x: T, y: T) -> T:
627 return x
628
629 fp = partial(f, 1)
630 reveal_type(fp(1)) # N: Revealed type is "builtins.int"
631 reveal_type(fp("a")) # N: Revealed type is "builtins.str"
632 fp(object()) # E: Value of type variable "T" of "f" cannot be "object"
633
634 gp = partial(g, 1)
635 reveal_type(gp(1)) # N: Revealed type is "builtins.int"
636 gp("a") # E: Argument 1 to "g" has incompatible type "str"; expected "int"
637
638 hp = partial(h, 1)
639 reveal_type(hp(1)) # N: Revealed type is "builtins.int"
640 hp("a") # E: Argument 1 to "h" has incompatible type "str"; expected "int"
641 [builtins fixtures/tuple.pyi]
642
643 [case testFunctoolsPartialOverloadedCallableProtocol]
644 from functools import partial
645 from typing import Callable, Protocol, overload
646
647 class P(Protocol):
648 @overload
649 def __call__(self, x: int) -> int: ...
650 @overload
651 def __call__(self, x: str) -> str: ...
652
653 def f(x: P):
654 reveal_type(partial(x, 1)()) # N: Revealed type is "builtins.int"
655
656 # TODO: but this is incorrect, predating the functools.partial plugin
657 reveal_type(partial(x, "a")()) # N: Revealed type is "builtins.int"
658 [builtins fixtures/tuple.pyi]
659
660 [case testFunctoolsPartialTypeVarErasure]
661 from typing import Callable, TypeVar, Union
662 from typing_extensions import ParamSpec, TypeVarTuple, Unpack
663 from functools import partial
664
665 def use_int_callable(x: Callable[[int], int]) -> None:
666 pass
667 def use_func_callable(
668 x: Callable[
669 [Callable[[int], None]],
670 Callable[[int], None],
671 ],
672 ) -> None:
673 pass
674
675 Tc = TypeVar("Tc", int, str)
676 Tb = TypeVar("Tb", bound=Union[int, str])
677 P = ParamSpec("P")
678 Ts = TypeVarTuple("Ts")
679
680 def func_b(a: Tb, b: str) -> Tb:
681 return a
682 def func_c(a: Tc, b: str) -> Tc:
683 return a
684
685 def func_fn(fn: Callable[P, Tc], b: str) -> Callable[P, Tc]:
686 return fn
687 def func_fn_unpack(fn: Callable[[Unpack[Ts]], Tc], b: str) -> Callable[[Unpack[Ts]], Tc]:
688 return fn
689
690 # We should not leak stray typevars that aren't in scope:
691 reveal_type(partial(func_b, b="")) # N: Revealed type is "functools.partial[Any]"
692 reveal_type(partial(func_c, b="")) # N: Revealed type is "functools.partial[Any]"
693 reveal_type(partial(func_fn, b="")) # N: Revealed type is "functools.partial[def (*Any, **Any) -> Any]"
694 reveal_type(partial(func_fn_unpack, b="")) # N: Revealed type is "functools.partial[def (*Any) -> Any]"
695
696 use_int_callable(partial(func_b, b=""))
697 use_func_callable(partial(func_b, b=""))
698 use_int_callable(partial(func_c, b=""))
699 use_func_callable(partial(func_c, b=""))
700 use_int_callable(partial(func_fn, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[def (*Any, **Any) -> Any]"; expected "Callable[[int], int]" \
701 # N: "partial[def (*Any, **Any) -> Any].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> def (*Any, **Any) -> Any"
702 use_func_callable(partial(func_fn, b=""))
703 use_int_callable(partial(func_fn_unpack, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[def (*Any) -> Any]"; expected "Callable[[int], int]" \
704 # N: "partial[def (*Any) -> Any].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> def (*Any) -> Any"
705 use_func_callable(partial(func_fn_unpack, b=""))
706
707 # But we should not erase typevars that aren't bound by function
708 # passed to `partial`:
709
710 def outer_b(arg: Tb) -> None:
711
712 def inner(a: Tb, b: str) -> Tb:
713 return a
714
715 reveal_type(partial(inner, b="")) # N: Revealed type is "functools.partial[Tb`-1]"
716 use_int_callable(partial(inner, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[Tb]"; expected "Callable[[int], int]" \
717 # N: "partial[Tb].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> Tb"
718
719 def outer_c(arg: Tc) -> None:
720
721 def inner(a: Tc, b: str) -> Tc:
722 return a
723
724 reveal_type(partial(inner, b="")) # N: Revealed type is "functools.partial[builtins.int]" \
725 # N: Revealed type is "functools.partial[builtins.str]"
726 use_int_callable(partial(inner, b="")) # E: Argument 1 to "use_int_callable" has incompatible type "partial[str]"; expected "Callable[[int], int]" \
727 # N: "partial[str].__call__" has type "def __call__(__self, *args: Any, **kwargs: Any) -> str"
728 [builtins fixtures/tuple.pyi]