Skip to content

Commit 3c6158d

Browse files
committed
✅ custom stubtest script for the scipy.version literals
1 parent b901cd0 commit 3c6158d

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ jobs:
4242
uv run ruff check --output-format=github
4343
uv run ruff format --check
4444
45+
- name: check version literals
46+
run: uv run scripts/check_version_literals.py
47+
4548
# mypy_primer expects pyright to pass
4649
- name: pyright
4750
uses: jakebailey/[email protected]

lefthook.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pre-commit:
2121
- name: basedpyright
2222
glob: "*.{py,pyi}"
2323
run: uv {run} basedpyright --threads=3 {staged_files}
24+
- name: check-version-literals
25+
run: uv {run} scripts/check_version_literals.py
2426

2527
post-checkout:
2628
jobs:

scripts/check_version_literals.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Test that the `scipy.version` stub literals match runtime."""
2+
3+
# ruff: noqa: S101
4+
5+
import importlib.util
6+
import sys
7+
import types
8+
from pathlib import Path
9+
10+
from scipy import version as version_scipy
11+
12+
PATH_STUBS = Path(__file__).parent.parent / "scipy-stubs"
13+
14+
15+
def _import_pyi(name: str, path: str | Path) -> types.ModuleType:
16+
"""Hack to import a `.pyi` file as a module."""
17+
spec = importlib.util.spec_from_loader(name, loader=None)
18+
if spec is None:
19+
raise ImportError(name=name, path=str(path))
20+
module = importlib.util.module_from_spec(spec)
21+
22+
source = Path(path).read_text(encoding="utf-8")
23+
exec(source, module.__dict__) # noqa: S102
24+
25+
sys.modules[spec.name] = module
26+
return module
27+
28+
29+
def main() -> None:
30+
version_scipyi = _import_pyi("scipyi.version", PATH_STUBS / "version.pyi")
31+
literals_scipyi = {
32+
n: v
33+
for n, v in vars(version_scipyi).items()
34+
if not n.startswith("_") and v is not Ellipsis
35+
}
36+
assert literals_scipyi, "No literals found in scipy-stubs/version.pyi"
37+
38+
for name, val in literals_scipyi.items():
39+
val_expect = getattr(version_scipy, name, "<MISSING>")
40+
qname = f"scipy.version.{name}"
41+
assert val == val_expect, f"Expected `{qname} = {val_expect!r}`, got {val!r}."
42+
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)