Skip to content

Commit 93a0a9f

Browse files
committed
Enable strict optional for more test files (9)
1 parent ef0b763 commit 93a0a9f

File tree

4 files changed

+99
-84
lines changed

4 files changed

+99
-84
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: 30 additions & 28 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
@@ -229,10 +230,10 @@ T = TypeVar('T')
229230
S = TypeVar('S')
230231
def u(x: T, y: S) -> Union[S, T]: pass
231232

232-
a = None # type: Any
233+
a: Any
233234

234-
reveal_type(u(C(), None)) # N: Revealed type is "__main__.C"
235-
reveal_type(u(None, C())) # N: Revealed type is "__main__.C"
235+
reveal_type(u(C(), None)) # N: Revealed type is "Union[None, __main__.C]"
236+
reveal_type(u(None, C())) # N: Revealed type is "Union[__main__.C, None]"
236237

237238
reveal_type(u(C(), a)) # N: Revealed type is "Union[Any, __main__.C]"
238239
reveal_type(u(a, C())) # N: Revealed type is "Union[__main__.C, Any]"
@@ -266,7 +267,7 @@ class M(Generic[V]):
266267

267268
def f(x: M[C]) -> None:
268269
y = x.get(None)
269-
reveal_type(y) # N: Revealed type is "__main__.C"
270+
reveal_type(y) # N: Revealed type is "Union[__main__.C, None]"
270271

271272
[case testUnionSimplificationSpecialCases2]
272273
from typing import Any, TypeVar, Union
@@ -277,15 +278,15 @@ T = TypeVar('T')
277278
S = TypeVar('S')
278279
def u(x: T, y: S) -> Union[S, T]: pass
279280

280-
a = None # type: Any
281+
a: Any
281282

282283
# Base-class-Any and None, simplify
283-
reveal_type(u(C(), None)) # N: Revealed type is "__main__.C"
284-
reveal_type(u(None, C())) # N: Revealed type is "__main__.C"
284+
reveal_type(u(C(), None)) # N: Revealed type is "Union[None, __main__.C]"
285+
reveal_type(u(None, C())) # N: Revealed type is "Union[__main__.C, None]"
285286

286287
# Normal instance type and None, simplify
287-
reveal_type(u(1, None)) # N: Revealed type is "builtins.int"
288-
reveal_type(u(None, 1)) # N: Revealed type is "builtins.int"
288+
reveal_type(u(1, None)) # N: Revealed type is "Union[None, builtins.int]"
289+
reveal_type(u(None, 1)) # N: Revealed type is "Union[builtins.int, None]"
289290

290291
# Normal instance type and base-class-Any, no simplification
291292
reveal_type(u(C(), 1)) # N: Revealed type is "Union[builtins.int, __main__.C]"
@@ -317,10 +318,10 @@ S = TypeVar('S')
317318
R = TypeVar('R')
318319
def u(x: T, y: S, z: R) -> Union[R, S, T]: pass
319320

320-
a = None # type: Any
321+
a: Any
321322

322323
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"
324+
reveal_type(u(C(), C(), None)) # N: Revealed type is "Union[None, __main__.C]"
324325
reveal_type(u(a, a, 1)) # N: Revealed type is "Union[builtins.int, Any]"
325326
reveal_type(u(a, C(), a)) # N: Revealed type is "Union[Any, __main__.C]"
326327
reveal_type(u('', 1, 1)) # N: Revealed type is "Union[builtins.int, builtins.str]"
@@ -370,9 +371,9 @@ T = TypeVar('T')
370371
S = TypeVar('S')
371372
def u(x: T, y: S) -> Union[S, T]: pass
372373

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

377378
# Two identical items
378379
reveal_type(u(t_o, t_o)) # N: Revealed type is "Type[builtins.object]"
@@ -397,10 +398,10 @@ T = TypeVar('T')
397398
S = TypeVar('S')
398399
def u(x: T, y: S) -> Union[S, T]: pass
399400

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
401+
t_o: Type[object]
402+
t_s: Type[str]
403+
t_a: Type[Any]
404+
t: type
404405

405406
# Union with object
406407
reveal_type(u(t_o, object())) # N: Revealed type is "builtins.object"
@@ -1010,6 +1011,7 @@ MYTYPE = List[Union[str, "MYTYPE"]] # E: Cannot resolve name "MYTYPE" (possible
10101011
[builtins fixtures/list.pyi]
10111012

10121013
[case testNonStrictOptional]
1014+
# flags: --no-strict-optional
10131015
from typing import Optional, List
10141016

10151017
def union_test1(x):

0 commit comments

Comments
 (0)