From c2fbc9cace8bbf2523c7c7057420c2c18f2d53ae Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Thu, 12 Sep 2024 13:13:13 +0000 Subject: [PATCH 1/2] Filter pytype tests by stdlib/VERSIONS file Filter the files to run pytype tests on by stdlib/VERSIONS file. This becomes important for Python 3.12, where e.g. checking asynchat.pyi requires asyncore.pyi, both of which have been removed in 3.12. The implementation was copied from mypy_test.py and adapted to fit into the existing pytype test setup. --- tests/pytype_test.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/tests/pytype_test.py b/tests/pytype_test.py index 05be3d8dc74c..cb455a3562b2 100755 --- a/tests/pytype_test.py +++ b/tests/pytype_test.py @@ -25,6 +25,7 @@ from packaging.requirements import Requirement from _metadata import read_dependencies +from _utils import SupportedVersionsDict, VersionTuple, parse_stdlib_versions_file if sys.platform == "win32": print("pytype does not support Windows.", file=sys.stderr) @@ -123,16 +124,19 @@ def check_subdirs_discoverable(subdir_paths: list[str]) -> None: def determine_files_to_test(*, paths: Sequence[str]) -> list[str]: - """Determine all files to test, checking if it's in the exclude list and which Python versions to use. + """Determine all files to test. - Returns a list of pairs of the file path and Python version as an int.""" + Checks for files in the pytype exclude list and for the stdlib VERSIONS file. + """ filenames = find_stubs_in_paths(paths) ts = typeshed.Typeshed() - skipped = set(ts.read_blacklist()) + exclude_list = set(ts.read_blacklist()) + stdlib_module_versions = parse_stdlib_versions_file() files = [] for f in sorted(filenames): - rel = _get_relative(f) - if rel in skipped: + if _get_relative(f) in exclude_list: + continue + if not _is_supported_stdlib_version(stdlib_module_versions, f): continue files.append(f) return files @@ -149,6 +153,23 @@ def find_stubs_in_paths(paths: Sequence[str]) -> list[str]: return filenames +def _is_supported_stdlib_version(module_versions: SupportedVersionsDict, filename: str) -> bool: + parts = _get_relative(filename).split(os.path.sep) + if parts[0] != "stdlib": + return True + module_name = _get_module_name(filename) + min_version, max_version = _supported_versions_for_module(module_versions, module_name) + return min_version <= sys.version_info <= max_version + + +def _supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]: + while "." in module_name: + if module_name in module_versions: + return module_versions[module_name] + module_name = ".".join(module_name.split(".")[:-1]) + return module_versions[module_name] + + def _get_pkgs_associated_with_requirement(req_name: str) -> list[str]: dist = importlib.metadata.distribution(req_name) toplevel_txt_contents = dist.read_text("top_level.txt") @@ -208,9 +229,9 @@ def run_all_tests(*, files_to_test: Sequence[str], print_stderr: bool, dry_run: errors = 0 total_tests = len(files_to_test) missing_modules = get_missing_modules(files_to_test) + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" print("Testing files with pytype...") for i, f in enumerate(files_to_test): - python_version = f"{sys.version_info.major}.{sys.version_info.minor}" if dry_run: stderr = None else: From cc97e9c61a45448473b0976f785985938e7b3fdb Mon Sep 17 00:00:00 2001 From: Jan Kuehle Date: Thu, 12 Sep 2024 15:08:08 +0000 Subject: [PATCH 2/2] Move supported_versions_for_module to tests._utils --- tests/_utils.py | 8 ++++++++ tests/mypy_test.py | 11 +---------- tests/pytype_test.py | 12 ++---------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/tests/_utils.py b/tests/_utils.py index fab6793f7ffe..2879ee4864e9 100644 --- a/tests/_utils.py +++ b/tests/_utils.py @@ -132,6 +132,14 @@ def parse_stdlib_versions_file() -> SupportedVersionsDict: return result +def supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]: + while "." in module_name: + if module_name in module_versions: + return module_versions[module_name] + module_name = ".".join(module_name.split(".")[:-1]) + return module_versions[module_name] + + def _parse_version(v_str: str) -> tuple[int, int]: m = VERSION_RE.match(v_str) assert m, f"invalid version: {v_str}" diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 35a7fdab1153..e1b10fc8a7c8 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -26,8 +26,6 @@ PYTHON_VERSION, STDLIB_PATH, TESTS_DIR, - SupportedVersionsDict, - VersionTuple, colored, get_gitignore_spec, get_mypy_req, @@ -35,6 +33,7 @@ print_error, print_success_msg, spec_matches_path, + supported_versions_for_module, venv_python, ) @@ -375,14 +374,6 @@ def stdlib_module_name_from_path(path: Path) -> str: return ".".join(parts) -def supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]: - while "." in module_name: - if module_name in module_versions: - return module_versions[module_name] - module_name = ".".join(module_name.split(".")[:-1]) - return module_versions[module_name] - - @dataclass class TestSummary: mypy_result: MypyResult = MypyResult.SUCCESS diff --git a/tests/pytype_test.py b/tests/pytype_test.py index cb455a3562b2..6f9e40d070c9 100755 --- a/tests/pytype_test.py +++ b/tests/pytype_test.py @@ -25,7 +25,7 @@ from packaging.requirements import Requirement from _metadata import read_dependencies -from _utils import SupportedVersionsDict, VersionTuple, parse_stdlib_versions_file +from _utils import SupportedVersionsDict, parse_stdlib_versions_file, supported_versions_for_module if sys.platform == "win32": print("pytype does not support Windows.", file=sys.stderr) @@ -158,18 +158,10 @@ def _is_supported_stdlib_version(module_versions: SupportedVersionsDict, filenam if parts[0] != "stdlib": return True module_name = _get_module_name(filename) - min_version, max_version = _supported_versions_for_module(module_versions, module_name) + min_version, max_version = supported_versions_for_module(module_versions, module_name) return min_version <= sys.version_info <= max_version -def _supported_versions_for_module(module_versions: SupportedVersionsDict, module_name: str) -> tuple[VersionTuple, VersionTuple]: - while "." in module_name: - if module_name in module_versions: - return module_versions[module_name] - module_name = ".".join(module_name.split(".")[:-1]) - return module_versions[module_name] - - def _get_pkgs_associated_with_requirement(req_name: str) -> list[str]: dist = importlib.metadata.distribution(req_name) toplevel_txt_contents = dist.read_text("top_level.txt")