Skip to content

pytype_test: Mark typeshed-external dependencies as missing. #9486

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 6 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ packaging==22.0
pathspec
pycln==2.1.2 # must match .pre-commit-config.yaml
pyyaml==6.0
pytype==2022.12.15; platform_system != "Windows" and python_version < "3.11"
pytype==2023.1.10; platform_system != "Windows" and python_version < "3.11"
termcolor>=2
tomli==2.0.1
tomlkit==0.11.6
types-pyyaml
types-setuptools
typing-extensions
41 changes: 37 additions & 4 deletions tests/pytype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import os
import sys
import traceback
from collections.abc import Sequence
from collections.abc import Iterable, Sequence

import pkg_resources
import utils
from pytype import config as pytype_config, load_pytd # type: ignore[import]
from pytype.imports import typeshed # type: ignore[import]

Expand Down Expand Up @@ -56,11 +58,13 @@ def create_parser() -> argparse.ArgumentParser:
return parser


def run_pytype(*, filename: str, python_version: str) -> str | None:
def run_pytype(*, filename: str, python_version: str, missing_modules: Iterable[str]) -> str | None:
"""Runs pytype, returning the stderr if any."""
if python_version not in _LOADERS:
options = pytype_config.Options.create("", parse_pyi=True, python_version=python_version)
loader = load_pytd.create_loader(options)
# For simplicity, pretends missing modules are part of the stdlib.
missing_modules = tuple(os.path.join("stdlib", m) for m in missing_modules)
loader = load_pytd.create_loader(options, missing_modules)
_LOADERS[python_version] = (options, loader)
options, loader = _LOADERS[python_version]
stderr: str | None
Expand Down Expand Up @@ -131,14 +135,43 @@ def find_stubs_in_paths(paths: Sequence[str]) -> list[str]:
return filenames


def get_missing_modules(files_to_test: Sequence[str]) -> Iterable[str]:
"""Gets module names provided by typeshed-external dependencies.

Some typeshed stubs depend on dependencies outside of typeshed. Since pytype
isn't able to read such dependencies, we instead declare them as "missing"
modules, so that no errors are reported for them.
"""
stub_distributions = set()
for fi in files_to_test:
parts = fi.split(os.sep)
try:
idx = parts.index("stubs")
except ValueError:
continue
stub_distributions.add(parts[idx + 1])
missing_modules = set()
for distribution in stub_distributions:
for pkg in utils.read_dependencies(distribution).external_pkgs:
# See https://stackoverflow.com/a/54853084
top_level_file = os.path.join(pkg_resources.get_distribution(pkg).egg_info, "top_level.txt") # type: ignore[attr-defined]
with open(top_level_file) as f:
missing_modules.update(f.read().splitlines())
return missing_modules


def run_all_tests(*, files_to_test: Sequence[str], print_stderr: bool, dry_run: bool) -> None:
bad = []
errors = 0
total_tests = len(files_to_test)
missing_modules = get_missing_modules(files_to_test)
print("Testing files with pytype...")
for i, f in enumerate(files_to_test):
python_version = "{0.major}.{0.minor}".format(sys.version_info)
stderr = run_pytype(filename=f, python_version=python_version) if not dry_run else None
if dry_run:
stderr = None
else:
stderr = run_pytype(filename=f, python_version=python_version, missing_modules=missing_modules)
if stderr:
if print_stderr:
print(f"\n{stderr}")
Expand Down