pytype_test.py: Use importlib.metadata instead of pkg_resources (#10391)

This commit is contained in:
Alex Waygood
2023-06-30 21:37:06 +01:00
committed by GitHub
parent cd0388439a
commit 7fdd05003a
5 changed files with 20 additions and 16 deletions

View File

@@ -51,7 +51,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.9"
python-version: "3.10"
cache: pip
cache-dependency-path: requirements-tests.txt
- run: pip install -r requirements-tests.txt
@@ -66,5 +66,5 @@ jobs:
with:
version: ${{ steps.pyright_version.outputs.value }}
python-platform: ${{ matrix.python-platform }}
python-version: "3.9"
python-version: "3.10"
project: ./pyrightconfig.scripts_and_tests.json

View File

@@ -25,4 +25,3 @@ typing-extensions==4.6.3
# Type stubs used to type check our scripts.
types-pyyaml>=6.0.12.7
types-setuptools>=67.5.0.0

View File

@@ -19,7 +19,7 @@ objects at runtime.
in the `tests` and `scripts` directories.
To run the tests, follow the [setup instructions](../CONTRIBUTING.md#preparing-the-environment)
in the `CONTRIBUTING.md` document. In particular, you have to run with Python 3.9+.
in the `CONTRIBUTING.md` document. In particular, you have to run with Python 3.10+.
In order for `pytype_test` and `pyright_test` to work correctly, some third-party stubs
may require extra dependencies external to typeshed to be installed in your virtual environment
@@ -72,7 +72,7 @@ for this script.
Note: this test cannot be run on Windows
systems unless you are using Windows Subsystem for Linux.
It also requires a Python version < 3.11 as pytype does not yet support
It can currently only be run on Python 3.10 as pytype does not yet support
Python 3.11 and above.
Run using:

View File

@@ -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:

View File

@@ -13,7 +13,7 @@ from utils import colored, print_error
ReturnCode: TypeAlias = int
SUPPORTED_PLATFORMS = ("linux", "darwin", "win32")
SUPPORTED_VERSIONS = ("3.12", "3.11", "3.10", "3.9")
SUPPORTED_VERSIONS = ("3.12", "3.11", "3.10")
LOWEST_SUPPORTED_VERSION = min(SUPPORTED_VERSIONS, key=lambda x: int(x.split(".")[1]))
DIRECTORIES_TO_TEST = ("scripts", "tests")
EMPTY: list[str] = []