File: check-callable.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (678 lines) | stat: -rw-r--r-- 15,244 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
[case testCallableDef]
def f() -> None: pass

if callable(f):
    f()
else:
    f += 5

[builtins fixtures/callable.pyi]

[case testCallableLambda]
f = lambda: None

if callable(f):
    f()
else:
    f += 5

[builtins fixtures/callable.pyi]

[case testCallableNotCallable]
x = 5

if callable(x):
    x()
else:
    x += 5

[builtins fixtures/callable.pyi]

[case testUnion]
from typing import Callable, Union

x = 5  # type: Union[int, Callable[[], str]]

if callable(x):
    y = x() + 'test'
else:
    z = x + 6

[builtins fixtures/callable.pyi]

[case testUnionMultipleReturnTypes]
from typing import Callable, Union

x = 5  # type: Union[int, Callable[[], str], Callable[[], int]]

if callable(x):
    y = x() + 2 # E: Unsupported operand types for + ("str" and "int") \
                # N: Left operand is of type "Union[str, int]"
else:
    z = x + 6

[builtins fixtures/callable.pyi]

[case testUnionMultipleNonCallableTypes]
from typing import Callable, Union

x = 5  # type: Union[int, str, Callable[[], str]]

if callable(x):
    y = x() + 'test'
else:
    z = x + 6  # E: Unsupported operand types for + ("str" and "int") \
               # N: Left operand is of type "Union[int, str]"

[builtins fixtures/callable.pyi]

[case testCallableThenIsinstance]
from typing import Callable, Union

x = 5  # type: Union[int, str, Callable[[], str], Callable[[], int]]

if callable(x):
    y = x()
    if isinstance(y, int):
        b1 = y + 2
    else:
        b2 = y + 'test'
else:
    if isinstance(x, int):
        b3 = x + 3
    else:
        b4 = x + 'test2'

[builtins fixtures/callable.pyi]

[case testIsinstanceThenCallable]
from typing import Callable, Union

x = 5  # type: Union[int, str, Callable[[], str], Callable[[], int]]

if isinstance(x, int):
    b1 = x + 1
else:
    if callable(x):
        y = x()
        if isinstance(y, int):
            b2 = y + 1
        else:
            b3 = y + 'test'
    else:
        b4 = x + 'test2'

[builtins fixtures/callable.pyi]

[case testCallableWithDifferentArgTypes]
from typing import Callable, Union

x = 5  # type: Union[int, Callable[[], None], Callable[[int], None]]

if callable(x):
    x()  # E: Too few arguments

[builtins fixtures/callable.pyi]

[case testClassInitializer]
from typing import Callable, Union

class A:
    x = 5

a = A  # type: Union[A, Callable[[], A]]

if callable(a):
    a = a()

a.x + 6

[builtins fixtures/callable.pyi]

[case testCallableVariables]
from typing import Union

class A:
    x = 5

class B:
    x = int

x = A()  # type: Union[A, B]

if callable(x.x):
    y = x.x()
else:
    y = x.x + 5

[builtins fixtures/callable.pyi]

[case testCallableAnd]
from typing import Union, Callable

x = 5  # type: Union[int, Callable[[], str]]

if callable(x) and x() == 'test':
    x()
else:
    x + 5  # E: Unsupported left operand type for + ("Callable[[], str]") \
           # N: Left operand is of type "Union[int, Callable[[], str]]"

[builtins fixtures/callable.pyi]

[case testCallableOr]
from typing import Union, Callable

x = 5  # type: Union[int, Callable[[], str]]

if callable(x) or x() == 'test':  # E: "int" not callable
    x()  # E: "int" not callable
else:
    x + 5
[builtins fixtures/callable.pyi]

[case testCallableOrOtherType]
from typing import Union, Callable

x = 5  # type: Union[int, Callable[[], str]]

if callable(x) or x == 2:
    pass
else:
    pass
[builtins fixtures/callable.pyi]

[case testAnyCallable]
from typing import Any

x = 5  # type: Any

if callable(x):
    reveal_type(x)  # N: Revealed type is "Any"
else:
    reveal_type(x)  # N: Revealed type is "Any"
[builtins fixtures/callable.pyi]

