Skip to content

Commit fd3c5fe

Browse files
committed
fix(pyo3-macros): allow pyclass named Probe
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 #4792.
1 parent 3ffad5a commit fd3c5fe

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

pyo3-macros-backend/src/pyclass.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3010,13 +3010,14 @@ impl<'a> PyClassImplsBuilder<'a> {
30103010
type BaseNativeType = #base_nativetype;
30113011

30123012
fn items_iter() -> #pyo3_path::impl_::pyclass::PyClassItemsIter {
3013-
use #pyo3_path::impl_::pyclass::*;
3014-
let collector = PyClassImplCollector::<Self>::new();
3015-
static INTRINSIC_ITEMS: PyClassItems = PyClassItems {
3013+
use #pyo3_path::impl_::pyclass::PyMethods as _;
3014+
use #pyo3_path::types::PyAnyMethods as _;
3015+
let collector = #pyo3_path::impl_::pyclass::PyClassImplCollector::<Self>::new();
3016+
static INTRINSIC_ITEMS: #pyo3_path::impl_::pyclass::PyClassItems = #pyo3_path::impl_::pyclass::PyClassItems {
30163017
methods: &[#(#default_method_defs),*],
30173018
slots: &[#(#default_slot_defs),* #(#freelist_slots),*],
30183019
};
3019-
PyClassItemsIter::new(&INTRINSIC_ITEMS, #pymethods_items)
3020+
#pyo3_path::impl_::pyclass::PyClassItemsIter::new(&INTRINSIC_ITEMS, #pymethods_items)
30203021
}
30213022

30223023
const RAW_DOC: &'static ::std::ffi::CStr = #doc;

src/impl_/pyclass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ pub trait PyClassInventory: inventory::Collect {
10021002
}
10031003

10041004
// Items from #[pymethods] if not using inventory.
1005-
#[cfg(not(feature = "multiple-pymethods"))]
1005+
// #[cfg(not(feature = "multiple-pymethods"))]
10061006
pub trait PyMethods<T> {
10071007
fn py_methods(self) -> &'static PyClassItems;
10081008
}

tests/ui/pyclass_probe.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
#![deny(unused_imports)]
22
use pyo3::prelude::*;
33

4-
#[pyclass]
5-
pub struct Probe {}
6-
7-
#[pymethods]
8-
impl Probe {
9-
#[new]
10-
fn new() -> Self {
11-
Self {}
4+
#[pymodule]
5+
mod probe_no_fields {
6+
use pyo3::prelude::*;
7+
#[pyclass]
8+
pub struct Probe {}
9+
10+
#[pymethods]
11+
impl Probe {
12+
#[new]
13+
fn new() -> Self {
14+
Self {}
15+
}
1216
}
1317
}
1418

1519
#[pymodule]
16-
fn probe(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
17-
m.add_class::<Probe>()?;
18-
Ok(())
20+
mod probe_with_fields {
21+
use pyo3::prelude::*;
22+
#[pyclass(get_all)]
23+
pub struct Probe {
24+
field: u8,
25+
}
26+
27+
#[pymethods]
28+
impl Probe {
29+
#[new]
30+
fn new() -> Self {
31+
Self { field: 0 }
32+
}
33+
}
1934
}
2035

2136
#[pyclass]

0 commit comments

Comments
 (0)