Module pystub
Expand description
pystub — the three Python package stubs under
crates/atune_py/python/atune, composed from the live module and an explicit
annotation table (D27, docs/design/11-documentation-plan.md §8).
§Why a generator and not a hand-written stub
The Python surface is 8 classes, 3 exception types and several dozen
callables. A hand-written stub rots; a fully automatic one is impossible,
because pyo3 knows the names of a binding’s parameters but not their
types — experimental-inspect marks a #[pymodule]-registered module
Incomplete and hands back def __getattr__(name: str) -> Incomplete
(§1.3.4). So this generator composes two sources:
- the live module, imported through the embedded interpreter
atune_python::init_pythonalready gives the bindings’ own tests — the symbol set (each module’s__all__), the parameter names and defaults pyo3 derives into__text_signature__from the 21#[pyo3(signature = …)]attributes, and the docstrings pyo3 sets from///; crates/atune_dev/pystub_types.toml, for the annotations pyo3 cannot supply.
No wheel is built: append_to_inittab! makes import atune resolve
in-process, which is what keeps this generator inside the fast rust.yml
gate (§7’s Needs column).
§Fail-closed, in both directions
A symbol in the module with no table entry is an error naming the symbol and
the file to edit — never an Any. A table entry that matches no symbol is
also an error, so a removed binding cannot leave a stale annotation behind
to be silently reused by the next binding of the same name.
§Three owned files
Maturin’s mixed-package layout copies the Python source tree into the wheel,
so each live module has its own generated sibling stub:
__init__.pyi, samplers.pyi, and schedulers.pyi. The same live-module
introspection and table coverage pass feeds all three files. Keeping the
files as separate GeneratedDoc entries makes every path visible to the
generate-all registry and lets drift fail closed per file.
Structs§
- Live
Coverage 🔒 - Live names collected before checking the annotation table’s coverage.
- Member 🔒
- One member of a module or class.
- Module
Surface 🔒 - One module’s public surface, as the live interpreter reports it.
- Param 🔒
- One parameter of a callable.
- Python
Stub - One generated Python package stub.
- Symbol
Types 🔒 - One callable’s (or property’s) annotations.
- Type
Table 🔒 - The annotations pyo3 cannot supply, read from
TABLE.
Enums§
- Member
Kind 🔒 - The shapes a member can take.
- Stub
Module 🔒 - Which package module a generated stub describes.
Constants§
- GENERATOR 🔒
- The generator’s name, for error messages.
- MODULES 🔒
- The three modules the stubs cover, parent first.
- SKIPPED_
DUNDERS 🔒 - Attributes every
#[pyclass]carries that the stub does not restate. - TABLE 🔒
- The annotation table, relative to the repository root.
Functions§
- annotation_
source 🔒 - Collects the annotation text owned by one live module.
- banner 🔒
- The banner every generated stub opens with.
- callable_
block 🔒 - Renders one function or method.
- callable_
kind 🔒 - Reads a callable’s
__text_signature__. - canonicalize_
aliases 🔒 - Replaces duplicate public names for one live Python class with aliases.
- class_
block 🔒 - Renders one class.
- class_
kind 🔒 - Reads a class’s bases and members.
- code_
span_ 🔒labels - Puts the label of
[label](rust::path)into a code span. - docstring 🔒
- Reads
__doc__, treating an empty string as absent. - docstring_
block 🔒 - Renders a docstring at
indentcolumns. - enum_
block 🔒 - Renders one
enum.Enumsubclass and its members. - enum_
kind 🔒 - Reads an enum class’s mixin type and its members.
- filtered_
import 🔒 - Filters one candidate from-import line to names used by a module.
- identifier_
used 🔒 - Whether identifier appears as a whole identifier in text.
- introspect 🔒
- Imports the live module and reads the three surfaces out of it.
- is_
descriptor 🔒 - Whether
valueis one of the descriptor types pyo3 produces for a getter. - is_
enum_ 🔒class - Whether
valueis a subclass ofenum.Enum. - literal 🔒
- Accepts a default only if it is a plain Python literal.
- missing 🔒
- The error a symbol with no table entry fails with.
- module_
imports 🔒 - Renders only the candidate imports used by one module.
- module_
prelude 🔒 - Selects type aliases and variables referenced by one module’s annotations.
- module_
surface 🔒 - Reads one module’s surface.
- parse_
signature 🔒 - Parses
($self, name, low, *, log=False)into parameters. - property_
block 🔒 - Renders one
@property. - render_
module 🔒 - Renders one of the three generated package stubs.
- split_
top_ 🔒level - Splits on commas that are not inside a bracket or a quote.
- string_
array 🔒 - Reads an array of TOML strings from
item[key]. - string_
value 🔒 - Reads a TOML string, or fails naming
context. - unclassifiable 🔒
- Wraps a message as this generator’s fail-closed error.