[case testCallableCallableClasses]
from typing import Union


class A:
    pass


class B:
    def __call__(self) -> None:
        pass


a = A()  # type: A
b = B()  # type: B
c = A()  # type: Union[A, B]

if callable(a):
    5 + 'test'  # E: Unsupported operand types for + ("int" and "str")

if not callable(b):
    5 + 'test'

if callable(c):
    reveal_type(c)  # N: Revealed type is "__main__.B"
else:
    reveal_type(c)  # N: Revealed type is "__main__.A"

[builtins fixtures/callable.pyi]

[case testDecoratedCallMethods]
from typing import Any, Callable, Union, TypeVar

F = TypeVar('F', bound=Callable)

def decorator(f: F) -> F:
    pass
def change(f: Callable) -> Callable[[Any], str]:
    pass
def untyped(f):
    pass

class Some1:
    @decorator
    def __call__(self) -> int:
        pass
class Some2:
    @change
    def __call__(self) -> int:
        pass
class Some3:
    @untyped
    def __call__(self) -> int:
        pass
class Some4:
    __call__: Any

s1: Some1
s2: Some2
s3: Some3
s4: Some4

if callable(s1):
    1 + 'a'  # E: Unsupported operand types for + ("int" and "str")
else:
    2 + 'b'
if callable(s2):
    1 + 'a'  # E: Unsupported operand types for + ("int" and "str")
else:
    2 + 'b'
if callable(s3):
    1 + 'a'  # E: Unsupported operand types for + ("int" and "str")
else:
    2 + 'b'  # E: Unsupported operand types for + ("int" and "str")
if callable(s4):
    1 + 'a'  # E: Unsupported operand types for + ("int" and "str")
else:
    2 + 'b'  # E: Unsupported operand types for + ("int" and "str")
[builtins fixtures/callable.pyi]

[case testCallableNestedUnions]
from typing import Callable, Union

T = Union[Union[int, Callable[[], int]], Union[str, Callable[[], str]]]

def f(t: T) -> None:
    if callable(t):
        reveal_type(t())  # N: Revealed type is "Union[builtins.int, builtins.str]"
    else:
        reveal_type(t)  # N: Revealed type is "Union[builtins.int, builtins.str]"

[builtins fixtures/callable.pyi]

[case testCallableTypeVarEmpty]
from typing import TypeVar

T = TypeVar('T')

def f(t: T) -> T:
    if callable(t):
        return 5  # E: Incompatible return value type (got "int", expected "T")
    else:
        return t

[builtins fixtures/callable.pyi]

[case testCallableTypeVarUnion]
from typing import Callable, TypeVar, Union

T = TypeVar('T', int, Callable[[], int], Union[str, Callable[[], str]])

def f(t: T) -> None:
    if callable(t):
        reveal_type(t())  # N: Revealed type is "Any"  \
            # N: Revealed type is "builtins.int"  \
            # N: Revealed type is "builtins.str"
    else:
        reveal_type(t)  # N: Revealed type is "builtins.int"  # N: Revealed type is "builtins.str"

[builtins fixtures/callable.pyi]

[case testCallableTypeVarBound]
from typing import TypeVar


class A:
    def __call__(self) -> str:
        return 'hi'


T = TypeVar('T', bound=A)

def f(t: T) -> str:
    if callable(t):
        return t()
    else:
        return 5

[builtins fixtures/callable.pyi]

[case testCallableTypeType]
from typing import Type


class A:
    pass


T = Type[A]

def f(t: T) -> A:
    if callable(t):
        return t()
    else:
        return 5

[builtins fixtures/callable.pyi]

[case testCallableTypeUnion]
from abc import ABCMeta, abstractmethod
from typing import Type, Union


class A(metaclass=ABCMeta):
    @abstractmethod
    def f(self) -> None:
        pass


class B:
    pass


x = B  # type: Union[Type[A], Type[B]]
if callable(x):
    # Abstract classes raise an error when called, but are indeed `callable`
    pass
else:
    'test' + 5

[builtins fixtures/callable.pyi]

[case testCallableUnionOfTypes]
from abc import ABCMeta, abstractmethod
from typing import Type, Union


