Skip to content

Comments

fix(pyo3-macros): allow pyclass named Probe#5837

Merged
Icxolu merged 1 commit intoPyO3:mainfrom
maurosilber:main
Feb 24, 2026
Merged

fix(pyo3-macros): allow pyclass named Probe#5837
Icxolu merged 1 commit intoPyO3:mainfrom
maurosilber:main

Conversation

@maurosilber
Copy link
Contributor

The * import was shadowing the name of the pyclass.

Fixes #4792.

I tested on a codebase that had one struct/pyclass named Probe, and many other structs/pyclasses.
But I'm not sure if some of the imports that I removed are necessary in other situations that I have not tested.

@alex
Copy link
Contributor

alex commented Feb 23, 2026

Can you add a testcase for this so we don't regress?

@maurosilber
Copy link
Contributor Author

I created a copy of this file, but adding a field to the Probe struct:

#[pyclass]
pub struct Probe {}
#[pymethods]
impl Probe {
#[new]
fn new() -> Self {
Self {}
}
}
#[pymodule]
fn probe(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Probe>()?;
Ok(())
}

and then a new line with the corresponding filename here:
t.pass("tests/ui/pyclass_probe.rs");

But the test does not fail when I tried locally (before applying my changes, I mean). 🤔

@maurosilber maurosilber force-pushed the main branch 3 times, most recently from 26b23af to fd3c5fe Compare February 24, 2026 19:01
Copy link
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to revert the commented cfg and add a newsfragment, otherwise I think this looks good.

The `*` import was shadowing the name of the pyclass.

The PyMethods trait was only defined for the multiple-pymethods feature,
but it cannot be conditionally imported inside the macro.
I just defined the trait unconditionally.

Fixes PyO3#4792.
Copy link
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you!

@Icxolu Icxolu added this pull request to the merge queue Feb 24, 2026
Merged via the queue into PyO3:main with commit c1f3d34 Feb 24, 2026
41 of 42 checks passed
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

Successfully merging this pull request may close these issues.

Confusing error message when using #[pyclass] with a struct called Probe

3 participants