|
15 | 15 | from __future__ import annotations |
16 | 16 |
|
17 | 17 | import argparse |
| 18 | +import importlib.metadata |
18 | 19 | import os |
19 | 20 | import sys |
20 | 21 | import traceback |
| 22 | +from collections import defaultdict |
21 | 23 | from collections.abc import Iterable, Sequence |
22 | 24 |
|
23 | | -import pkg_resources |
| 25 | +from packaging.requirements import Requirement |
24 | 26 |
|
25 | 27 | from parse_metadata import read_dependencies |
26 | 28 |
|
27 | 29 | if sys.platform == "win32": |
28 | 30 | print("pytype does not support Windows.", file=sys.stderr) |
29 | 31 | sys.exit(1) |
30 | | -if sys.version_info >= (3, 11): |
31 | | - print("pytype does not support Python 3.11+ yet.", file=sys.stderr) |
| 32 | +if sys.version_info[:2] != (3, 10): |
| 33 | + print("pytype_test.py can currently only be run on Python 3.10.", file=sys.stderr) |
32 | 34 | sys.exit(1) |
33 | 35 |
|
34 | 36 | # pytype is not py.typed https://github.com/google/pytype/issues/1325 |
@@ -165,15 +167,18 @@ def get_missing_modules(files_to_test: Sequence[str]) -> Iterable[str]: |
165 | 167 | except ValueError: |
166 | 168 | continue |
167 | 169 | stub_distributions.add(parts[idx + 1]) |
| 170 | + |
| 171 | + dist_to_pkg_map = defaultdict(list) |
| 172 | + for dist, pkg_list in importlib.metadata.packages_distributions().items(): |
| 173 | + for pkg in pkg_list: |
| 174 | + dist_to_pkg_map[pkg].append(dist) |
| 175 | + |
168 | 176 | missing_modules = set() |
169 | 177 | for distribution in stub_distributions: |
170 | | - for pkg in read_dependencies(distribution).external_pkgs: |
171 | | - egg_info = pkg_resources.get_distribution(pkg).egg_info |
172 | | - assert isinstance(egg_info, str) |
173 | | - # See https://stackoverflow.com/a/54853084 |
174 | | - top_level_file = os.path.join(egg_info, "top_level.txt") |
175 | | - with open(top_level_file) as f: |
176 | | - missing_modules.update(f.read().splitlines()) |
| 178 | + for external_req in read_dependencies(distribution).external_pkgs: |
| 179 | + pkg = Requirement(external_req).name |
| 180 | + missing_modules.update(dist_to_pkg_map[pkg]) |
| 181 | + |
177 | 182 | test_dir = os.path.dirname(__file__) |
178 | 183 | exclude_list = os.path.join(test_dir, "pytype_exclude_list.txt") |
179 | 184 | with open(exclude_list) as f: |
|
0 commit comments