Skip to content

Commit d106c84

Browse files
authored
Enable strict optional for more test files (9) (#15607)
Followup to #15586 This will be the last one 🚀
1 parent ef0b763 commit d106c84

File tree

4 files changed

+93
-76
lines changed

4 files changed

+93
-76
lines changed

mypy/test/testcheck.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@
4949
typecheck_files.remove("check-modules-case.test")
5050

5151

52-
# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
53-
no_strict_optional_files = {
54-
"check-modules.test",
55-
"check-unions.test",
56-
"check-varargs.test",
57-
}
58-
59-
6052
class TypeCheckSuite(DataSuite):
6153
files = typecheck_files
6254

@@ -129,9 +121,7 @@ def run_case_once(
129121
perform_file_operations(operations)
130122

131123
# Parse options after moving files (in case mypy.ini is being moved).
132-
options = parse_options(
133-
original_program_text, testcase, incremental_step, no_strict_optional_files
134-
)
124+
options = parse_options(original_program_text, testcase, incremental_step)
135125
options.use_builtins_fixtures = True
136126
if not testcase.name.endswith("_no_incomplete"):
137127
options.enable_incomplete_feature = [TYPE_VAR_TUPLE, UNPACK]

test-data/unit/check-modules.test

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ x = object()
180180
[case testChainedAssignmentAndImports]
181181
import m
182182

183-
i, s = None, None # type: (int, str)
183+
i: int
184+
s: str
184185
if int():
185186
i = m.x
186187
if int():
@@ -585,6 +586,7 @@ x = 1+0
585586

586587

587588
[case testConditionalImportAndAssign]
589+
# flags: --no-strict-optional
588590
try:
589591
from m import x
590592
except:
@@ -676,6 +678,7 @@ class B(A): ...
676678

677679

678680
[case testImportVariableAndAssignNone]
681+
# flags: --no-strict-optional
679682
try:
680683
from m import x
681684
except:
@@ -684,6 +687,7 @@ except:
684687
x = 1
685688

686689
[case testImportFunctionAndAssignNone]
690+
# flags: --no-strict-optional
687691
try:
688692
from m import f
689693
except:
@@ -709,6 +713,7 @@ except:
709713
def f(): pass
710714

711715
[case testAssignToFuncDefViaGlobalDecl2]
716+
# flags: --no-strict-optional
712717
import typing
713718
from m import f
714719
def g() -> None:
@@ -721,6 +726,7 @@ def f(): pass
721726
[out]
722727

723728
[case testAssignToFuncDefViaNestedModules]
729+
# flags: --no-strict-optional
724730
import m.n
725731
m.n.f = None
726732
m.n.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
@@ -730,6 +736,7 @@ def f(): pass
730736
[out]
731737

732738
[case testAssignToFuncDefViaModule]
739+
# flags: --no-strict-optional
733740
import m
734741
m.f = None
735742
m.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
@@ -738,6 +745,7 @@ def f(): pass
738745
[out]
739746

740747
[case testConditionalImportAndAssignNoneToModule]
748+
# flags: --no-strict-optional
741749
if object():
742750
import m
743751
else:
@@ -758,6 +766,7 @@ else:
758766
[out]
759767

760768
[case testImportAndAssignToModule]
769+
# flags: --no-strict-optional
761770
import m
762771
m = None
763772
m.f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
@@ -930,8 +939,8 @@ tmp/a/b/__init__.py:3: error: Name "a" is not defined
930939

931940
[case testSubmoduleMixingLocalAndQualifiedNames]
932941
from a.b import MyClass
933-
val1 = None # type: a.b.MyClass # E: Name "a" is not defined
934-
val2 = None # type: MyClass
942+
val1: a.b.MyClass # E: Name "a" is not defined
943+
val2: MyClass
935944

936945
[file a/__init__.py]
937946
[file a/b.py]
@@ -1501,7 +1510,7 @@ import bar
15011510
from foo import *
15021511
def bar(y: AnyAlias) -> None: pass
15031512

1504-
l = None # type: ListAlias[int]
1513+
l: ListAlias[int]
15051514
reveal_type(l)
15061515

15071516
[file foo.py]
@@ -1532,7 +1541,7 @@ Row = Dict[str, int]
15321541

15331542
[case testImportStarAliasGeneric]
15341543
from y import *
1535-
notes = None # type: G[X]
1544+
notes: G[X]
15361545
another = G[X]()
15371546
second = XT[str]()
15381547
last = XT[G]()
@@ -1561,7 +1570,7 @@ from typing import Any
15611570
def bar(x: Any, y: AnyCallable) -> Any:
15621571
return 'foo'
15631572

1564-
cb = None # type: AnyCallable
1573+
cb: AnyCallable
15651574
reveal_type(cb) # N: Revealed type is "def (*Any, **Any) -> Any"
15661575

15671576
[file foo.py]

test-data/unit/check-unions.test

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ class B: y = 2
5555
class C: pass
5656
class D: pass
5757

58-
u = None # type: Union[A, C, D]
59-
v = None # type: Union[C, D]
60-
w = None # type: Union[A, B]
61-
x = None # type: Union[A, C]
62-
y = None # type: int
63-
z = None # type: str
58+
u: Union[A, C, D]
59+
v: Union[C, D]
60+
w: Union[A, B]
61+
x: Union[A, C]
62+
y: int
63+
z: str
6464

6565
if int():
6666
y = w.y
@@ -89,9 +89,9 @@ class B:
8989
class C:
9090
def foo(self) -> str: pass
9191

92-
x = None # type: Union[A, B]
93-
y = None # type: Union[A, C]
94-
i = None # type: int
92+
x: Union[A, B]
93+
y: Union[A, C]
94+
i: int
9595

9696
x.foo()
9797
y.foo()
@@ -103,7 +103,7 @@ if int():
103103

104104
[case testUnionIndexing]
105105
from typing import Union, List
106-
x = None # type: Union[List[int], str]
106+
x: Union[List[int], str]
107107
x[2]
108108
x[2] + 1 # E: Unsupported operand types for + ("str" and "int") \
109109
# N: Left operand is of type "Union[int, str]"
@@ -132,6 +132,7 @@ def f(x: Union[int, str]) -> int: pass
132132
def f(x: type) -> str: pass
133133

134134
[case testUnionWithNoneItem]
135+
# flags: --no-strict-optional
135136
from typing import Union
136137
def f() -> Union[int, None]: pass
137138
x = 1
@@ -221,6 +222,7 @@ else:
221222
# N: def g(x: Union[int, str]) -> None
222223

223224
[case testUnionSimplificationSpecialCases]
225+
# flags: --no-strict-optional
224226
from typing import Any, TypeVar, Union
225227

226228
class C(Any): pass
@@ -266,9 +268,10 @@ class M(Generic[V]):
266268

267269
def f(x: M[C]) -> None:
268270
y = x.get(None)
269-
reveal_type(y) # N: Revealed type is "__main__.C"
271+
reveal_type(y) # N: Revealed type is "Union[__main__.C, None]"
270272

271273
[case testUnionSimplificationSpecialCases2]
274+
# flags: --no-strict-optional
272275
from typing import Any, TypeVar, Union
273276

274277
class C(Any): pass
@@ -317,10 +320,10 @@ S = TypeVar('S')
317320
R = TypeVar('R')
318321
def u(x: T, y: S, z: R) -> Union[R, S, T]: pass
319322

320-
a = None # type: Any
323+
a: Any
321324

322325
reveal_type(u(1, 1, 1)) # N: Revealed type is "builtins.int"
323-
reveal_type(u(C(), C(), None)) # N: Revealed type is "__main__.C"
326+
reveal_type(u(C(), C(), None)) # N: Revealed type is "Union[None, __main__.C]"
324327
reveal_type(u(a, a, 1)) # N: Revealed type is "Union[builtins.int, Any]"
325328
reveal_type(u(a, C(), a)) # N: Revealed type is "Union[Any, __main__.C]"
326329
reveal_type(u('', 1, 1)) # N: Revealed type is "Union[builtins.int, builtins.str]"
@@ -370,9 +373,9 @@ T = TypeVar('T')
370373
S = TypeVar('S')
371374
def u(x: T, y: S) -> Union[S, T]: pass
372375

373-
t_o = None # type: Type[object]
374-
t_s = None # type: Type[str]
375-
t_a = None # type: Type[Any]
376+
t_o: Type[object]
377+
t_s: Type[str]
378+
t_a: Type[Any]
376379

377380
# Two identical items
378381
reveal_type(u(t_o, t_o)) # N: Revealed type is "Type[builtins.object]"
@@ -397,10 +400,10 @@ T = TypeVar('T')
397400
S = TypeVar('S')
398401
def u(x: T, y: S) -> Union[S, T]: pass
399402

400-
t_o = None # type: Type[object]
401-
t_s = None # type: Type[str]
402-
t_a = None # type: Type[Any]
403-
t = None # type: type
403+
t_o: Type[object]
404+
t_s: Type[str]
405+
t_a: Type[Any]
406+
t: type
404407

405408
# Union with object
406409
reveal_type(u(t_o, object())) # N: Revealed type is "builtins.object"
@@ -1010,6 +1013,7 @@ MYTYPE = List[Union[str, "MYTYPE"]] # E: Cannot resolve name "MYTYPE" (possible
10101013
[builtins fixtures/list.pyi]
10111014

10121015
[case testNonStrictOptional]
1016+
# flags: --no-strict-optional
10131017
from typing import Optional, List
10141018

10151019
def union_test1(x):

0 commit comments

Comments
 (0)