flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_stub_sync.py
Go to the documentation of this file.
1"""Contract: the shipped type stub packaging/_flow.pyi covers every public name of peclet.flow._flow.
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.flow import _flow
14
15STUB = Path(__file__).resolve().parents[2] / "packaging" / "_flow.pyi"
16# Bound only under PECLET_FLOW_MPI (src/flow_bindings.cpp).
17MPI_ONLY = {"mpi_block", "predict_hierarchy", "Solver.init_mpi", "SolverColocated.init_mpi",
18 "SolverDiagnostics.rebalance_by_weights",
19 "SolverColocatedDiagnostics.rebalance_by_weights"}
20
21
22# Private handle classes that public methods return (`Solver.anderson_accelerator`): the stub must
23# define them, or its return annotations name an undefined type.
24RETURNED_PRIVATE = {"_AndersonAccelerator", "_AndersonAcceleratorColocated"}
25
26
27def _public(name):
28 return not name.startswith("_") or name == "__init__"
29
30
32 tree = ast.parse(STUB.read_text())
33 names = set()
34 for node in tree.body:
35 if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
36 names.add(node.name)
37 elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
38 names.add(node.target.id)
39 if isinstance(node, ast.ClassDef):
40 for member in node.body:
41 if isinstance(member, ast.FunctionDef) and _public(member.name):
42 names.add(f"{node.name}.{member.name}")
43 return names
44
45
47 names = set()
48 for name in dir(_flow):
49 if name.startswith("_") and name not in RETURNED_PRIVATE:
50 continue
51 names.add(name)
52 obj = getattr(_flow, name)
53 if isinstance(obj, type):
54 # A class without a bound constructor (the diagnostics tiers) still has nanobind's
55 # refusing __init__, which carries no signature and is not part of the API.
56 names.update(f"{name}.{m}" for m, v in vars(obj).items()
57 if _public(m) and (m != "__init__" or hasattr(v, "__nb_signature__")))
58 return names
59
60
62 missing = sorted(_runtime_names() - _stub_names())
63 assert not missing, (f"public names of peclet.flow._flow missing from {STUB.name}: {missing}; "
64 "regenerate the stub (recipe in CMakeLists.txt)")
65
66
68 extra = _stub_names() - _runtime_names()
69 if not _flow.has_mpi: # a build without MPI: the stub lists more
70 extra -= MPI_ONLY
71 assert not extra, f"names in {STUB.name} that peclet.flow._flow does not define: {sorted(extra)}"
test_stub_covers_every_public_name()
test_stub_names_exist_at_runtime()