pyo3_stub_gen/lib.rs
1//! This crate creates stub files in following three steps using [inventory] crate:
2//!
3//! Define type information in Rust code (or by proc-macro)
4//! ---------------------------------------------------------
5//! The first step is to define Python type information in Rust code. [type_info] module provides several structs, for example:
6//!
7//! - [type_info::PyFunctionInfo] stores information of Python function, i.e. the name of the function, arguments and its types, return type, etc.
8//! - [type_info::PyClassInfo] stores information for Python class definition, i.e. the name of the class, members and its types, methods, etc.
9//!
10//! For better understanding of what happens in the background, let's define these information manually:
11//!
12//! ```
13//! use pyo3::*;
14//! use pyo3_stub_gen::type_info::*;
15//!
16//! // Usual PyO3 class definition
17//! #[pyclass(module = "my_module", name = "MyClass")]
18//! struct MyClass {
19//! #[pyo3(get)]
20//! name: String,
21//! #[pyo3(get)]
22//! description: Option<String>,
23//! }
24//!
25//! // Submit type information for stub file generation to inventory
26//! inventory::submit!{
27//! // Send information about Python class
28//! PyClassInfo {
29//! // Type ID of Rust struct (used to gathering phase discussed later)
30//! struct_id: std::any::TypeId::of::<MyClass>,
31//!
32//! // Python module name. Since stub file is generated per modules,
33//! // this helps where the class definition should be placed.
34//! module: Some("my_module"),
35//!
36//! // Python class name
37//! pyclass_name: "MyClass",
38//!
39//! getters: &[
40//! MemberInfo {
41//! name: "name",
42//! r#type: <String as ::pyo3_stub_gen::PyStubType>::type_output,
43//! doc: "Name docstring",
44//! default: None,
45//! deprecated: None,
46//! },
47//! MemberInfo {
48//! name: "description",
49//! r#type: <Option<String> as ::pyo3_stub_gen::PyStubType>::type_output,
50//! doc: "Description docstring",
51//! default: None,
52//! deprecated: None,
53//! },
54//! ],
55//!
56//! setters: &[],
57//!
58//! doc: "Docstring used in Python",
59//!
60//! // Base classes
61//! bases: &[],
62//!
63//! // Decorated with `#[pyclass(eq, ord)]`
64//! has_eq: false,
65//! has_ord: false,
66//! // Decorated with `#[pyclass(hash, str)]`
67//! has_hash: false,
68//! has_str: false,
69//! // Decorated with `#[pyclass(subclass)]`
70//! subclass: false,
71//! }
72//! }
73//! ```
74//!
75//! Roughly speaking, the above corresponds a following stub file `my_module.pyi`:
76//!
77//! ```python
78//! class MyClass:
79//! """
80//! Docstring used in Python
81//! """
82//! name: str
83//! """Name docstring"""
84//! description: Optional[str]
85//! """Description docstring"""
86//! ```
87//!
88//! We want to generate this [type_info::PyClassInfo] section automatically from `MyClass` Rust struct definition.
89//! This is done by using `#[gen_stub_pyclass]` proc-macro:
90//!
91//! ```
92//! use pyo3::*;
93//! use pyo3_stub_gen::{type_info::*, derive::gen_stub_pyclass};
94//!
95//! // Usual PyO3 class definition
96//! #[gen_stub_pyclass]
97//! #[pyclass(module = "my_module", name = "MyClass")]
98//! struct MyClass {
99//! #[pyo3(get)]
100//! name: String,
101//! #[pyo3(get)]
102//! description: Option<String>,
103//! }
104//! ```
105//!
106//! Since proc-macro is a converter from Rust code to Rust code, the output must be a Rust code.
107//! However, we need to gather these [type_info::PyClassInfo] definitions to generate stub files,
108//! and the above [inventory::submit] is for it.
109//!
110//! Gather type information into [StubInfo]
111//! ----------------------------------------
112//! [inventory] crate provides a mechanism to gather [inventory::submit]ted information when the library is loaded.
113//! To access these information through [inventory::iter], we need to define a gather function in the crate.
114//! Typically, this is done by following:
115//!
116//! ```rust
117//! use pyo3_stub_gen::{StubInfo, Result};
118//!
119//! pub fn stub_info() -> Result<StubInfo> {
120//! let manifest_dir: &::std::path::Path = env!("CARGO_MANIFEST_DIR").as_ref();
121//! StubInfo::from_pyproject_toml(manifest_dir.join("pyproject.toml"))
122//! }
123//! ```
124//!
125//! There is a helper macro to define it easily:
126//!
127//! ```rust
128//! pyo3_stub_gen::define_stub_info_gatherer!(sub_info);
129//! ```
130//!
131//! Generate stub file from [StubInfo]
132//! -----------------------------------
133//! [StubInfo] translates [type_info::PyClassInfo] and other information into a form helpful for generating stub files while gathering.
134//!
135//! [generate] module provides structs implementing [std::fmt::Display] to generate corresponding parts of stub file.
136//! For example, [generate::MethodDef] generates Python class method definition as follows:
137//!
138//! ```rust
139//! use pyo3_stub_gen::{TypeInfo, generate::*, type_info::ParameterKind};
140//!
141//! let method = MethodDef {
142//! name: "foo",
143//! parameters: Parameters {
144//! positional_or_keyword: vec![Parameter {
145//! name: "x",
146//! kind: ParameterKind::PositionalOrKeyword,
147//! type_info: TypeInfo::builtin("int"),
148//! default: ParameterDefault::None,
149//! }],
150//! ..Parameters::new()
151//! },
152//! r#return: TypeInfo::builtin("int"),
153//! doc: "This is a foo method.",
154//! r#type: MethodType::Instance,
155//! deprecated: None,
156//! is_async: false,
157//! type_ignored: None,
158//! is_overload: false,
159//! };
160//!
161//! assert_eq!(
162//! method.to_string().trim(),
163//! r#"
164//! def foo(self, x: builtins.int) -> builtins.int:
165//! r"""
166//! This is a foo method.
167//! """
168//! "#.trim()
169//! );
170//! ```
171//!
172//! [generate::ClassDef] generates Python class definition using [generate::MethodDef] and others, and other `*Def` structs works as well.
173//!
174//! [generate::Module] consists of `*Def` structs and yields an entire stub file `*.pyi` for a single Python (sub-)module, i.e. a shared library build by PyO3.
175//! [generate::Module]s are created as a part of [StubInfo], which merges [type_info::PyClassInfo]s and others submitted to [inventory] separately.
176//! [StubInfo] is instantiated with [pyproject::PyProject] to get where to generate the stub file,
177//! and [StubInfo::generate] generates the stub files for every modules.
178//!
179
180pub use inventory;
181pub use pyo3_stub_gen_derive as derive; // re-export to use in generated code
182
183pub mod exception;
184pub mod generate;
185pub mod pyproject;
186pub mod rule_name;
187mod stub_type;
188pub mod type_info;
189pub mod util;
190
191pub use generate::StubInfo;
192pub use stub_type::{PyStubType, TypeInfo};
193
194pub type Result<T> = anyhow::Result<T>;
195
196/// Create a function to initialize [StubInfo] from `pyproject.toml` in `CARGO_MANIFEST_DIR`.
197///
198/// If `pyproject.toml` is in another place, you need to create a function to call [StubInfo::from_pyproject_toml] manually.
199/// This must be placed in your PyO3 library crate, i.e. same crate where [inventory::submit]ted,
200/// not in `gen_stub` executables due to [inventory] mechanism.
201///
202#[macro_export]
203macro_rules! define_stub_info_gatherer {
204 ($function_name:ident) => {
205 /// Auto-generated function to gather information to generate stub files
206 pub fn $function_name() -> $crate::Result<$crate::StubInfo> {
207 let manifest_dir: &::std::path::Path = env!("CARGO_MANIFEST_DIR").as_ref();
208 $crate::StubInfo::from_pyproject_toml(manifest_dir.join("pyproject.toml"))
209 }
210 };
211}
212
213/// Add module-level documention using interpolation of runtime expressions.
214/// The first argument `module_doc!` receives is the full module name;
215/// the second and followings are a format string, same to `format!`.
216/// ```rust
217/// pyo3_stub_gen::module_doc!(
218/// "module.name",
219/// "Document for {} v{} ...",
220/// env!("CARGO_PKG_NAME"),
221/// env!("CARGO_PKG_VERSION")
222/// );
223/// ```
224#[macro_export]
225macro_rules! module_doc {
226 ($module:literal, $($fmt:tt)+) => {
227 $crate::inventory::submit! {
228 $crate::type_info::ModuleDocInfo {
229 module: $module,
230 doc: {
231 fn _fmt() -> String {
232 ::std::format!($($fmt)+)
233 }
234 _fmt
235 }
236 }
237 }
238 };
239}
240
241/// Add module-level variable, the first argument `module_variable!` receives is the full module name;
242/// the second argument is the name of the variable, the third argument is the type of the variable,
243/// and (optional) the fourth argument is the default value of the variable.
244/// ```rust
245/// pyo3_stub_gen::module_variable!("module.name", "CONSTANT1", usize);
246/// pyo3_stub_gen::module_variable!("module.name", "CONSTANT2", usize, 123);
247/// ```
248#[macro_export]
249macro_rules! module_variable {
250 ($module:expr, $name:expr, $ty:ty) => {
251 $crate::inventory::submit! {
252 $crate::type_info::PyVariableInfo{
253 name: $name,
254 module: $module,
255 r#type: <$ty as $crate::PyStubType>::type_output,
256 default: None,
257 }
258 }
259 };
260 ($module:expr, $name:expr, $ty:ty, $value:expr) => {
261 $crate::inventory::submit! {
262 $crate::type_info::PyVariableInfo{
263 name: $name,
264 module: $module,
265 r#type: <$ty as $crate::PyStubType>::type_output,
266 default: Some({
267 fn _fmt() -> String {
268 let v: $ty = $value;
269 $crate::util::fmt_py_obj(v)
270 }
271 _fmt
272 }),
273 }
274 }
275 };
276}
277
278#[doc = include_str!("../README.md")]
279mod readme {}