mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-07 20:54:28 +08:00
pytype_test.py: Use importlib.metadata instead of pkg_resources (#10391)
This commit is contained in:
@@ -15,20 +15,22 @@ will also discover incorrect usage of imported modules.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import importlib.metadata
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
from collections import defaultdict
|
||||
from collections.abc import Iterable, Sequence
|
||||
|
||||
import pkg_resources
|
||||
from packaging.requirements import Requirement
|
||||
|
||||
from parse_metadata import read_dependencies
|
||||
|
||||
if sys.platform == "win32":
|
||||
print("pytype does not support Windows.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if sys.version_info >= (3, 11):
|
||||
print("pytype does not support Python 3.11+ yet.", file=sys.stderr)
|
||||
if sys.version_info[:2] != (3, 10):
|
||||
print("pytype_test.py can currently only be run on Python 3.10.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
# 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]:
|
||||
except ValueError:
|
||||
continue
|
||||
stub_distributions.add(parts[idx + 1])
|
||||
|
||||
dist_to_pkg_map = defaultdict(list)
|
||||
for dist, pkg_list in importlib.metadata.packages_distributions().items():
|
||||
for pkg in pkg_list:
|
||||
dist_to_pkg_map[pkg].append(dist)
|
||||
|
||||
missing_modules = set()
|
||||
for distribution in stub_distributions:
|
||||
for pkg in read_dependencies(distribution).external_pkgs:
|
||||
egg_info = pkg_resources.get_distribution(pkg).egg_info
|
||||
assert isinstance(egg_info, str)
|
||||
# See https://stackoverflow.com/a/54853084
|
||||
top_level_file = os.path.join(egg_info, "top_level.txt")
|
||||
with open(top_level_file) as f:
|
||||
missing_modules.update(f.read().splitlines())
|
||||
for external_req in read_dependencies(distribution).external_pkgs:
|
||||
pkg = Requirement(external_req).name
|
||||
missing_modules.update(dist_to_pkg_map[pkg])
|
||||
|
||||
test_dir = os.path.dirname(__file__)
|
||||
exclude_list = os.path.join(test_dir, "pytype_exclude_list.txt")
|
||||
with open(exclude_list) as f:
|
||||
|
||||
Reference in New Issue
Block a user