Skip to content

Commit

Permalink
Filter pytype tests by stdlib/VERSIONS file (#12649)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
frigus02 authored Sep 12, 2024
1 parent bc6b3b2 commit d326c9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
8 changes: 8 additions & 0 deletions tests/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
11 changes: 1 addition & 10 deletions tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
PYTHON_VERSION,
STDLIB_PATH,
TESTS_DIR,
SupportedVersionsDict,
VersionTuple,
colored,
get_gitignore_spec,
get_mypy_req,
parse_stdlib_versions_file,
print_error,
print_success_msg,
spec_matches_path,
supported_versions_for_module,
venv_python,
)

Expand Down Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions tests/pytype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from packaging.requirements import Requirement

from _metadata import read_dependencies
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)
Expand Down Expand Up @@ -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
Expand All @@ -149,6 +153,15 @@ 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 _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")
Expand Down Expand Up @@ -208,9 +221,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:
Expand Down

0 comments on commit d326c9b

Please sign in to comment.