macro_rules! type_alias {
($module:expr, $name:ident = $($base:ty)|+, $doc:expr) => { ... };
($module:expr, $name:ident = $($base:ty)|+) => { ... };
($module:expr, $name:ident = $ty:ty, $doc:expr) => { ... };
($module:expr, $name:ident = $ty:ty) => { ... };
}Expand description
Define a module-level type alias with runtime support.
This macro creates a zero-sized struct that:
- Generates a type alias entry in Python stub files (
.pyi) - Can be registered at runtime using
runtime::PyModuleTypeAliasExt::add_type_alias
§Syntax
ⓘ
// Union type
pyo3_stub_gen::type_alias!("module.name", MyUnion = TypeA | TypeB);
// Single type (alias)
pyo3_stub_gen::type_alias!("module.name", MyAlias = SomeType);
// With documentation
pyo3_stub_gen::type_alias!("module.name", MyAlias = TypeA | TypeB, "Documentation string");§Examples
use pyo3_stub_gen::type_alias;
// Define a union type alias
type_alias!("my_module", NumberOrString = i32 | String);
// Define a single type alias
type_alias!("my_module", OptionalInt = Option<i32>);§Runtime Registration
To make the type alias importable from Python, register it in your module:
ⓘ
use pyo3::prelude::*;
use pyo3_stub_gen::runtime::PyModuleTypeAliasExt;
type_alias!("my_module", NumberOrString = i32 | String);
#[pymodule]
fn my_module(m: &Bound<PyModule>) -> PyResult<()> {
m.add_type_alias::<NumberOrString>()?;
Ok(())
}