How class method overloads work
mainClass methods can be overloaded using the submit! macro combined with gen_methods_from_python!. This allows you to manually define method signatures (including @overload variants) for a #[gen_stub_pyclass].
Rules for class method overloads:
- Multiple methods with the same name must use the
@overloaddecorator in the Python string. - At most one method can have
is_overload = false(this represents the actual implementation). - The
@overloaddecorator is automatically detected from the provided Python stub syntax.
use pyo3::prelude::*;
use pyo3_stub_gen::{derive::*, inventory::submit};
#[gen_stub_pyclass]
#[pyclass]
pub struct ManualSubmit {}
submit! {
gen_methods_from_python! {
r"#
class ManualSubmit:
@overload
def echo(self, obj: int) -> int: ...
@overload
def echo(self, obj: float) -> float: ...
"#
}
}
#[pymethods]
impl ManualSubmit {
fn echo<'arg>(&self, obj: Bound<'arg, PyAny>) -> Bound<'arg, PyAny> {
obj
}
}