8000 pytype_test: Mark typeshed-external dependencies as missing. (#9486) · python/typeshed@622a96b · GitHub
[go: up one dir, main page]

Skip to content

Commit 622a96b

Browse files
rchen152Avasam
andauthored
pytype_test: Mark typeshed-external dependencies as missing. (#9486)
Co-authored-by: Avasam <samuel.06@hotmail.com>
1 parent 3c24501 commit 622a96b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

requirements-tests.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ packaging==22.0
1010
pathspec
1111
pycln==2.1.2 # must match .pre-commit-config.yaml
1212
pyyaml==6.0
13-
pytype==2022.12.15; platform_system != "Windows" and python_version < "3.11"
13+
pytype==2023.1.10; platform_system != "Windows" and python_version < "3.11"
1414
termcolor>=2
1515
tomli==2.0.1
1616
tomlkit==0.11.6
1717
types-pyyaml
18+
types-setuptools
1819
typing-extensions

tests/pytype_test.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import os
1717
import sys
1818
import traceback
19-
from collections.abc import Sequence
19+
from collections.abc import Iterable, Sequence
2020

21+
import pkg_resources
22+
import utils
2123
from pytype import config as pytype_config, load_pytd # type: ignore[import]
2224
from pytype.imports import typeshed # type: ignore[import]
2325

@@ -56,11 +58,13 @@ def create_parser() -> argparse.ArgumentParser:
5658
return parser
5759

5860

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

133137

138+
def get_missing_modules(files_to_test: Sequence[str]) -> Iterable[str]:
139+
"""Gets module names provided by typeshed-external dependencies.
140+
141+
Some typeshed stubs depend on dependencies outside of typeshed. Since pytype
142+
isn't able to read such dependencies, we instead declare them as "missing"
143+
modules, so that no errors are reported for them.
144+
"""
145+
stub_distributions = set()
146+
for fi in files_to_test:
147+
parts = fi.split(os.sep)
148+
try:
149+
idx = parts.index("stubs")
150+
except ValueError:
151+
continue
152+
stub_distributions.add(parts[idx + 1])
153+
missing_modules = set()
154+
for distribution in stub_distributions:
155+
for pkg in utils.read_dependencies(distribution).external_pkgs:
156+
# See https://stackoverflow.com/a/54853084
157+
top_level_file = os.path.join(pkg_resources.get_distribution(pkg).egg_info, "top_level.txt") # type: ignore[attr-defined]
158+
with open(top_level_file) as f:
159+
missing_modules.update(f.read().splitlines())
160+
return missing_modules
161+
162+
134163
def run_all_tests(*, files_to_test: Sequence[str], print_stderr: bool, dry_run: bool) -> None:
135164
bad = []
136165
errors = 0
137166
total_tests = len(files_to_test)
167+
missing_modules = get_missing_modules(files_to_test)
138168
print("Testing files with pytype...")
139169
for i, f in enumerate(files_to_test):
140170
python_version = "{0.major}.{0.minor}".format(sys.version_info)
141-
stderr = run_pytype(filename=f, python_version=python_version) if not dry_run else None
171+
if dry_run:
172+
stderr = None
173+
else:
174+
stderr = run_pytype(filename=f, python_version=python_version, missing_modules=missing_modules)
142175
if stderr:
143176
if print_stderr:
144177
print(f"\n{stderr}")

0 commit comments

Comments
 (0)
0