10
10
11
11
[case testCallingVariableWithFunctionType]
12
12
from typing import Callable
13
- f = None # type: Callable[[A], B]
14
- a, b = None, None # type: (A, B)
13
+ f: Callable[[A], B]
14
+ a: A
15
+ b: B
15
16
if int():
16
17
a = f(a) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
17
18
if int():
@@ -82,9 +83,9 @@ from typing import Callable
82
83
class A: pass
83
84
class B(A): pass
84
85
85
- f = None # type : Callable[[B], A]
86
- g = None # type : Callable[[A], A] # subtype of f
87
- h = None # type : Callable[[B], B] # subtype of f
86
+ f: Callable[[B], A]
87
+ g: Callable[[A], A] # subtype of f
88
+ h: Callable[[B], B] # subtype of f
88
89
if int():
89
90
g = h # E: Incompatible types in assignment (expression has type "Callable[[B], B]", variable has type "Callable[[A], A]")
90
91
if int():
@@ -132,7 +133,7 @@ ff = g
132
133
from typing import Callable
133
134
134
135
def f(a: int, b: str) -> None: pass
135
- f_nonames = None # type : Callable[[int, str], None]
136
+ f_nonames: Callable[[int, str], None]
136
137
def g(a: int, b: str = "") -> None: pass
137
138
def h(aa: int, b: str = "") -> None: pass
138
139
@@ -160,7 +161,7 @@ if int():
160
161
from typing import Any, Callable
161
162
162
163
def everything(*args: Any, **kwargs: Any) -> None: pass
163
- everywhere = None # type : Callable[..., None]
164
+ everywhere: Callable[..., None]
164
165
165
166
def specific_1(a: int, b: str) -> None: pass
166
167
def specific_2(a: int, *, b: str) -> None: pass
@@ -238,6 +239,7 @@ if int():
238
239
gg = f # E: Incompatible types in assignment (expression has type "Callable[[int, str], None]", variable has type "Callable[[Arg(int, 'a'), Arg(str, 'b')], None]")
239
240
240
241
[case testFunctionTypeCompatibilityWithOtherTypes]
242
+ # flags: --no-strict-optional
241
243
from typing import Callable
242
244
f = None # type: Callable[[], None]
243
245
a, o = None, None # type: (A, object)
@@ -272,8 +274,8 @@ def g(x: int) -> Tuple[()]:
272
274
273
275
[case testFunctionSubtypingWithVoid]
274
276
from typing import Callable
275
- f = None # type : Callable[[], None]
276
- g = None # type : Callable[[], object]
277
+ f: Callable[[], None]
278
+ g: Callable[[], object]
277
279
if int():
278
280
f = g # E: Incompatible types in assignment (expression has type "Callable[[], object]", variable has type "Callable[[], None]")
279
281
if int():
@@ -286,9 +288,9 @@ if int():
286
288
287
289
[case testFunctionSubtypingWithMultipleArgs]
288
290
from typing import Callable
289
- f = None # type : Callable[[A, A], None]
290
- g = None # type : Callable[[A, B], None]
291
- h = None # type : Callable[[B, B], None]
291
+ f: Callable[[A, A], None]
292
+ g: Callable[[A, B], None]
293
+ h: Callable[[B, B], None]
292
294
if int():
293
295
f = g # E: Incompatible types in assignment (expression has type "Callable[[A, B], None]", variable has type "Callable[[A, A], None]")
294
296
if int():
@@ -313,9 +315,9 @@ class B(A): pass
313
315
314
316
[case testFunctionTypesWithDifferentArgumentCounts]
315
317
from typing import Callable
316
- f = None # type : Callable[[], None]
317
- g = None # type : Callable[[A], None]
318
- h = None # type : Callable[[A, A], None]
318
+ f: Callable[[], None]
319
+ g: Callable[[A], None]
320
+ h: Callable[[A, A], None]
319
321
320
322
if int():
321
323
f = g # E: Incompatible types in assignment (expression has type "Callable[[A], None]", variable has type "Callable[[], None]")
@@ -342,8 +344,8 @@ class A:
342
344
343
345
def f() -> None: pass
344
346
345
- t = None # type : type
346
- a = None # type : A
347
+ t: type
348
+ a: A
347
349
348
350
if int():
349
351
a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A")
@@ -356,9 +358,9 @@ if int():
356
358
from foo import *
357
359
[file foo.pyi]
358
360
from typing import Callable, overload
359
- f = None # type : Callable[[AA], A]
360
- g = None # type : Callable[[B], B]
361
- h = None # type : Callable[[A], AA]
361
+ f: Callable[[AA], A]
362
+ g: Callable[[B], B]
363
+ h: Callable[[A], AA]
362
364
363
365
if int():
364
366
h = i # E: Incompatible types in assignment (expression has type overloaded function, variable has type "Callable[[A], AA]")
@@ -395,11 +397,13 @@ def j(x: A) -> AA:
395
397
from foo import *
396
398
[file foo.pyi]
397
399
from typing import Callable, overload
398
- g1 = None # type: Callable[[A], A]
399
- g2 = None # type: Callable[[B], B]
400
- g3 = None # type: Callable[[C], C]
401
- g4 = None # type: Callable[[A], B]
402
- a, b, c = None, None, None # type: (A, B, C)
400
+ g1: Callable[[A], A]
401
+ g2: Callable[[B], B]
402
+ g3: Callable[[C], C]
403
+ g4: Callable[[A], B]
404
+ a: A
405
+ b: B
406
+ c: C
403
407
404
408
if int():
405
409
b = f(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
@@ -448,15 +452,15 @@ f([D]) # E: List item 0 has incompatible type "Type[D]"; expected "Callable[[An
448
452
[case testSubtypingTypeTypeAsCallable]
449
453
from typing import Callable, Type
450
454
class A: pass
451
- x = None # type : Callable[..., A]
452
- y = None # type : Type[A]
455
+ x: Callable[..., A]
456
+ y: Type[A]
453
457
x = y
454
458
455
459
[case testSubtypingCallableAsTypeType]
456
460
from typing import Callable, Type
457
461
class A: pass
458
- x = None # type : Callable[..., A]
459
- y = None # type : Type[A]
462
+ x: Callable[..., A]
463
+ y: Type[A]
460
464
if int():
461
465
y = x # E: Incompatible types in assignment (expression has type "Callable[..., A]", variable has type "Type[A]")
462
466
@@ -573,11 +577,11 @@ A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "i
573
577
[case testMethodAsDataAttribute]
574
578
from typing import Any, Callable, ClassVar
575
579
class B: pass
576
- x = None # type : Any
580
+ x: Any
577
581
class A:
578
582
f = x # type: ClassVar[Callable[[A], None]]
579
583
g = x # type: ClassVar[Callable[[A, B], None]]
580
- a = None # type : A
584
+ a: A
581
585
a.f()
582
586
a.g(B())
583
587
a.f(a) # E: Too many arguments
@@ -586,21 +590,21 @@ a.g() # E: Too few arguments
586
590
[case testMethodWithInvalidMethodAsDataAttribute]
587
591
from typing import Any, Callable, ClassVar
588
592
class B: pass
589
- x = None # type : Any
593
+ x: Any
590
594
class A:
591
595
f = x # type: ClassVar[Callable[[], None]]
592
596
g = x # type: ClassVar[Callable[[B], None]]
593
- a = None # type : A
597
+ a: A
594
598
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument
595
599
a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]"
596
600
597
601
[case testMethodWithDynamicallyTypedMethodAsDataAttribute]
598
602
from typing import Any, Callable, ClassVar
599
603
class B: pass
600
- x = None # type : Any
604
+ x: Any
601
605
class A:
602
606
f = x # type: ClassVar[Callable[[Any], Any]]
603
- a = None # type : A
607
+ a: A
604
608
a.f()
605
609
a.f(a) # E: Too many arguments
606
610
@@ -627,7 +631,7 @@ class A:
627
631
@overload
628
632
def f(self, b: B) -> None: pass
629
633
g = f
630
- a = None # type : A
634
+ a: A
631
635
a.g()
632
636
a.g(B())
633
637
a.g(a) # E: No overload variant matches argument type "A" \
@@ -640,7 +644,7 @@ a.g(a) # E: No overload variant matches argument type "A" \
640
644
class A:
641
645
def f(self, x): pass
642
646
g = f
643
- a = None # type : A
647
+ a: A
644
648
a.g(object())
645
649
a.g(a, a) # E: Too many arguments
646
650
a.g() # E: Too few arguments
@@ -652,7 +656,7 @@ class B: pass
652
656
class A(Generic[t]):
653
657
def f(self, x: t) -> None: pass
654
658
g = f
655
- a = None # type : A[B]
659
+ a: A[B]
656
660
a.g(B())
657
661
a.g(a) # E: Argument 1 has incompatible type "A[B]"; expected "B"
658
662
@@ -661,11 +665,11 @@ from typing import Any, TypeVar, Generic, Callable, ClassVar
661
665
t = TypeVar('t')
662
666
class B: pass
663
667
class C: pass
664
- x = None # type : Any
668
+ x: Any
665
669
class A(Generic[t]):
666
670
f = x # type: ClassVar[Callable[[A[B]], None]]
667
- ab = None # type : A[B]
668
- ac = None # type : A[C]
671
+ ab: A[B]
672
+ ac: A[C]
669
673
ab.f()
670
674
ac.f() # E: Invalid self argument "A[C]" to attribute function "f" with type "Callable[[A[B]], None]"
671
675
@@ -674,21 +678,21 @@ from typing import Any, TypeVar, Generic, Callable, ClassVar
674
678
t = TypeVar('t')
675
679
class B: pass
676
680
class C: pass
677
- x = None # type : Any
681
+ x: Any
678
682
class A(Generic[t]):
679
683
f = x # type: ClassVar[Callable[[A], None]]
680
- ab = None # type : A[B]
681
- ac = None # type : A[C]
684
+ ab: A[B]
685
+ ac: A[C]
682
686
ab.f()
683
687
ac.f()
684
688
685
689
[case testCallableDataAttribute]
686
690
from typing import Callable, ClassVar
687
691
class A:
688
- g = None # type : ClassVar[Callable[[A], None]]
692
+ g: ClassVar[Callable[[A], None]]
689
693
def __init__(self, f: Callable[[], None]) -> None:
690
694
self.f = f
691
- a = A(None)
695
+ a = A(lambda: None)
692
696
a.f()
693
697
a.g()
694
698
a.f(a) # E: Too many arguments
@@ -895,7 +899,7 @@ def dec(x) -> Callable[[Any], None]: pass
895
899
class A:
896
900
@dec
897
901
def f(self, a, b, c): pass
898
- a = None # type : A
902
+ a: A
899
903
a.f()
900
904
a.f(None) # E: Too many arguments for "f" of "A"
901
905
@@ -1945,9 +1949,9 @@ def a(f: Callable[[VarArg(int)], int]):
1945
1949
from typing import Callable
1946
1950
from mypy_extensions import Arg, DefaultArg
1947
1951
1948
- int_str_fun = None # type : Callable[[int, str], str]
1949
- int_opt_str_fun = None # type : Callable[[int, DefaultArg(str, None)], str]
1950
- int_named_str_fun = None # type : Callable[[int, Arg(str, 's')], str]
1952
+ int_str_fun: Callable[[int, str], str]
1953
+ int_opt_str_fun: Callable[[int, DefaultArg(str, None)], str]
1954
+ int_named_str_fun: Callable[[int, Arg(str, 's')], str]
1951
1955
1952
1956
def isf(ii: int, ss: str) -> str:
1953
1957
return ss
@@ -2140,6 +2144,7 @@ main:8: error: Cannot use a covariant type variable as a parameter
2140
2144
from typing import TypeVar, Generic, Callable
2141
2145
2142
2146
[case testRejectContravariantReturnType]
2147
+ # flags: --no-strict-optional
2143
2148
from typing import TypeVar, Generic
2144
2149
2145
2150
t = TypeVar('t', contravariant=True)
@@ -2148,16 +2153,18 @@ class A(Generic[t]):
2148
2153
return None
2149
2154
[builtins fixtures/bool.pyi]
2150
2155
[out]
2151
- main:5 : error: Cannot use a contravariant type variable as return type
2156
+ main:6 : error: Cannot use a contravariant type variable as return type
2152
2157
2153
2158
[case testAcceptCovariantReturnType]
2159
+ # flags: --no-strict-optional
2154
2160
from typing import TypeVar, Generic
2155
2161
2156
2162
t = TypeVar('t', covariant=True)
2157
2163
class A(Generic[t]):
2158
2164
def foo(self) -> t:
2159
2165
return None
2160
2166
[builtins fixtures/bool.pyi]
2167
+
2161
2168
[case testAcceptContravariantArgument]
2162
2169
from typing import TypeVar, Generic
2163
2170
0 commit comments