Skip to content

Commit e0b159e

Browse files
authored
Enable strict optional for more test files (6) (#15603)
1 parent fa8853b commit e0b159e

File tree

4 files changed

+216
-185
lines changed

4 files changed

+216
-185
lines changed

mypy/test/testcheck.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@
5151

5252
# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
5353
no_strict_optional_files = {
54-
"check-functions.test",
55-
"check-generic-subtyping.test",
56-
"check-generics.test",
5754
"check-inference-context.test",
5855
"check-inference.test",
5956
"check-isinstance.test",

test-data/unit/check-functions.test

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
[case testCallingVariableWithFunctionType]
1212
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
1516
if int():
1617
a = f(a) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
1718
if int():
@@ -82,9 +83,9 @@ from typing import Callable
8283
class A: pass
8384
class 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
8889
if int():
8990
g = h # E: Incompatible types in assignment (expression has type "Callable[[B], B]", variable has type "Callable[[A], A]")
9091
if int():
@@ -132,7 +133,7 @@ ff = g
132133
from typing import Callable
133134

134135
def f(a: int, b: str) -> None: pass
135-
f_nonames = None # type: Callable[[int, str], None]
136+
f_nonames: Callable[[int, str], None]
136137
def g(a: int, b: str = "") -> None: pass
137138
def h(aa: int, b: str = "") -> None: pass
138139

@@ -160,7 +161,7 @@ if int():
160161
from typing import Any, Callable
161162

162163
def everything(*args: Any, **kwargs: Any) -> None: pass
163-
everywhere = None # type: Callable[..., None]
164+
everywhere: Callable[..., None]
164165

165166
def specific_1(a: int, b: str) -> None: pass
166167
def 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
241243
from typing import Callable
242244
f = None # type: Callable[[], None]
243245
a, o = None, None # type: (A, object)
@@ -272,8 +274,8 @@ def g(x: int) -> Tuple[()]:
272274

273275
[case testFunctionSubtypingWithVoid]
274276
from typing import Callable
275-
f = None # type: Callable[[], None]
276-
g = None # type: Callable[[], object]
277+
f: Callable[[], None]
278+
g: Callable[[], object]
277279
if int():
278280
f = g # E: Incompatible types in assignment (expression has type "Callable[[], object]", variable has type "Callable[[], None]")
279281
if int():
@@ -286,9 +288,9 @@ if int():
286288

287289
[case testFunctionSubtypingWithMultipleArgs]
288290
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]
292294
if int():
293295
f = g # E: Incompatible types in assignment (expression has type "Callable[[A, B], None]", variable has type "Callable[[A, A], None]")
294296
if int():
@@ -313,9 +315,9 @@ class B(A): pass
313315

314316
[case testFunctionTypesWithDifferentArgumentCounts]
315317
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]
319321

320322
if 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

343345
def f() -> None: pass
344346

345-
t = None # type: type
346-
a = None # type: A
347+
t: type
348+
a: A
347349

348350
if int():
349351
a = A # E: Incompatible types in assignment (expression has type "Type[A]", variable has type "A")
@@ -356,9 +358,9 @@ if int():
356358
from foo import *
357359
[file foo.pyi]
358360
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]
362364

363365
if 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:
395397
from foo import *
396398
[file foo.pyi]
397399
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
403407

404408
if 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]
449453
from typing import Callable, Type
450454
class A: pass
451-
x = None # type: Callable[..., A]
452-
y = None # type: Type[A]
455+
x: Callable[..., A]
456+
y: Type[A]
453457
x = y
454458

455459
[case testSubtypingCallableAsTypeType]
456460
from typing import Callable, Type
457461
class A: pass
458-
x = None # type: Callable[..., A]
459-
y = None # type: Type[A]
462+
x: Callable[..., A]
463+
y: Type[A]
460464
if 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]
574578
from typing import Any, Callable, ClassVar
575579
class B: pass
576-
x = None # type: Any
580+
x: Any
577581
class 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
581585
a.f()
582586
a.g(B())
583587
a.f(a) # E: Too many arguments
@@ -586,21 +590,21 @@ a.g() # E: Too few arguments
586590
[case testMethodWithInvalidMethodAsDataAttribute]
587591
from typing import Any, Callable, ClassVar
588592
class B: pass
589-
x = None # type: Any
593+
x: Any
590594
class A:
591595
f = x # type: ClassVar[Callable[[], None]]
592596
g = x # type: ClassVar[Callable[[B], None]]
593-
a = None # type: A
597+
a: A
594598
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument
595599
a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]"
596600

597601
[case testMethodWithDynamicallyTypedMethodAsDataAttribute]
598602
from typing import Any, Callable, ClassVar
599603
class B: pass
600-
x = None # type: Any
604+
x: Any
601605
class A:
602606
f = x # type: ClassVar[Callable[[Any], Any]]
603-
a = None # type: A
607+
a: A
604608
a.f()
605609
a.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
631635
a.g()
632636
a.g(B())
633637
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" \
640644
class A:
641645
def f(self, x): pass
642646
g = f
643-
a = None # type: A
647+
a: A
644648
a.g(object())
645649
a.g(a, a) # E: Too many arguments
646650
a.g() # E: Too few arguments
@@ -652,7 +656,7 @@ class B: pass
652656
class A(Generic[t]):
653657
def f(self, x: t) -> None: pass
654658
g = f
655-
a = None # type: A[B]
659+
a: A[B]
656660
a.g(B())
657661
a.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
661665
t = TypeVar('t')
662666
class B: pass
663667
class C: pass
664-
x = None # type: Any
668+
x: Any
665669
class 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]
669673
ab.f()
670674
ac.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
674678
t = TypeVar('t')
675679
class B: pass
676680
class C: pass
677-
x = None # type: Any
681+
x: Any
678682
class 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]
682686
ab.f()
683687
ac.f()
684688

685689
[case testCallableDataAttribute]
686690
from typing import Callable, ClassVar
687691
class 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)
692696
a.f()
693697
a.g()
694698
a.f(a) # E: Too many arguments
@@ -895,7 +899,7 @@ def dec(x) -> Callable[[Any], None]: pass
895899
class A:
896900
@dec
897901
def f(self, a, b, c): pass
898-
a = None # type: A
902+
a: A
899903
a.f()
900904
a.f(None) # E: Too many arguments for "f" of "A"
901905

@@ -1945,9 +1949,9 @@ def a(f: Callable[[VarArg(int)], int]):
19451949
from typing import Callable
19461950
from 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

19521956
def 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
21402144
from typing import TypeVar, Generic, Callable
21412145

21422146
[case testRejectContravariantReturnType]
2147+
# flags: --no-strict-optional
21432148
from typing import TypeVar, Generic
21442149

21452150
t = 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
21542160
from typing import TypeVar, Generic
21552161

21562162
t = TypeVar('t', covariant=True)
21572163
class A(Generic[t]):
21582164
def foo(self) -> t:
21592165
return None
21602166
[builtins fixtures/bool.pyi]
2167+
21612168
[case testAcceptContravariantArgument]
21622169
from typing import TypeVar, Generic
21632170

0 commit comments

Comments
 (0)