Repository navigation
Generated stubs are not importing submodules #3345
RamblingThis is kind of #2486, but I can't figure this one out despite spending most of today looking at many issues and discussions. I have also tried looking at how cryptography is doing it and can't figure it out either. I have read through Alternate Python source directory (src layout). I have the empty I have read through Type stub generation (*.pyi files) and introspection but it doesn't mention anything about submodules Despite Python submodules claiming it doesn't define a package, it feels like it does? Doesn't say anything about type stubs though. I am using a mixed layout, using the template from: # maturin 1.15.0
maturin new --mixed --src --bindings pyo3I have a mod py_defs;
// pyo3 0.30.1
use pyo3::prelude::*;
#[pymodule]
mod _native {
#[pymodule]
mod defs {
#[pymodule_export]
use crate::py_defs::MyClass;
}
#[pyfunction]
#[pyo3(signature = () -> "MyClass")]
fn my_function() -> Py<PyAny> { ... }
}…and Then, maturin develop --generate-stubsis generating:
From a Python interpreter, I'm able to
Thank you. |
Replies: 1 comment 1 reply
|
Maturin 1.15.0 bundles For 1.15.0, you can keep your module layout. Qualify the return annotation: #[pyo3(signature = () -> "defs.MyClass")]Then, after generating, add this to from . import defs as defsThat binds Checked a minimal stub fixture with mypy 2.4.0 and Pyright 1.1.414 using Codex. Both reject the missing binding and accept this layout; I haven't rebuilt your extension. |
Maturin 1.15.0 bundles
pyo3-introspection0.29.0, independently of the PyO3 version in your crate. The missing submodule re-export was fixed in PyO3 #6360; Maturin's main branch already has the generator update.For 1.15.0, you can keep your module layout. Qualify the return annotation:
#[pyo3(signature = () -> "defs.MyClass")]Then, after generating, add this to
src/mypackage/_native/__init__.pyi:That binds
_native.defsand lets the return annotation resolve to itsMyClass. Regenerating the stubs will overwrite the manual import.Checked a minimal stub fixture with mypy 2.4.0 and Pyright 1.1.414 using Codex. Both reject the missing binding and accept this layo…