Skip to content

Python stubs inherit the method sets

Each Python method set of geomcore-py/src/method_sets.rs has one stub-only private class in the stubs (geomcore/__init__.pyi since ADR 0038): _CurveMethods and _Curve2DMethods (both built on _CurveMethodsInEitherDimension), _BoundedCurveMethods, _SidedEvaluationMethods, _SurfaceMethods (built on _ParametricSurfaceMethods) and _SidedSurfaceEvaluationMethods. A class stub inherits the classes of the sets its shape_methods! line names and lists only its own methods. What differs between the classes that share a set is a type argument: the point, vector, bounding box and projection classes of a dimension, the discretization class, and the classes of a surface's u and v iso curves (_SurfaceMethods[Line, Circle] for a cylinder). The runtime classes do not have these bases. They exist only for type checkers and IDEs.

Before this decision the stubs repeated every set in every class: second_derivatives was written 20 times in curves.pyi and 9 times in surfaces.pyi, and the parity test restated the sets a third time. Now each shared method is typed once in the stubs, as it is written once in Rust, and a swept-surface stub is its constructor and its own few methods.

The set classes carry the docstrings of the Rust doc comments, which are the methods' __doc__ at runtime, so an IDE shows them on hover. tests/test_parity.py keeps the copies honest. It requires a docstring on every set method and checks that each stub docstring is the runtime one. It reads the sets from the stubs instead of restating them, and checks that every class has each method its stub inherits. stubtest does not check that, because it compares the methods a stub gives a class, wherever they come from, but not the stub's bases. The same test runs mypy over a generated file that touches every public method of every class, plus a few assert_type lines on the type arguments. This shows that type checkers still see each class's full method list.

Considered options

  • Generate the stubs from the built module, from each method's __text_signature__ and __doc__ and a table of type annotations, with a CI check that the committed stubs are current: rejected. __text_signature__ carries parameter names and defaults but no types. So the table would hold the type of every parameter and return value of every method, which is the stubs again in another format, and it would also need the intersect overloads and the per-class return types as special cases. The generator would add a build step and a CI job that needs the built module. Its one real gain is a docstring on every method, the class-specific ones included, for free. Revisit it if the Python reference generated from docstrings (on the roadmap) needs those.
  • One _method_sets.pyi module holding the sets, imported by the class stubs: rejected. stubtest checks every stub module against a runtime module of the same name and fails on geomcore._method_sets, which does not exist. It would need an allowlist entry for a whole module, or an empty runtime module shipped only to satisfy the check. The set classes sit in the two stub files they serve instead.
  • typing.Protocol bases: not needed. stubtest accepts plain private base classes that the runtime classes lack, and nothing asks for the structural typing a protocol adds. A caller who wants "any curve" has the Curve and Curve2D unions (public since ADR 0038).
  • Keep the copies and add docstrings to each: rejected. Every shared docstring would be copied 9 to 16 times, and the copies would drift the way the trait twins did before ADR 0015.

Consequences

A new curve or surface class costs its own methods in its Rust file, its stub and a fixture in the parity test: the sets come from one base list in each. A method added to a Rust set goes into the matching stub class, with its doc comment as the docstring, and the parity test fails until both agree. Type checkers report the inherited methods as defined on the private set classes. Errors and hovers therefore name the private set class, while reveal_type on a call still gives the class-specific type. Class-specific methods have no docstrings in the stubs yet. Adding one is optional, and the parity test checks any docstring a stub carries.

Amended (#334): every class, function, method and property documented at runtime now carries the same docstring in the stub, class-specific ones included, so an editor that reads only the stub shows them on hover. scripts/sync_stub_docstrings.py copies them from the built module into the stub; it fills docstrings only, so the hand-written signatures stay as they are and the generator rejected above is still rejected. The parity test fails until the stub matches the runtime. A parameter that takes any curve or surface is now annotated with the set class every one of them inherits (_CurveMethods, _Curve2DMethods, _SurfaceMethods[Any, Any]) instead of the Curve | TrimmedCurve or Surface union. mypy types a list mixing a circle and a line as their common base, which the union refused. The parity test reads such a parameter as taking every class that inherits the set. The type variables of the iso curves are covariant, so a list of surfaces joins to _SurfaceMethods rather than object.