PyO3 / PyO3/pyo3

#[pymodule(module="MODULE")] does not set the __module__ attribute

Open
#4,870 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description
#[pyclass(module = "module")]
struct Foo {}

Correctly sets the __module__ attribute of the class.

The same behavior for other (sub) pymodules is expected.
The attribute is needed for tools such as https://github.com/mkdocstrings/griffe to properly find the submodule. See https://github.com/PyO3/maturin/discussions/1365.

What seems to happen is the module value of the submodule is used to determine the module value of the (inlined) pyclasses.

Steps to Reproduce
  1. Create a module with a submodule. Use module="..."
/// Module documentation: A Python module implemented in Rust.
#[pymodule]
mod mymodule {
    use pyo3::prelude::*;

    /// Sub-Module documentation!!!
    #[pymodule(module="mymodule.mymodule.submodule")]
    mod submodule {
    	use super::*;

        #[pyclass]
        struct Foo{};

        /// Hack: workaround for https://github.com/PyO3/pyo3/issues/759   
        fn init(m: &Bound<'_, PyModule>) -> PyResult<()> {
            Python::with_gil(|py| {
                py.import("sys")?
                    .getattr("modules")?
                    .set_item("mymodule.submodule", m)
            })
        }
   }
}

  1. Check the __module__ attribute`
>>> from mymodule import submodule
>>> submodule.__module__
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    submodule.__module__
AttributeError: module 'submodule' has no attribute '__module__'
Backtrace

Your operating system and version

Fedora 41

Your Python version (python --version)

Python 3.13.1

Your Rust version (rustc --version)

rustc 1.81.0 (eeb90cda1 2024-09-04)

Your PyO3 version

pyo3 v0.23.3 (https://github.com/PyO3/pyo3#5c363b5d)

How did you install python? Did you use a virtualenv?

dnf install python

Additional Info

As a workaround, __module__ attribute could be set manually:

        fn init(m: &Bound<'_, PyModule>) -> PyResult<()> {
            Python::with_gil(|py| {
                py.import("sys")?
                    .getattr("modules")?
                    .set_item("mymodule.submodule", m)
            })?;
            m.setattr("__module__", "mymodule.mymodule.submodule") // <-- Add this
        }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the #[pymodule] and #[pyclass] handling for nested modules in the PyO3 source. Reproduce the example with a nested submodule and inspect the resulting module attribute. Done means the nested module and its classes expose the expected module information without the manual workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.