Skip to content

Commit 0b90344

Browse files
authored
Allow stubs to depend on numpy (#9499)
1 parent b9eab63 commit 0b90344

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ jobs:
9191
- uses: actions/checkout@v3
9292
- uses: actions/setup-python@v4
9393
with:
94-
python-version: "3.10"
94+
python-version: ${{ matrix.python-version }}
9595
cache: pip
9696
cache-dependency-path: requirements-tests.txt
97-
- run: pip install -r requirements-tests.txt
97+
- run: pip install $(grep mypy== requirements-tests.txt) packaging pathspec termcolor tomli typing-extensions
9898
- run: python ./tests/mypy_test.py --platform=${{ matrix.platform }} --python-version=${{ matrix.python-version }}
9999

100100
regression-tests:

tests/mypy_test.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import tempfile
1313
import time
1414
from collections import defaultdict
15+
from collections.abc import Sequence
1516
from dataclasses import dataclass
1617
from itertools import product
1718
from pathlib import Path
1819
from threading import Lock
19-
from typing import TYPE_CHECKING, Any, NamedTuple
20+
from typing import TYPE_CHECKING, Any, NamedTuple, Tuple
2021

2122
if TYPE_CHECKING:
2223
from _typeshed import StrPath
@@ -53,7 +54,7 @@
5354

5455
ReturnCode: TypeAlias = int
5556
VersionString: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_VERSIONS"]
56-
VersionTuple: TypeAlias = tuple[int, int]
57+
VersionTuple: TypeAlias = Tuple[int, int]
5758
Platform: TypeAlias = Annotated[str, "Must be one of the entries in SUPPORTED_PLATFORMS"]
5859

5960

@@ -78,6 +79,21 @@ def valid_path(cmd_arg: str) -> Path:
7879
parser = argparse.ArgumentParser(
7980
description="Typecheck typeshed's stubs with mypy. Patterns are unanchored regexps on the full path."
8081
)
82+
if sys.version_info < (3, 8):
83+
84+
class ExtendAction(argparse.Action):
85+
def __call__(
86+
self,
87+
parser: argparse.ArgumentParser,
88+
namespace: argparse.Namespace,
89+
values: Sequence[str],
90+
option_string: object = None,
91+
) -> None:
92+
items = getattr(namespace, self.dest) or []
93+
items.extend(values)
94+
setattr(namespace, self.dest, items)
95+
96+
parser.register("action", "extend", ExtendAction)
8197
parser.add_argument(
8298
"filter",
8399
type=valid_path,
@@ -324,7 +340,7 @@ def test_third_party_distribution(
324340

325341
mypypath = os.pathsep.join(str(Path("stubs", dist)) for dist in seen_dists)
326342
if args.verbose:
327-
print(colored(f"\n{mypypath=}", "blue"))
343+
print(colored(f"\nMYPYPATH={mypypath}", "blue"))
328344
code = run_mypy(
329345
args,
330346
configurations,

tests/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import venv
1010
from collections.abc import Iterable, Mapping
11-
from functools import cache
11+
from functools import lru_cache
1212
from pathlib import Path
1313
from typing import NamedTuple
1414
from typing_extensions import Annotated
@@ -25,6 +25,10 @@ def colored(text: str, color: str | None = None, on_color: str | None = None, at
2525
return text
2626

2727

28+
# A backport of functools.cache for Python <3.9
29+
# This module is imported by mypy_test.py, which needs to run on 3.7 in CI
30+
cache = lru_cache(None)
31+
2832
# Used to install system-wide packages for different OS types:
2933
METADATA_MAPPING = {"linux": "apt_dependencies", "darwin": "brew_dependencies", "win32": "choco_dependencies"}
3034

0 commit comments

Comments
 (0)