PyO3 / PyO3/pyo3

Defining `pymethods` in a different module silently fails

Open
#4,281 0 comments 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

Note that the below issue only seems to occur when:

  • The binding is defined in a different crate than the entrypoint
  • The multiple-pybindings feature is enabled

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

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

Clone the linked pyo3-repro repository and run the commands in the issue, starting with Cargo.toml, submodule, and main.py. Compare the behavior with and without the multiple-pymethods feature, then trace how the Example pyclass and its pymethods are exposed across modules. Done means the Python Example constructor is available in the multi-module configuration without changing the module layout.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.