From 85b2bc3e8a1ee7a4a3a55a9ee547f7d3fb4520bc Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Wed, 11 Jan 2023 15:22:29 +0000 Subject: [PATCH] Allow stubs to depend on `numpy` --- .github/workflows/tests.yml | 4 ++-- tests/mypy_test.py | 22 +++++++++++++++++++--- tests/utils.py | 6 +++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c36b24386d07..01ffd72c228e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -91,10 +91,10 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-python@v4 with: - python-version: "3.10" + python-version: ${{ matrix.python-version }} cache: pip cache-dependency-path: requirements-tests.txt - - run: pip install -r requirements-tests.txt + - run: pip install $(grep mypy== requirements-tests.txt) packaging pathspec termcolor tomli typing-extensions - run: python ./tests/mypy_test.py --platform=${{ matrix.platform }} --python-version=${{ matrix.python-version }} regression-tests: diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 0fd07095b1d5..000b26f7bacc 100644 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -12,11 +12,12 @@ import tempfile import time from collections import defaultdict +from collections.abc import Sequence from dataclasses import dataclass from itertools import product from pathlib import Path from threading import Lock -from typing import TYPE_CHECKING, Any, NamedTuple +from typing import TYPE_CHECKING, Any, NamedTuple, Tuple if TYPE_CHECKING: from _typeshed import StrPath @@ -52,7 +53,7 @@ ReturnCode: TypeAlias = int VersionString: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_VERSIONS"] -VersionTuple: TypeAlias = tuple[int, int] +VersionTuple: TypeAlias = Tuple[int, int] Platform: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_PLATFORMS"] @@ -77,6 +78,21 @@ def valid_path(cmd_arg: str) -> Path: parser = argparse.ArgumentParser( description="Typecheck typeshed's stubs with mypy. Patterns are unanchored regexps on the full path." ) +if sys.version_info < (3, 8): + + class ExtendAction(argparse.Action): + def __call__( + self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Sequence[str], + option_string: object = None, + ) -> None: + items = getattr(namespace, self.dest) or [] + items.extend(values) + setattr(namespace, self.dest, items) + + parser.register("action", "extend", ExtendAction) parser.add_argument( "filter", type=valid_path, @@ -323,7 +339,7 @@ def test_third_party_distribution( mypypath = os.pathsep.join(str(Path("stubs", dist)) for dist in seen_dists) if args.verbose: - print(colored(f"\n{mypypath=}", "blue")) + print(colored(f"\nMYPYPATH={mypypath}", "blue")) code = run_mypy( args, configurations, diff --git a/tests/utils.py b/tests/utils.py index a8d81f93e122..6924c209e115 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -8,7 +8,7 @@ import sys import venv from collections.abc import Iterable, Mapping -from functools import cache +from functools import lru_cache from pathlib import Path from typing import NamedTuple from typing_extensions import Annotated @@ -25,6 +25,10 @@ def colored(text: str, color: str | None = None, on_color: str | None = None, at return text +# A backport of functools.cache for Python <3.9 +# This module is imported by mypy_test.py, which needs to run on 3.7 in CI +cache = lru_cache(None) + # Used to install system-wide packages for different OS types: METADATA_MAPPING = {"linux": "apt_dependencies", "darwin": "brew_dependencies", "win32": "choco_dependencies"}