PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Other
11.44k stars 693 forks source link

Defining `pymethods` in a different module silently fails #4281

Open Rigidity opened 1 week ago

Rigidity commented 1 week ago

Bug Description

Note that the below issue only seems to occur when:

If you define a pyclass in one module, then pymethods in a different module, it fails to expose the bindings to Python without a compile time error. I did this to clean up imports, and would have expected it to not make a difference (similar to how you can impl a normal struct in a different module within the same crate). I tried re-exporting everything from the pybindings module, as well as making it public, to no avail.

Here is the original fix PR I had to make, if it helps https://github.com/Chia-Network/chia_rs/pull/583

Steps to Reproduce

Here is an example of this issue: https://github.com/Rigidity/pyo3-repro

Clone the repo, then run

python3 -m venv venv
. ./venv/bin/activate
pip install maturin

maturin develop -m Cargo.toml
python main.py

Then remove multiple-pymethods from Cargo.toml and run:

maturin develop -m Cargo.toml
python main.py

It now works. You can also remove the submodule module, and put the pymethods in the same module as the Example struct, and it will also work.

Backtrace

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(Example())
TypeError: No constructor defined

Your operating system and version

MacOS Sonoma 14.5

Your Python version (python --version)

Python 3.8.18

Your Rust version (rustc --version)

rustc 1.80.0-nightly (032af18af 2024-06-02)

Your PyO3 version

0.21.2

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

I use pyenv

Additional Info

No response