Debian Codegraph @

test-data/unit/check-callable.test

raw
1 [case testCallableDef]
2 def f() -> None: pass
3
4 if callable(f):
5 f()
6 else:
7 f += 5
8
9 [builtins fixtures/callable.pyi]
10
11 [case testCallableLambda]
12 f = lambda: None
13
14 if callable(f):
15 f()
16 else:
17 f += 5
18
19 [builtins fixtures/callable.pyi]
20
21 [case testCallableNotCallable]
22 x = 5
23
24 if callable(x):
25 x()
26 else:
27 x += 5
28
29 [builtins fixtures/callable.pyi]
30
31 [case testUnion]
32 from typing import Callable, Union
33
34 x = 5 # type: Union[int, Callable[[], str]]
35
36 if callable(x):
37 y = x() + 'test'
38 else:
39 z = x + 6
40
41 [builtins fixtures/callable.pyi]
42
43 [case testUnionMultipleReturnTypes]
44 from typing import Callable, Union
45
46 x = 5 # type: Union[int, Callable[[], str], Callable[[], int]]
47
48 if callable(x):
49 y = x() + 2 # E: Unsupported operand types for + ("str" and "int") \
50 # N: Left operand is of type "str | int"
51 else:
52 z = x + 6
53
54 [builtins fixtures/callable.pyi]
55
56 [case testUnionMultipleNonCallableTypes]
57 from typing import Callable, Union
58
59 x = 5 # type: Union[int, str, Callable[[], str]]
60
61 if callable(x):
62 y = x() + 'test'
63 else:
64 z = x + 6 # E: Unsupported operand types for + ("str" and "int") \
65 # N: Left operand is of type "int | str"
66
67 [builtins fixtures/callable.pyi]
68
69 [case testCallableThenIsinstance]
70 from typing import Callable, Union
71
72 x = 5 # type: Union[int, str, Callable[[], str], Callable[[], int]]
73
74 if callable(x):
75 y = x()
76 if isinstance(y, int):
77 b1 = y + 2
78 else:
79 b2 = y + 'test'
80 else:
81 if isinstance(x, int):
82 b3 = x + 3
83 else:
84 b4 = x + 'test2'
85
86 [builtins fixtures/callable.pyi]
87
88 [case testIsinstanceThenCallable]
89 from typing import Callable, Union
90
91 x = 5 # type: Union[int, str, Callable[[], str], Callable[[], int]]
92
93 if isinstance(x, int):
94 b1 = x + 1
95 else:
96 if callable(x):
97 y = x()
98 if isinstance(y, int):
99 b2 = y + 1
100 else:
101 b3 = y + 'test'
102 else:
103 b4 = x + 'test2'
104
105 [builtins fixtures/callable.pyi]
106
107 [case testCallableWithDifferentArgTypes]
108 from typing import Callable, Union
109
110 x = 5 # type: Union[int, Callable[[], None], Callable[[int], None]]
111
112 if callable(x):
113 x() # E: Too few arguments
114
115 [builtins fixtures/callable.pyi]
116
117 [case testClassInitializer]
118 from typing import Callable, Union
119
120 class A:
121 x = 5
122
123 a = A # type: Union[A, Callable[[], A]]
124
125 if callable(a):
126 a = a()
127
128 a.x + 6
129
130 [builtins fixtures/callable.pyi]
131
132 [case testCallableVariables]
133 from typing import Union
134
135 class A:
136 x = 5
137
138 class B:
139 x = int
140
141 x = A() # type: Union[A, B]
142
143 if callable(x.x):
144 y = x.x()
145 else:
146 y = x.x + 5
147
148 [builtins fixtures/callable.pyi]
149
150 [case testCallableAnd]
151 from typing import Union, Callable
152
153 x = 5 # type: Union[int, Callable[[], str]]
154
155 if callable(x) and x() == 'test':
156 x()
157 else:
158 x + 5 # E: Unsupported left operand type for + ("Callable[[], str]") \
159 # N: Left operand is of type "int | Callable[[], str]"
160
161 [builtins fixtures/callable.pyi]
162
163 [case testCallableOr]
164 from typing import Union, Callable
165
166 x = 5 # type: Union[int, Callable[[], str]]
167
168 if callable(x) or x() == 'test': # E: "int" not callable
169 x() # E: "int" not callable
170 else:
171 x + 5
172 [builtins fixtures/callable.pyi]
173
174 [case testCallableOrOtherType]
175 from typing import Union, Callable
176
177 x = 5 # type: Union[int, Callable[[], str]]
178
179 if callable(x) or x == 2:
180 pass
181 else:
182 pass
183 [builtins fixtures/callable.pyi]
184
185 [case testAnyCallable]
186 from typing import Any
187
188 x = 5 # type: Any
189
190 if callable(x):
191 reveal_type(x) # N: Revealed type is "Any"
192 else:
193 reveal_type(x) # N: Revealed type is "Any"
194 [builtins fixtures/callable.pyi]
195
196 [case testCallableCallableClasses]
197 from typing import Union
198
199
200 class A:
201 pass
202
203
204 class B:
205 def __call__(self) -> None:
206 pass
207
208
209 a = A() # type: A
210 b = B() # type: B
211 c = A() # type: Union[A, B]
212
213 if callable(a):
214 5 + 'test' # E: Unsupported operand types for + ("int" and "str")
215
216 if not callable(b):
217 5 + 'test'
218
219 if callable(c):
220 reveal_type(c) # N: Revealed type is "__main__.B"
221 else:
222 reveal_type(c) # N: Revealed type is "__main__.A"
223
224 [builtins fixtures/callable.pyi]
225
226 [case testDecoratedCallMethods]
227 from typing import Any, Callable, Union, TypeVar
228
229 F = TypeVar('F', bound=Callable)
230
231 def decorator(f: F) -> F:
232 pass
233 def change(f: Callable) -> Callable[[Any], str]:
234 pass
235 def untyped(f):
236 pass
237
238 class Some1:
239 @decorator
240 def __call__(self) -> int:
241 pass
242 class Some2:
243 @change
244 def __call__(self) -> int:
245 pass
246 class Some3:
247 @untyped
248 def __call__(self) -> int:
249 pass
250 class Some4:
251 __call__: Any
252
253 s1: Some1
254 s2: Some2
255 s3: Some3
256 s4: Some4
257
258 if callable(s1):
259 1 + 'a' # E: Unsupported operand types for + ("int" and "str")
260 else:
261 2 + 'b'
262 if callable(s2):
263 1 + 'a' # E: Unsupported operand types for + ("int" and "str")
264 else:
265 2 + 'b'
266 if callable(s3):
267 1 + 'a' # E: Unsupported operand types for + ("int" and "str")
268 else:
269 2 + 'b' # E: Unsupported operand types for + ("int" and "str")
270 if callable(s4):
271 1 + 'a' # E: Unsupported operand types for + ("int" and "str")
272 else:
273 2 + 'b' # E: Unsupported operand types for + ("int" and "str")
274 [builtins fixtures/callable.pyi]
275
276 [case testCallableNestedUnions]
277 from typing import Callable, Union
278
279 T = Union[Union[int, Callable[[], int]], Union[str, Callable[[], str]]]
280
281 def f(t: T) -> None:
282 if callable(t):
283 reveal_type(t()) # N: Revealed type is "builtins.int | builtins.str"
284 else:
285 reveal_type(t) # N: Revealed type is "builtins.int | builtins.str"
286
287 [builtins fixtures/callable.pyi]
288
289 [case testCallableTypeVarEmpty]
290 from typing import TypeVar
291
292 T = TypeVar('T')
293
294 def f(t: T) -> T:
295 if callable(t):
296 return 5 # E: Incompatible return value type (got "int", expected "T")
297 else:
298 return t
299
300 [builtins fixtures/callable.pyi]
301
302 [case testCallableTypeVarUnion]
303 from typing import Callable, TypeVar, Union
304
305 T = TypeVar('T', int, Callable[[], int], Union[str, Callable[[], str]])
306
307 def f(t: T) -> None:
308 if callable(t):
309 reveal_type(t()) # N: Revealed type is "Any" \
310 # N: Revealed type is "builtins.int" \
311 # N: Revealed type is "builtins.str"
312 else:
313 reveal_type(t) # N: Revealed type is "builtins.int" # N: Revealed type is "builtins.str"
314
315 [builtins fixtures/callable.pyi]
316
317 [case testCallableTypeVarBound]
318 from typing import TypeVar
319
320
321 class A:
322 def __call__(self) -> str:
323 return 'hi'
324
325
326 T = TypeVar('T', bound=A)
327
328 def f(t: T) -> str:
329 if callable(t):
330 return t()
331 else:
332 return 5
333
334 [builtins fixtures/callable.pyi]
335
336 [case testCallableTypeType]
337 from typing import Type
338
339
340 class A:
341 pass
342
343
344 T = Type[A]
345
346 def f(t: T) -> A:
347 if callable(t):
348 return t()
349 else:
350 return 5
351
352 [builtins fixtures/callable.pyi]
353
354 [case testCallableTypeUnion]
355 from abc import ABCMeta, abstractmethod
356 from typing import Type, Union
357
358
359 class A(metaclass=ABCMeta):
360 @abstractmethod
361 def f(self) -> None:
362 pass
363
364
365 class B:
366 pass
367
368
369 x = B # type: Union[Type[A], Type[B]]
370 if callable(x):
371 # Abstract classes raise an error when called, but are indeed `callable`
372 pass
373 else:
374 'test' + 5
375
376 [builtins fixtures/callable.pyi]
377
378 [case testCallableUnionOfTypes]
379 from abc import ABCMeta, abstractmethod
380 from typing import Type, Union
381
382
383 class A(metaclass=ABCMeta):
384 @abstractmethod
385 def f(self) -> None:
386 pass
387
388
389 class B:
390 pass
391
392
393 x = B # type: Type[Union[A, B]]
394 if callable(x):
395 # Abstract classes raise an error when called, but are indeed `callable`
396 pass
397 else:
398 'test' + 5
399
400 [builtins fixtures/callable.pyi]
401
402 [case testCallableObject]
403
404 def f(o: object) -> None:
405 if callable(o):
406 o(1,2,3)
407 1 + 'boom' # E: Unsupported operand types for + ("int" and "str")
408 o('hi') + 12
409 reveal_type(o) # N: Revealed type is "__main__.<callable subtype of object>"
410
411 [builtins fixtures/callable.pyi]
412
413 [case testCallableObject2]
414
415 class Foo(object):
416 def bar(self) -> None:
417 pass
418
419 def g(o: Foo) -> None:
420 o.bar()
421 if callable(o):
422 o.foo() # E: "Foo" has no attribute "foo"
423 o.bar()
424 o(1,2,3)
425 else:
426 o.bar()
427
428 [builtins fixtures/callable.pyi]
429
430 [case testCallableObjectAny]
431
432 from typing import Any
433
434 class Foo(Any):
435 def bar(self) -> None:
436 pass
437
438 def g(o: Foo) -> None:
439 o.bar()
440 o.baz()
441 if callable(o):
442 o('test')
443 o.lurr(1,2,3)
444
445 [builtins fixtures/callable.pyi]
446
447 [case testCallableObjectGeneric]
448
449 from typing import TypeVar, Generic
450
451 T = TypeVar('T')
452 class Test(Generic[T]):
453 def __self__(self, x: T) -> None:
454 self.x = x
455
456 def g(o: Test[T], x: T) -> T:
457 if callable(o):
458 o.foo() # E: "Test[T]" has no attribute "foo"
459 o(1,2,3)
460 o.x = x
461 o.x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "T")
462 1 + o.x # E: Unsupported operand types for + ("int" and "T")
463 return o.x
464
465 return x
466
467 [builtins fixtures/callable.pyi]
468
469 [case testCallablePromote]
470
471 def take_float(f: float) -> None:
472 pass
473
474 def g(o: int) -> None:
475 if callable(o):
476 take_float(o)
477 o(1,2,3)
478
479 [builtins fixtures/callable.pyi]
480
481 [case testCallableTuple]
482
483 from typing import NamedTuple
484
485 Thing = NamedTuple('Thing', [('s', str), ('n', int)])
486
487 def g(o: Thing) -> None:
488 if callable(o):
489 o.s + o.n # E: Unsupported operand types for + ("str" and "int")
490 i, s = o
491 i + s # E: Unsupported operand types for + ("str" and "int")
492 o(1,2,3)
493
494 [builtins fixtures/callable.pyi]
495
496 [case testCallableNoArgs]
497
498 if callable(): # E: Missing positional argument "x" in call to "callable"
499 pass
500
501 [builtins fixtures/callable.pyi]
502
503 [case testCallableWithAnyArgs]
504
505 def main(fn: object) -> None:
506 if callable(fn):
507 fn()
508 fn(1, 2, 3)
509 fn(take="that")
510
511 [builtins fixtures/callable.pyi]
512
513 [case testCallableUnionOfNoneAndCallable]
514
515 from typing import Union, Callable
516
517 def f() -> int:
518 return 42
519
520 fn = f # type: Union[None, Callable[[], int]]
521
522 if callable(fn):
523 reveal_type(fn) # N: Revealed type is "def () -> builtins.int"
524 else:
525 reveal_type(fn) # N: Revealed type is "None"
526
527 [builtins fixtures/callable.pyi]
528
529 [case testBuiltinsTypeAsCallable]
530 from __future__ import annotations
531
532 reveal_type(type) # N: Revealed type is "def (x: Any) -> builtins.type"
533 _TYPE = type
534 reveal_type(_TYPE) # N: Revealed type is "def (x: Any) -> builtins.type"
535 _TYPE('bar')
536
537 [builtins fixtures/callable.pyi]
538
539 [case testErrorMessageAboutSelf]
540 # https://github.com/python/mypy/issues/11309
541 class Some:
542 def method(self, a) -> None: pass
543 @classmethod
544 def cls_method(cls, a) -> None: pass
545 @staticmethod
546 def st_method(a) -> None: pass
547
548 def bad_method(a) -> None: pass
549 @classmethod
550 def bad_cls_method(a) -> None: pass
551 @staticmethod
552 def bad_st_method() -> None: pass
553
554 s: Some
555
556 s.method(1)
557 s.cls_method(1)
558 Some.cls_method(1)
559 s.st_method(1)
560 Some.st_method(1)
561
562 s.method(1, 2) # E: Too many arguments for "method" of "Some"
563 s.cls_method(1, 2) # E: Too many arguments for "cls_method" of "Some"
564 Some.cls_method(1, 2) # E: Too many arguments for "cls_method" of "Some"
565 s.st_method(1, 2) # E: Too many arguments for "st_method" of "Some"
566 Some.st_method(1, 2) # E: Too many arguments for "st_method" of "Some"
567
568 s.bad_method(1) # E: Too many arguments for "bad_method" of "Some" \
569 # N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
570 s.bad_cls_method(1) # E: Too many arguments for "bad_cls_method" of "Some" \
571 # N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
572 Some.bad_cls_method(1) # E: Too many arguments for "bad_cls_method" of "Some" \
573 # N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
574 s.bad_st_method(1) # E: Too many arguments for "bad_st_method" of "Some"
575 Some.bad_st_method(1) # E: Too many arguments for "bad_st_method" of "Some"
576 [builtins fixtures/callable.pyi]
577
578 [case testClassMethodAliasStub]
579 from a import f
580 f("no") # E: Argument 1 has incompatible type "str"; expected "int"
581 [file a.pyi]
582 from b import C
583 f = C.f
584 [file b.pyi]
585 import a
586 class C(B):
587 @classmethod
588 def f(self, x: int) -> C: ...
589 class B: ...
590 [builtins fixtures/classmethod.pyi]
591
592 [case testClassMethodAliasInClass]
593 from typing import overload
594
595 class C:
596 @classmethod
597 def foo(cls) -> int: ...
598
599 bar = foo
600
601 @overload
602 @classmethod
603 def foo2(cls, x: int) -> int: ...
604 @overload
605 @classmethod
606 def foo2(cls, x: str) -> str: ...
607 @classmethod
608 def foo2(cls, x):
609 ...
610
611 bar2 = foo2
612
613 reveal_type(C.bar) # N: Revealed type is "def () -> builtins.int"
614 reveal_type(C().bar) # N: Revealed type is "def () -> builtins.int"
615 reveal_type(C.bar2) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
616 reveal_type(C().bar2) # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
617 [builtins fixtures/classmethod.pyi]
618
619 [case testPropertyAliasInClassBody]
620 class A:
621 @property
622 def f(self) -> int: ...
623
624 g = f
625
626 @property
627 def f2(self) -> int: ...
628 @f2.setter
629 def f2(self, val: int) -> None: ...
630
631 g2 = f2
632
633 reveal_type(A().g) # N: Revealed type is "builtins.int"
634 reveal_type(A().g2) # N: Revealed type is "builtins.int"
635 A().g = 1 # E: Property "g" defined in "A" is read-only
636 A().g2 = 1
637 A().g2 = "no" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
638 [builtins fixtures/property.pyi]
639
640 [case testCallableUnionCallback]
641 from typing import Union, Callable, TypeVar
642
643 TA = TypeVar("TA", bound="A")
644 class A:
645 def __call__(self: TA, other: Union[Callable, TA]) -> TA: ...
646 a: A
647 a() # E: Missing positional argument "other" in call to "__call__" of "A"
648 a(a)
649 a(lambda: None)
650
651 [case testCallableSubtypingTrivialSuffix]
652 from typing import Any, Protocol
653
654 class Call(Protocol):
655 def __call__(self, x: int, *args: Any, **kwargs: Any) -> None: ...
656
657 def f1() -> None: ...
658 a1: Call = f1 # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "Call") \
659 # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None"
660 def f2(x: str) -> None: ...
661 a2: Call = f2 # E: Incompatible types in assignment (expression has type "Callable[[str], None]", variable has type "Call") \
662 # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None"
663 def f3(y: int) -> None: ...
664 a3: Call = f3 # E: Incompatible types in assignment (expression has type "Callable[[int], None]", variable has type "Call") \
665 # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None"
666 def f4(x: int) -> None: ...
667 a4: Call = f4
668
669 def f5(x: int, y: int) -> None: ...
670 a5: Call = f5
671
672 def f6(x: int, y: int = 0) -> None: ...
673 a6: Call = f6
674
675 def f7(x: int, *, y: int) -> None: ...
676 a7: Call = f7
677
678 def f8(x: int, *args: int, **kwargs: str) -> None: ...
679 a8: Call = f8
680 [builtins fixtures/tuple.pyi]