pnm 1.0.3
peclet.pnm — pore-network extraction from SDF geometry (Kokkos)
Loading...
Searching...
No Matches
test_stub_sync.py
Go to the documentation of this file.
1"""Contract: the shipped type stub packaging/_pnm.pyi covers every public name of peclet.pnm._pnm.
2
3The stub is generated by nanobind.stubgen against an MPI-enabled host build (recipe in
4CMakeLists.txt, next to the rule that stages it). A binding added without regenerating it fails
5here. Names in the stub but absent at runtime fail too, except on a build without MPI, which lacks
6the distributed names the stub carries.
7
8Run: PYTHONPATH=<build> python -m pytest -q tests/python/test_stub_sync.py
9"""
10import ast
11from pathlib import Path
12
13from peclet.pnm import _pnm
14
15STUB = Path(__file__).resolve().parents[2] / "packaging" / "_pnm.pyi"
16# Bound only under PECLET_PNM_MPI (src/pnm_bindings.cpp).
17MPI_ONLY = {"mpi_block", "extract_pore_network_mpi", "extract_network_flow_mpi"}
18
19
20def _public(name):
21 return not name.startswith("_") or name == "__init__"
22
23
24def _members(cls):
25 return {f"{cls.name}.{m.name}" for m in cls.body
26 if isinstance(m, ast.FunctionDef) and _public(m.name)}
27
28
30 names = set()
31 for node in ast.parse(STUB.read_text()).body:
32 if isinstance(node, ast.ClassDef):
33 names |= {node.name} | _members(node)
34 elif isinstance(node, ast.FunctionDef):
35 names.add(node.name)
36 elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
37 names.add(node.target.id)
38 return names
39
40
42 names = set()
43 for name in dir(_pnm):
44 if name.startswith("_"):
45 continue
46 names.add(name)
47 obj = getattr(_pnm, name)
48 if isinstance(obj, type):
49 # A class without a bound constructor (SDFReader) still has nanobind's refusing
50 # __init__, which carries no signature and is not part of the API.
51 names.update(f"{name}.{m}" for m, v in vars(obj).items()
52 if _public(m) and (m != "__init__" or hasattr(v, "__nb_signature__")))
53 return names
54
55
57 missing = sorted(_runtime_names() - _stub_names())
58 assert not missing, (f"public names of peclet.pnm._pnm missing from {STUB.name}: {missing}; "
59 "regenerate the stub (recipe in CMakeLists.txt)")
60
61
63 extra = _stub_names() - _runtime_names()
64 if not hasattr(_pnm, "mpi_block"): # a build without MPI: the stub lists more
65 extra -= MPI_ONLY
66 assert not extra, f"names in {STUB.name} that peclet.pnm._pnm does not define: {sorted(extra)}"
test_stub_covers_every_public_name()
test_stub_names_exist_at_runtime()