class A(metaclass=ABCMeta):
    @abstractmethod
    def f(self) -> None:
        pass


class B:
    pass


x = B  # type: Type[Union[A, B]]
if callable(x):
    # Abstract classes raise an error when called, but are indeed `callable`
    pass
else:
    'test' + 5

[builtins fixtures/callable.pyi]

[case testCallableObject]

def f(o: object) -> None:
    if callable(o):
        o(1,2,3)
        1 + 'boom'  # E: Unsupported operand types for + ("int" and "str")
        o('hi') + 12
        reveal_type(o)  # N: Revealed type is "__main__.<callable subtype of object>"

[builtins fixtures/callable.pyi]

[case testCallableObject2]

class Foo(object):
    def bar(self) -> None:
        pass

def g(o: Foo) -> None:
    o.bar()
    if callable(o):
        o.foo()  # E: "Foo" has no attribute "foo"
        o.bar()
        o(1,2,3)
    else:
        o.bar()

[builtins fixtures/callable.pyi]

[case testCallableObjectAny]

from typing import Any

class Foo(Any):
    def bar(self) -> None:
        pass

def g(o: Foo) -> None:
    o.bar()
    o.baz()
    if callable(o):
        o('test')
        o.lurr(1,2,3)

[builtins fixtures/callable.pyi]

[case testCallableObjectGeneric]

from typing import TypeVar, Generic

T = TypeVar('T')
class Test(Generic[T]):
    def __self__(self, x: T) -> None:
        self.x = x

def g(o: Test[T], x: T) -> T:
    if callable(o):
        o.foo()  # E: "Test[T]" has no attribute "foo"
        o(1,2,3)
        o.x = x
        o.x = 1  # E: Incompatible types in assignment (expression has type "int", variable has type "T")
        1 + o.x  # E: Unsupported operand types for + ("int" and "T")
        return o.x

    return x

[builtins fixtures/callable.pyi]

[case testCallablePromote]

def take_float(f: float) -> None:
    pass

def g(o: int) -> None:
    if callable(o):
        take_float(o)
        o(1,2,3)

[builtins fixtures/callable.pyi]

[case testCallableTuple]

from typing import NamedTuple

Thing = NamedTuple('Thing', [('s', str), ('n', int)])

def g(o: Thing) -> None:
    if callable(o):
        o.s + o.n  # E: Unsupported operand types for + ("str" and "int")
        i, s = o
        i + s  # E: Unsupported operand types for + ("str" and "int")
        o(1,2,3)

[builtins fixtures/callable.pyi]

[case testCallableNoArgs]

if callable():  # E: Missing positional argument "x" in call to "callable"
    pass

[builtins fixtures/callable.pyi]

[case testCallableWithNoneArgs]

fn = None
if callable(fn):
  fn()

[builtins fixtures/callable.pyi]

[case testCallableUnionOfNoneAndCallable]

from typing import Union, Callable

def f() -> int:
    return 42

fn = f  # type: Union[None, Callable[[], int]]

if callable(fn):
    reveal_type(fn) # N: Revealed type is "def () -> builtins.int"
else:
    reveal_type(fn) # N: Revealed type is "None"

[builtins fixtures/callable.pyi]

[case testBuiltinsTypeAsCallable]
from __future__ import annotations

reveal_type(type)  # N: Revealed type is "def (x: Any) -> builtins.type"
_TYPE = type
reveal_type(_TYPE)  # N: Revealed type is "def (x: Any) -> builtins.type"
_TYPE('bar')

[builtins fixtures/callable.pyi]

[case testErrorMessageAboutSelf]
# https://github.com/python/mypy/issues/11309
class Some:
    def method(self, a) -> None: pass
    @classmethod
    def cls_method(cls, a) -> None: pass
    @staticmethod
    def st_method(a) -> None: pass

    def bad_method(a) -> None: pass
    @classmethod
    def bad_cls_method(a) -> None: pass
    @staticmethod
    def bad_st_method() -> None: pass

s: Some

s.method(1)
s.cls_method(1)
Some.cls_method(1)
s.st_method(1)
Some.st_method(1)

