Skip to content

Commit 7d1a899

Browse files
authored
Add foundation to enable --strict-optional in all test files (#15586)
1 parent 1e14d13 commit 7d1a899

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

mypy/test/helpers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import shutil
88
import sys
99
import time
10-
from typing import Any, Callable, Iterable, Iterator, Pattern
10+
from typing import Any, Callable, Collection, Iterable, Iterator, Pattern
1111

1212
# Exporting Suite as alias to TestCase for backwards compatibility
1313
# TODO: avoid aliasing - import and subclass TestCase directly
@@ -317,7 +317,10 @@ def assert_type(typ: type, value: object) -> None:
317317

318318

319319
def parse_options(
320-
program_text: str, testcase: DataDrivenTestCase, incremental_step: int
320+
program_text: str,
321+
testcase: DataDrivenTestCase,
322+
incremental_step: int,
323+
no_strict_optional_files: Collection[str] = (),
321324
) -> Options:
322325
"""Parse comments like '# flags: --foo' in a test case."""
323326
options = Options()
@@ -340,7 +343,9 @@ def parse_options(
340343
flag_list = []
341344
options = Options()
342345
# TODO: Enable strict optional in test cases by default (requires *many* test case changes)
343-
options.strict_optional = False
346+
if os.path.basename(testcase.file) in no_strict_optional_files:
347+
options.strict_optional = False
348+
344349
options.error_summary = False
345350
options.hide_error_codes = True
346351
options.force_uppercase_builtins = True

mypy/test/testcheck.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,48 @@
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-abstract.test",
55+
"check-async-await.test",
56+
"check-basic.test",
57+
"check-bound.test",
58+
"check-classes.test",
59+
"check-dynamic-typing.test",
60+
"check-enum.test",
61+
"check-expressions.test",
62+
"check-formatting.test",
63+
"check-functions.test",
64+
"check-generic-subtyping.test",
65+
"check-generics.test",
66+
"check-incremental.test",
67+
"check-inference-context.test",
68+
"check-inference.test",
69+
"check-inline-config.test",
70+
"check-isinstance.test",
71+
"check-kwargs.test",
72+
"check-lists.test",
73+
"check-literal.test",
74+
"check-modules.test",
75+
"check-namedtuple.test",
76+
"check-newsemanal.test",
77+
"check-overloading.test",
78+
"check-plugin-attrs.test",
79+
"check-protocols.test",
80+
"check-selftype.test",
81+
"check-serialize.test",
82+
"check-statements.test",
83+
"check-super.test",
84+
"check-tuples.test",
85+
"check-type-aliases.test",
86+
"check-type-checks.test",
87+
"check-typeddict.test",
88+
"check-typevar-values.test",
89+
"check-unions.test",
90+
"check-varargs.test",
91+
}
92+
93+
5294
class TypeCheckSuite(DataSuite):
5395
files = typecheck_files
5496

@@ -121,7 +163,9 @@ def run_case_once(
121163
perform_file_operations(operations)
122164

123165
# Parse options after moving files (in case mypy.ini is being moved).
124-
options = parse_options(original_program_text, testcase, incremental_step)
166+
options = parse_options(
167+
original_program_text, testcase, incremental_step, no_strict_optional_files
168+
)
125169
options.use_builtins_fixtures = True
126170
if not testcase.name.endswith("_no_incomplete"):
127171
options.enable_incomplete_feature = [TYPE_VAR_TUPLE, UNPACK]

mypy/test/testfinegrained.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
# Set to True to perform (somewhat expensive) checks for duplicate AST nodes after merge
4646
CHECK_CONSISTENCY = False
4747

48+
# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
49+
no_strict_optional_files = {"fine-grained.test", "fine-grained-suggest.test"}
50+
4851

4952
class FineGrainedSuite(DataSuite):
5053
files = find_test_files(
@@ -140,7 +143,9 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
140143

141144
def get_options(self, source: str, testcase: DataDrivenTestCase, build_cache: bool) -> Options:
142145
# This handles things like '# flags: --foo'.
143-
options = parse_options(source, testcase, incremental_step=1)
146+
options = parse_options(
147+
source, testcase, incremental_step=1, no_strict_optional_files=no_strict_optional_files
148+
)
144149
options.incremental = True
145150
options.use_builtins_fixtures = True
146151
options.show_traceback = True

0 commit comments

Comments
 (0)