-
-
Notifications
You must be signed in to change notification settings - Fork 390
RFC: Support automated stub generation #3090
Description
PyO3 stub generation implementation is moving forward well and starts to be in a fairly good state. It would be great to expose it in Maturin.
For context: PyO3 stub generation works by inserting into the built binaries extra symbols, each containing a JSON object describing some element (a module, a class...) and pointing to other elements by identifiers. The pyo3-introspection crate then extracts the fragments from the binaries, parse them and build stub files. The stubs generated are designed in such a way that they are never "too narrow" i.e. they should not introduce a type error in working code. We aim at having as good as possible compatibility in the JSON fragment, providing backward compatibility (newer pyo3-introspection can read fragments generated by older pyo3) and best effort forward compatibility (older pyo3-introspection should write not too bad stubs from data from newer pyo3).
I see two ways to expose stubs in PyO3:
- a new
maturin build-stubscommand that would run a debug build and write the stub files in a given output directory. - an option
--introspect-stubsor--generate-stubsto thebuildanddevelopcommands that would be available on binary-only pyo3 projects and automatically add the stubs to the built artifacts. We might start by having this option as disabled by default then enable it by default if we are confident the stubs are always correct. Added automatic type stub generation with pyo3-introspection #2940 goes into this direction.
Both are useful, 1. seems very useful to experiment with stubs and in hybrid Rust/Python projects, and 2 seems convenient in binary only projects as a "just works" mechanism.
If we have agreement on the objectives, a possible timeline might be:
maturin build-stubsadded soon relying on thepyo3-introspection0.28 release. It covers nearly everything except doc comments and advanced features like custom imports, generics... and might be a good experiments start. We would make it clear this is a "preview" unstable feature.- When we are happy with the stubs state and after new PyO3 releases, implement the
--generate-stubsoption. - In long term, if we are confident about stubs quality, switch
--generate-stubsas a default behavior in a possible maturin breaking release.