Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative modules don't set item's module properly #4070

Closed
wyfo opened this issue Apr 12, 2024 · 5 comments
Closed

Declarative modules don't set item's module properly #4070

wyfo opened this issue Apr 12, 2024 · 5 comments

Comments

@wyfo
Copy link
Contributor

wyfo commented Apr 12, 2024

Using this code

use pyo3::prelude::*;

#[pymodule]
mod my_extension {
    #[pymodule]
    mod submodule {
        #[pyfunction]
        fn foo() {}
    }
}

my_extension.submodule.foo.__module__ returns submodule instead of my_extension.submodule, as it would be expected for pure Python modules.

Relates to #3900

@davidhewitt
Copy link
Member

davidhewitt commented Apr 13, 2024

Agreed this could be much better. There's additional runtime complexity because my_extension might itself be a submodule to a pure python root e.g. my_package.my_native_code_extension.submodule

@Tpt
Copy link
Contributor

Tpt commented May 27, 2024

I have opened #4213 that should fix it

@davidhewitt
Copy link
Member

Closing as resolved in #4213

@b-kiiskila
Copy link

Sorry to post on an older issue but this seems like it's related, if I should open a new issue let me know and I'll go ahead and do that.

If you define a function on a submodule then the __module__ field ends up as just the name of the submodule and not the fully qualified module name. I can work around this for the time being but this seems like it's not intentional behaviour.

This also behaves differently with a class.

Just as a very simple example if you defined a module like this:

#[pymodule]
mod my_module {
    use super::*;

    #[pyfunction]
    fn double(x: i64) -> PyResult<i64> {
        Ok(x * 2)
    }

    #[pyclass]
    struct DummyClassRootMod {}

    #[pymodule]
    mod dummy_submodule {
        use super::*;

        #[pyfunction]
        fn triple(x: i64) -> PyResult<i64> {
            Ok(x * 3)
        }

        #[pyclass]
        struct DummyClassSubMod {}
    }
}

If I then use this in Python I can see the module defined correctly on the top level function, but the submodules are not defined the way I'd expect. Additionally it seems like the classes aren't defined quite right, I'm using maturin with my project and have my module-name defined as project.my_module for this example. So maybe that part in particular should be an issue on the maturin repo.

from project import my_module
my_module.double.__module__
Out[3]: 'project.my_module'
my_module.dummy_submodule.triple.__module__
Out[4]: 'dummy_submodule'
my_module.DummyClassRootMod.__module__
Out[5]: 'my_module'
my_module.dummy_submodule.DummyClassSubMod.__module__
Out[6]: 'my_module.dummy_submodule'

@LilyFoote
Copy link
Contributor

Hi @b-kiiskila, I think it would be best to open a new issue - it's easier to keep track of open problems if they're in open issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants