PyO3 / PyO3/pyo3

Having an 'x' getter and a 'get_x' method leads to an internal naming conflict

Open
#5,974 5 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

In one of my pyo3 projects, I need to have both an 'x' attribute and a 'get_x' method with additional parameters for some more specialized calculations of the value. This naming is necessary for backwards compatibility.

However, this produces an internal compiler error due to naming conflict.

Below you can see that even using 'name = ...' doesn't fix the issue.

Steps to Reproduce

pyo3_naming_limitation working example

Working example, just cargo build. Here is the code contained immediately, for convenience:

#[pyclass]
pub struct Object {
    x: u32,
    y: u32,
}

#[pymethods]
impl Object {
    #[getter]
    fn x(&self) -> u32 {
        self.x
    }

    fn get_x(&self) -> u32 {
        self.x
    }

    #[getter]
    fn y(&self) -> u32 {
        self.y
    }

    #[pyo3(name = "get_y")]
    fn y_get(&self) -> u32 {
        self.y
    }

}
Backtrace
error[E0592]: duplicate definitions with name `__pymethod_get_x__`
  --> src/lib.rs:21:1
   |
21 | #[pymethods]
   | ^^^^^^^^^^^^
   | |
   | duplicate definitions for `__pymethod_get_x__`
   | other definition for `__pymethod_get_x__`
   |
   = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0592]: duplicate definitions with name `__pymethod_get_y__`
  --> src/lib.rs:21:1
   |
21 | #[pymethods]
   | ^^^^^^^^^^^^
   | |
   | duplicate definitions for `__pymethod_get_y__`
   | other definition for `__pymethod_get_y__`
   |
   = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0592`.
error: could not compile `pyo3_limitation` (lib) due to 2 previous errors
Your operating system and version

Debian GNU/Linux 13 (trixie)

Your Python version (python --version)

3.13.9

Your Rust version (rustc --version)

1.94.1

Your PyO3 version

0.27.0

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

This can be replicated without running python.

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

Start with the inline src/lib.rs reproduction or the linked pyo3_naming_limitation project and run cargo build to confirm the duplicate pymethod_get_x and pymethod_get_y errors. Then inspect PyO3's #[pymethods] attribute macro handling of getters and renamed methods; done means the example compiles while exposing both the x getter and get_x method, including the named y_get case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.