Allow adding test cases testing version-dependent Python features (#9238)

This commit is contained in:
Alex Waygood
2022-11-21 16:03:28 +00:00
committed by GitHub
parent c161827d6d
commit 1f8491d23d
2 changed files with 28 additions and 1 deletions

View File

@@ -93,3 +93,17 @@ extensions, and since the tests need to pass on all Python versions >=3.7, PEP
PEP 585 syntax can also not be used in the `test_cases` directory. Use
`typing.Tuple` instead of `tuple`, `typing.Callable` instead of
`collections.abc.Callable`, and `typing.Match` instead of `re.Match` (etc.).
### Version-dependent tests
Some tests will only pass on mypy
with a specific Python version passed on the command line to the `tests/regr_test.py` script.
To mark a test-case file as being skippable on lower versions of Python,
append `-py3*` to the filename.
For example, if `foo` is a stdlib feature that's new in Python 3.9,
test cases for `foo` should be put in a file named `test_cases/stdlib/check_foo-py39.py`.
This means that mypy will only run the test case
if `--python-version 3.9`, `--python-version 3.10` or `--python-version 3.11`
is passed on the command line to `tests/regr_test.py`,
but it *won't* run the test case if `--python-version 3.7` or `--python-version 3.8`
is passed on the command line.

View File

@@ -5,6 +5,7 @@ from __future__ import annotations
import argparse
import os
import re
import shutil
import subprocess
import sys
@@ -130,7 +131,19 @@ def test_testcase_directory(package: PackageInfo, version: str, platform: str) -
shutil.copytree(Path("stubs", requirement), new_typeshed / "stubs" / requirement)
env_vars["MYPYPATH"] = os.pathsep.join(map(str, new_typeshed.glob("stubs/*")))
flags.extend(["--custom-typeshed-dir", str(td_path / "typeshed")])
result = subprocess.run([sys.executable, "-m", "mypy", new_test_case_dir, *flags], capture_output=True, env=env_vars)
# If the test-case filename ends with -py39,
# only run the test if --python-version was set to 3.9 or higher (for example)
for path in new_test_case_dir.rglob("*.py"):
if match := re.fullmatch(r".*-py3(\d{1,2})", path.stem):
minor_version_required = int(match[1])
assert f"3.{minor_version_required}" in SUPPORTED_VERSIONS
if minor_version_required <= int(version.split(".")[1]):
flags.append(str(path))
else:
flags.append(str(path))
result = subprocess.run([sys.executable, "-m", "mypy", *flags], capture_output=True, env=env_vars)
if result.returncode:
print_error("failure\n")