1010
1111[case testCallingVariableWithFunctionType]
1212from 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
1516if int():
1617 a = f(a) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
1718if int():
@@ -82,9 +83,9 @@ from typing import Callable
8283class A: pass
8384class B(A): pass
8485
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
8889if int():
8990 g = h # E: Incompatible types in assignment (expression has type "Callable[[B], B]", variable has type "Callable[[A], A]")
9091if int():
@@ -132,7 +133,7 @@ ff = g
132133from typing import Callable
133134
134135def f(a: int, b: str) -> None: pass
135- f_nonames = None # type : Callable[[int, str], None]
136+ f_nonames: Callable[[int, str], None]
136137def g(a: int, b: str = "") -> None: pass
137138def h(aa: int, b: str = "") -> None: pass
138139
@@ -160,7 +161,7 @@ if int():
160161from typing import Any, Callable
161162
162163def everything(*args: Any, **kwargs: Any) -> None: pass
163- everywhere = None # type : Callable[..., None]
164+ everywhere: Callable[..., None]
164165
165166def specific_1(a: int, b: str) -> None: pass
166167def specific_2(a: int, *, b: str) -> None: pass
@@ -238,6 +239,7 @@ if int():
238239 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]")
239240
240241[case testFunctionTypeCompatibilityWithOtherTypes]
242+ # flags: --no-strict-optional
241243from typing import Callable
242244f = None # type: Callable[[], None]
243245a, o = None, None # type: (A, object)
@@ -272,8 +274,8 @@ def g(x: int) -> Tuple[()]:
272274
273275[case testFunctionSubtypingWithVoid]
274276from typing import Callable
275- f = None # type : Callable[[], None]
276- g = None # type : Callable[[], object]
277+ f: Callable[[], None]
278+ g: Callable[[], object]
277279if int():
278280 f = g # E: Incompatible types in assignment (expression has type "Callable[[], object]", variable has type "Callable[[], None]")
279281if int():
@@ -286,9 +288,9 @@ if int():
286288
287289[case testFunctionSubtypingWithMultipleArgs]
288290from 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]
292294if int():
293295 f = g # E: Incompatible types in assignment (expression has type "Callable[[A, B], None]", variable has type "Callable[[A, A], None]")
294296if int():
@@ -313,9 +315,9 @@ class B(A): pass
313315
314316[case testFunctionTypesWithDifferentArgumentCounts]
315317from 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]
319321
320322if int():
321323 f = g # E: Incompatible types in assignment (expression has type "Callable[[A], None]", variable has type "Callable[[], None]")
@@ -342,8 +344,8 @@ class A:
342344
343345def f() -> None: pass
344346
345- t = None # type : type
346- a = None # type : A
347+ t: type
348+ a: A
347349
348350if int():
349351 a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A")
@@ -356,9 +358,9 @@ if int():
356358from foo import *
357359[file foo.pyi]
358360from 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]
362364
363365if int():
364366 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:
395397from foo import *
396398[file foo.pyi]
397399from 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
403407
404408if int():
405409 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
448452[case testSubtypingTypeTypeAsCallable]
449453from typing import Callable, Type
450454class A: pass
451- x = None # type : Callable[..., A]
452- y = None # type : Type[A]
455+ x: Callable[..., A]
456+ y: Type[A]
453457x = y
454458
455459[case testSubtypingCallableAsTypeType]
456460from typing import Callable, Type
457461class A: pass
458- x = None # type : Callable[..., A]
459- y = None # type : Type[A]
462+ x: Callable[..., A]
463+ y: Type[A]
460464if int():
461465 y = x # E: Incompatible types in assignment (expression has type "Callable[..., A]", variable has type "Type[A]")
462466
@@ -573,11 +577,11 @@ A().f('') # E: Argument 1 to "f" of "A" has incompatible type "str"; expected "i
573577[case testMethodAsDataAttribute]
574578from typing import Any, Callable, ClassVar
575579class B: pass
576- x = None # type : Any
580+ x: Any
577581class A:
578582 f = x # type: ClassVar[Callable[[A], None]]
579583 g = x # type: ClassVar[Callable[[A, B], None]]
580- a = None # type : A
584+ a: A
581585a.f()
582586a.g(B())
583587a.f(a) # E: Too many arguments
@@ -586,21 +590,21 @@ a.g() # E: Too few arguments
586590[case testMethodWithInvalidMethodAsDataAttribute]
587591from typing import Any, Callable, ClassVar
588592class B: pass
589- x = None # type : Any
593+ x: Any
590594class A:
591595 f = x # type: ClassVar[Callable[[], None]]
592596 g = x # type: ClassVar[Callable[[B], None]]
593- a = None # type : A
597+ a: A
594598a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument
595599a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]"
596600
597601[case testMethodWithDynamicallyTypedMethodAsDataAttribute]
598602from typing import Any, Callable, ClassVar
599603class B: pass
600- x = None # type : Any
604+ x: Any
601605class A:
602606 f = x # type: ClassVar[Callable[[Any], Any]]
603- a = None # type : A
607+ a: A
604608a.f()
605609a.f(a) # E: Too many arguments
606610
@@ -627,7 +631,7 @@ class A:
627631 @overload
628632 def f(self, b: B) -> None: pass
629633 g = f
630- a = None # type : A
634+ a: A
631635a.g()
632636a.g(B())
633637a.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" \
640644class A:
641645 def f(self, x): pass
642646 g = f
643- a = None # type : A
647+ a: A
644648a.g(object())
645649a.g(a, a) # E: Too many arguments
646650a.g() # E: Too few arguments
@@ -652,7 +656,7 @@ class B: pass
652656class A(Generic[t]):
653657 def f(self, x: t) -> None: pass
654658 g = f
655- a = None # type : A[B]
659+ a: A[B]
656660a.g(B())
657661a.g(a) # E: Argument 1 has incompatible type "A[B]"; expected "B"
658662
@@ -661,11 +665,11 @@ from typing import Any, TypeVar, Generic, Callable, ClassVar
661665t = TypeVar('t')
662666class B: pass
663667class C: pass
664- x = None # type : Any
668+ x: Any
665669class A(Generic[t]):
666670 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]
669673ab.f()
670674ac.f() # E: Invalid self argument "A[C]" to attribute function "f" with type "Callable[[A[B]], None]"
671675
@@ -674,21 +678,21 @@ from typing import Any, TypeVar, Generic, Callable, ClassVar
674678t = TypeVar('t')
675679class B: pass
676680class C: pass
677- x = None # type : Any
681+ x: Any
678682class A(Generic[t]):
679683 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]
682686ab.f()
683687ac.f()
684688
685689[case testCallableDataAttribute]
686690from typing import Callable, ClassVar
687691class A:
688- g = None # type : ClassVar[Callable[[A], None]]
692+ g: ClassVar[Callable[[A], None]]
689693 def __init__(self, f: Callable[[], None]) -> None:
690694 self.f = f
691- a = A(None)
695+ a = A(lambda: None)
692696a.f()
693697a.g()
694698a.f(a) # E: Too many arguments
@@ -895,7 +899,7 @@ def dec(x) -> Callable[[Any], None]: pass
895899class A:
896900 @dec
897901 def f(self, a, b, c): pass
898- a = None # type : A
902+ a: A
899903a.f()
900904a.f(None) # E: Too many arguments for "f" of "A"
901905
@@ -1945,9 +1949,9 @@ def a(f: Callable[[VarArg(int)], int]):
19451949from typing import Callable
19461950from mypy_extensions import Arg, DefaultArg
19471951
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]
19511955
19521956def isf(ii: int, ss: str) -> str:
19531957 return ss
@@ -2140,6 +2144,7 @@ main:8: error: Cannot use a covariant type variable as a parameter
21402144from typing import TypeVar, Generic, Callable
21412145
21422146[case testRejectContravariantReturnType]
2147+ # flags: --no-strict-optional
21432148from typing import TypeVar, Generic
21442149
21452150t = TypeVar('t', contravariant=True)
@@ -2148,16 +2153,18 @@ class A(Generic[t]):
21482153 return None
21492154[builtins fixtures/bool.pyi]
21502155[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
21522157
21532158[case testAcceptCovariantReturnType]
2159+ # flags: --no-strict-optional
21542160from typing import TypeVar, Generic
21552161
21562162t = TypeVar('t', covariant=True)
21572163class A(Generic[t]):
21582164 def foo(self) -> t:
21592165 return None
21602166[builtins fixtures/bool.pyi]
2167+
21612168[case testAcceptContravariantArgument]
21622169from typing import TypeVar, Generic
21632170
0 commit comments