Skip to content

Commit c1f3d34

Browse files
authored
fix(pyo3-macros): allow pyclass named Probe (#5837)
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 c1f3d34

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

newsfragments/5837.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow pyclass named `Probe` with `get_all` fields.

pyo3-macros-backend/src/pyclass.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,7 +2829,11 @@ impl<'a> PyClassImplsBuilder<'a> {
28292829
};
28302830

28312831
let (pymethods_items, inventory, inventory_class) = match self.methods_type {
2832-
PyClassMethodsType::Specialization => (quote! { collector.py_methods() }, None, None),
2832+
PyClassMethodsType::Specialization => (
2833+
quote! {{ use #pyo3_path::impl_::pyclass::PyMethods as _; collector.py_methods() }},
2834+
None,
2835+
None,
2836+
),
28332837
PyClassMethodsType::Inventory => {
28342838
// To allow multiple #[pymethods] block, we define inventory types.
28352839
let inventory_class_name = syn::Ident::new(
@@ -3010,13 +3014,12 @@ impl<'a> PyClassImplsBuilder<'a> {
30103014
type BaseNativeType = #base_nativetype;
30113015

30123016
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 {
3017+
let collector = #pyo3_path::impl_::pyclass::PyClassImplCollector::<Self>::new();
3018+
static INTRINSIC_ITEMS: #pyo3_path::impl_::pyclass::PyClassItems = #pyo3_path::impl_::pyclass::PyClassItems {
30163019
methods: &[#(#default_method_defs),*],
30173020
slots: &[#(#default_slot_defs),* #(#freelist_slots),*],
30183021
};
3019-
PyClassItemsIter::new(&INTRINSIC_ITEMS, #pymethods_items)
3022+
#pyo3_path::impl_::pyclass::PyClassItemsIter::new(&INTRINSIC_ITEMS, #pymethods_items)
30203023
}
30213024

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

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)