Skip to content

Commit

Permalink
Add foundation to enable --strict-optional in all test files (#15586)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Jul 4, 2023
1 parent 1e14d13 commit 7d1a899
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
11 changes: 8 additions & 3 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import shutil
import sys
import time
from typing import Any, Callable, Iterable, Iterator, Pattern
from typing import Any, Callable, Collection, Iterable, Iterator, Pattern

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


def parse_options(
program_text: str, testcase: DataDrivenTestCase, incremental_step: int
program_text: str,
testcase: DataDrivenTestCase,
incremental_step: int,
no_strict_optional_files: Collection[str] = (),
) -> Options:
"""Parse comments like '# flags: --foo' in a test case."""
options = Options()
Expand All @@ -340,7 +343,9 @@ def parse_options(
flag_list = []
options = Options()
# TODO: Enable strict optional in test cases by default (requires *many* test case changes)
options.strict_optional = False
if os.path.basename(testcase.file) in no_strict_optional_files:
options.strict_optional = False

options.error_summary = False
options.hide_error_codes = True
options.force_uppercase_builtins = True
Expand Down
46 changes: 45 additions & 1 deletion mypy/test/testcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,48 @@
typecheck_files.remove("check-modules-case.test")


# TODO: Enable strict optional in test cases by default. Remove files here, once test cases are updated
no_strict_optional_files = {
"check-abstract.test",
"check-async-await.test",
"check-basic.test",
"check-bound.test",
"check-classes.test",
"check-dynamic-typing.test",
"check-enum.test",
"check-expressions.test",
"check-formatting.test",
"check-functions.test",
"check-generic-subtyping.test",
"check-generics.test",
"check-incremental.test",
"check-inference-context.test",
"check-inference.test",
"check-inline-config.test",
"check-isinstance.test",
"check-kwargs.test",
"check-lists.test",
"check-literal.test",
"check-modules.test",
"check-namedtuple.test",
"check-newsemanal.test",
"check-overloading.test",
"check-plugin-attrs.test",
"check-protocols.test",
"check-selftype.test",
"check-serialize.test",
"check-statements.test",
"check-super.test",
"check-tuples.test",
"check-type-aliases.test",
"check-type-checks.test",
"check-typeddict.test",
"check-typevar-values.test",
"check-unions.test",
"check-varargs.test",
}


class TypeCheckSuite(DataSuite):
files = typecheck_files

Expand Down Expand Up @@ -121,7 +163,9 @@ def run_case_once(
perform_file_operations(operations)

# Parse options after moving files (in case mypy.ini is being moved).
options = parse_options(original_program_text, testcase, incremental_step)
options = parse_options(
original_program_text, testcase, incremental_step, no_strict_optional_files
)
options.use_builtins_fixtures = True
if not testcase.name.endswith("_no_incomplete"):
options.enable_incomplete_feature = [TYPE_VAR_TUPLE, UNPACK]
Expand Down
7 changes: 6 additions & 1 deletion mypy/test/testfinegrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
# Set to True to perform (somewhat expensive) checks for duplicate AST nodes after merge
CHECK_CONSISTENCY = False

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


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

def get_options(self, source: str, testcase: DataDrivenTestCase, build_cache: bool) -> Options:
# This handles things like '# flags: --foo'.
options = parse_options(source, testcase, incremental_step=1)
options = parse_options(
source, testcase, incremental_step=1, no_strict_optional_files=no_strict_optional_files
)
options.incremental = True
options.use_builtins_fixtures = True
options.show_traceback = True
Expand Down

0 comments on commit 7d1a899

Please sign in to comment.