Skip to content

Commit f08cc60

Browse files
authored
Fix Probe class naming issue with #[pymethods] (#4988)
Commit 603a55f ("fix `#[pyclass]` could not be named `Probe` (#4794)") fixed the issue where it was not possible to define a class named `Probe`. However, similar issue exists when trying to add `#[pymethods]` implementations to the `Probe` class. It generates similar confusing errors as the original issue (#4792), due to the internal `Probe` trait being in scope at the user code: error[E0782]: expected a type, found a trait help: you can add the `dyn` keyword if you want a trait object # | impl dyn Probe { Fix the issue by avoiding pollution of the imports from the macro expansion and instead use qualified path to the internal types.
1 parent 8e7ac5d commit f08cc60

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

newsfragments/4988.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `Probe` class naming issue with `#[pymethods]`

pyo3-macros-backend/src/pymethod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,8 @@ pub fn impl_py_method_def_new(
367367
args: *mut #pyo3_path::ffi::PyObject,
368368
kwargs: *mut #pyo3_path::ffi::PyObject,
369369
) -> *mut #pyo3_path::ffi::PyObject {
370-
use #pyo3_path::impl_::pyclass::*;
371370
#[allow(unknown_lints, non_local_definitions)]
372-
impl PyClassNewTextSignature<#cls> for PyClassImplCollector<#cls> {
371+
impl #pyo3_path::impl_::pyclass::PyClassNewTextSignature<#cls> for #pyo3_path::impl_::pyclass::PyClassImplCollector<#cls> {
373372
#[inline]
374373
fn new_text_signature(self) -> ::std::option::Option<&'static str> {
375374
#text_signature_body

tests/ui/pyclass_probe.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,18 @@ use pyo3::prelude::*;
33
#[pyclass]
44
pub struct Probe {}
55

6+
#[pymethods]
7+
impl Probe {
8+
#[new]
9+
fn new() -> Self {
10+
Self {}
11+
}
12+
}
13+
14+
#[pymodule]
15+
fn probe(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
16+
m.add_class::<Probe>()?;
17+
Ok(())
18+
}
19+
620
fn main() {}

0 commit comments

Comments
 (0)