s.method(1, 2)  # E: Too many arguments for "method" of "Some"
s.cls_method(1, 2)  # E: Too many arguments for "cls_method" of "Some"
Some.cls_method(1, 2)  # E: Too many arguments for "cls_method" of "Some"
s.st_method(1, 2)  # E: Too many arguments for "st_method" of "Some"
Some.st_method(1, 2)  # E: Too many arguments for "st_method" of "Some"

s.bad_method(1)  # E: Too many arguments for "bad_method" of "Some" \
                 # N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
s.bad_cls_method(1)  # E: Too many arguments for "bad_cls_method" of "Some" \
                     # N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
Some.bad_cls_method(1)  # E: Too many arguments for "bad_cls_method" of "Some" \
                        # N: Looks like the first special argument in a method is not named "self", "cls", or "mcs", maybe it is missing?
s.bad_st_method(1)  # E: Too many arguments for "bad_st_method" of "Some"
Some.bad_st_method(1)  # E: Too many arguments for "bad_st_method" of "Some"
[builtins fixtures/callable.pyi]

[case testClassMethodAliasStub]
from a import f
f("no")  # E: Argument 1 has incompatible type "str"; expected "int"
[file a.pyi]
from b import C
f = C.f
[file b.pyi]
import a
class C(B):
    @classmethod
    def f(self, x: int) -> C: ...
class B: ...
[builtins fixtures/classmethod.pyi]

[case testClassMethodAliasInClass]
from typing import overload

class C:
    @classmethod
    def foo(cls) -> int: ...

    bar = foo

    @overload
    @classmethod
    def foo2(cls, x: int) -> int: ...
    @overload
    @classmethod
    def foo2(cls, x: str) -> str: ...
    @classmethod
    def foo2(cls, x):
        ...

    bar2 = foo2

reveal_type(C.bar)  # N: Revealed type is "def () -> builtins.int"
reveal_type(C().bar)  # N: Revealed type is "def () -> builtins.int"
reveal_type(C.bar2)  # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
reveal_type(C().bar2)  # N: Revealed type is "Overload(def (x: builtins.int) -> builtins.int, def (x: builtins.str) -> builtins.str)"
[builtins fixtures/classmethod.pyi]

[case testPropertyAliasInClassBody]
class A:
    @property
    def f(self) -> int: ...

    g = f

    @property
    def f2(self) -> int: ...
    @f2.setter
    def f2(self, val: int) -> None: ...

    g2 = f2

reveal_type(A().g)  # N: Revealed type is "builtins.int"
reveal_type(A().g2)  # N: Revealed type is "builtins.int"
A().g = 1  # E: Property "g" defined in "A" is read-only
A().g2 = 1
A().g2 = "no"  # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[builtins fixtures/property.pyi]

[case testCallableUnionCallback]
from typing import Union, Callable, TypeVar

TA = TypeVar("TA", bound="A")
class A:
    def __call__(self: TA, other: Union[Callable, TA]) -> TA: ...
a: A
a()  # E: Missing positional argument "other" in call to "__call__" of "A"
a(a)
a(lambda: None)

[case testCallableSubtypingTrivialSuffix]
from typing import Any, Protocol

class Call(Protocol):
    def __call__(self, x: int, *args: Any, **kwargs: Any) -> None: ...

def f1() -> None: ...
a1: Call = f1  # E: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "Call") \
               # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None"
def f2(x: str) -> None: ...
a2: Call = f2  # E: Incompatible types in assignment (expression has type "Callable[[str], None]", variable has type "Call") \
               # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None"
def f3(y: int) -> None: ...
a3: Call = f3  # E: Incompatible types in assignment (expression has type "Callable[[int], None]", variable has type "Call") \
               # N: "Call.__call__" has type "def __call__(self, x: int, *args: Any, **kwargs: Any) -> None"
def f4(x: int) -> None: ...
a4: Call = f4

def f5(x: int, y: int) -> None: ...
a5: Call = f5

def f6(x: int, y: int = 0) -> None: ...
a6: Call = f6

def f7(x: int, *, y: int) -> None: ...
a7: Call = f7

def f8(x: int, *args: int, **kwargs: str) -> None: ...
a8: Call = f8
[builtins fixtures/tuple.pyi]