General issues with submodules #3591
MathijsdeBoer
started this conversation in
General
Replies: 2 comments 3 replies
-
I fixed the first issue using a later comment in #759, but it took me longer than I'd like to admit. Something like this really ought to be in the documentation without digging through issue comments In short: // ./src/lib.rs
use pyo3::prelude::*;
mod linalg;
#[pymodule]
fn _rs(_py: Python, m: &PyModule) -> PyResult<()> {
linalg::init_linalg(_py, m)?;
Ok(())
} // ./src/linalg.mod.rs
mod vec3;
pub use vec3::Vec3;
use pyo3::prelude::*;
#[pymodule]
fn linalg(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Vec3>()?;
Ok(())
}
pub fn init_linalg(_py: Python, m: &PyModule) -> PyResult<()> {
let linalg_child_module = PyModule::new(_py, "evdmeshlib._rs.linalg")?;
linalg(_py, linalg_child_module)?;
m.add("linalg", linalg_child_module)?;
_py.import("sys")?
.getattr("modules")?
.set_item("evdmeshlib._rs.linalg", linalg_child_module)?;
Ok(())
} # ./python/evdmeshlib/__init__.py
from evdmeshlib._rs.linalg import Vec3
def test_vec3():
v = Vec3(1, 2, 3)
w = Vec3(4, 5, 6)
print(v)
print(w)
print(v + w)
if __name__ == '__main__':
test_vec3() Unfortunately, the second issue, type hinting for submodules, is still unknown to me. RustRover is doing quite a bit of yelling at me for writing my Python files with this. |
Beta Was this translation helpful? Give feedback.
3 replies
-
Can "adding type hints for submodules" work when using declarative modules? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've been playing around with Maturin and Pyo3 of a little while, and have gotten some neat things to work. Mainly, I want to use Pyo3 to move some of my math heavy Python code to Rust, so I can get speedups for processing. That is, a majorly Python project, with some aspects moved to Rust as opposed to a lot of the tutorials I can find assuming a majorly Rust project you can call from Python.
For example, my current project is based around doing a lot of ray/triangle intersections, which lends itself a bit more to a compiled language for speed. My first iteration, while functional, had a few misconfigurations that made working with it a bit of a hassle. Every time I wanted to use one of my structs, I'd have to type the entire slug to get there.
While it would be much more ergonomic to be able to do
I've seen #759, which seems to be a pure Rust project, where the
supermodule
is the top-level package, but I'd like to use the Rust code as a submodule instead.I've been making a few new projects with maturin, and got to being able to do
But when I then add a new Rust module as submodule:
And adding
submodule
to the top-levelpymodule
withI can't do
As it fails on
from linalg._rs.submodule import SomeStruct
withBut doing the thing I mentioned at the start:
does work. Somewhat. RustRover and PyCharm both yell at me for
_rs.submodule
not existing, while the code does run.How could I make this work as expected? If at all. Googling the issue simply gives me a lot of techblogs that copy the basic template, and leave it at that, instead of diving in a little deeper.
Secondary question: How would I use type hints properly with submodules?
I know that the file setup, with
py.typed
and_rs.pyi
in the root python package, described above works fine, as I got it from the official documentation, but I can't quite figure out how this would work if I get my submodules to work. The single_rs.pyi
wouldn't also cover the_rs.submodule.SomeStruct
type hinting, right?Beta Was this translation helpful? Give feedback.
All reactions