A common pattern for creating async Python methods in Rust is to use a synchronous function that returns a Python awaitable via pyo3_async_runtimes::tokio::future_into_py.
Here is an example of such a function:
#[pymethods]
impl MyClass {
// The Rust signature is sync
pub fn my_async_method<'p>(&self, py: Python<'p>) -> PyResult<Bound<'p, PyAny>> {
pyo3_async_runtimes::tokio::future_into_py(py, async move {
// ... async work ...
Ok(Python::with_gil(|py| py.None()))
})
}
}
While this works perfectly at runtime and is awaitable in Python, pyo3-stub-gen inspects the synchronous Rust signature and generates an incorrect stub:
# Generated stub
def my_async_method(self) -> Any: ...
This breaks type checking and IntelliSense for users of the library, as the method is not correctly marked as a coroutine.
I propose adding a new attribute, for example #[gen_stub(async)], that developers can add to these functions. This attribute would serve as a hint to pyo3-stub-gen to generate an async def stub, regardless of the Rust function's signature.
# Desired stub
async def my_async_method(self) -> Any: ...
This feature would greatly improve the developer experience by allowing robust async patterns in Rust to be accurately reflected in the generated Python type hints.
Thank you for considering this request!
A common pattern for creating async Python methods in Rust is to use a synchronous function that returns a Python awaitable via
pyo3_async_runtimes::tokio::future_into_py.Here is an example of such a function:
While this works perfectly at runtime and is awaitable in Python, pyo3-stub-gen inspects the synchronous Rust signature and generates an incorrect stub:
This breaks type checking and IntelliSense for users of the library, as the method is not correctly marked as a coroutine.
I propose adding a new attribute, for example #[gen_stub(async)], that developers can add to these functions. This attribute would serve as a hint to pyo3-stub-gen to generate an async def stub, regardless of the Rust function's signature.
This feature would greatly improve the developer experience by allowing robust async patterns in Rust to be accurately reflected in the generated Python type hints.
Thank you for considering this request!