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