Skip to content

mypy_test.py: Add command-line argument to enable selecting which parts of the test to run #8143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@

SUPPORTED_VERSIONS = [(3, 11), (3, 10), (3, 9), (3, 8), (3, 7), (3, 6), (2, 7)]
SUPPORTED_PLATFORMS = frozenset({"linux", "win32", "darwin"})
TYPESHED_DIRECTORIES = frozenset({"stdlib", "stubs", "tests", "test_cases", "scripts"})

MajorVersion: TypeAlias = int
MinorVersion: TypeAlias = int
Platform: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_PLATFORMS"]
Directory: TypeAlias = Annotated[str, "Must be one of the entries in TYPESHED_DIRECTORIES"]


def python_version(arg: str) -> tuple[MajorVersion, MinorVersion]:
Expand All @@ -50,11 +52,18 @@ def python_platform(platform: str) -> str:
return platform


def typeshed_directory(directory: str) -> str:
if directory not in TYPESHED_DIRECTORIES:
raise ValueError
return directory


class CommandLineArgs(argparse.Namespace):
verbose: int
dry_run: bool
exclude: list[str] | None
python_version: list[tuple[MajorVersion, MinorVersion]] | None
dir: list[Directory] | None
platform: list[Platform] | None
filter: list[str]

Expand All @@ -66,6 +75,14 @@ class CommandLineArgs(argparse.Namespace):
parser.add_argument(
"-p", "--python-version", type=python_version, nargs="*", action="extend", help="These versions only (major[.minor])"
)
parser.add_argument(
"-d",
"--dir",
type=typeshed_directory,
nargs="*",
action="extend",
help="Test only these top-level typeshed directories (defaults to all typeshed directories)",
)
parser.add_argument(
"--platform",
type=python_platform,
Expand All @@ -85,6 +102,7 @@ class TestConfig:
exclude: list[str] | None
major: MajorVersion
minor: MinorVersion
directories: frozenset[Directory]
platform: Platform
filter: list[str]

Expand Down Expand Up @@ -477,41 +495,47 @@ def test_the_test_cases(code: int, args: TestConfig) -> TestResults:
def test_typeshed(code: int, args: TestConfig) -> TestResults:
print(f"*** Testing Python {args.major}.{args.minor} on {args.platform}")
files_checked_this_version = 0
code, stdlib_files_checked = test_stdlib(code, args)
files_checked_this_version += stdlib_files_checked
print()
if "stdlib" in args.directories:
code, stdlib_files_checked = test_stdlib(code, args)
files_checked_this_version += stdlib_files_checked
print()

if args.major == 2:
return TestResults(code, files_checked_this_version)

code, third_party_files_checked = test_third_party_stubs(code, args)
files_checked_this_version += third_party_files_checked
print()
if "stubs" in args.directories:
code, third_party_files_checked = test_third_party_stubs(code, args)
files_checked_this_version += third_party_files_checked
print()

if args.minor >= 9:
# Run mypy against our own test suite and the scripts directory
#
# Skip this on earlier Python versions,
# as we're using new syntax and new functions in some test files
code, test_script_files_checked = test_the_test_scripts(code, args)
files_checked_this_version += test_script_files_checked
if "tests" in args.directories:
code, test_script_files_checked = test_the_test_scripts(code, args)
files_checked_this_version += test_script_files_checked
print()

if "scripts" in args.directories:
code, script_files_checked = test_scripts_directory(code, args)
files_checked_this_version += script_files_checked
print()

if "test_cases" in args.directories:
code, test_case_files_checked = test_the_test_cases(code, args)
files_checked_this_version += test_case_files_checked
print()

code, script_files_checked = test_scripts_directory(code, args)
files_checked_this_version += script_files_checked
print()

code, test_case_files_checked = test_the_test_cases(code, args)
files_checked_this_version += test_case_files_checked
print()

return TestResults(code, files_checked_this_version)


def main() -> None:
args = parser.parse_args(namespace=CommandLineArgs())
versions = args.python_version or SUPPORTED_VERSIONS
platforms = args.platform or [sys.platform]
tested_directories = frozenset(args.dir) if args.dir else TYPESHED_DIRECTORIES
code = 0
total_files_checked = 0
for (major, minor), platform in product(versions, platforms):
Expand All @@ -521,6 +545,7 @@ def main() -> None:
exclude=args.exclude,
major=major,
minor=minor,
directories=tested_directories,
platform=platform,
filter=args.filter,
)
Expand Down