diff --git a/newsfragments/3178.fixed.md b/newsfragments/3178.fixed.md new file mode 100644 index 00000000000..706eaf07589 --- /dev/null +++ b/newsfragments/3178.fixed.md @@ -0,0 +1 @@ +Fix a compile error when `#[pymethods]` items come from somewhere else (for example, as a macro argument) and a custom receiver like `Py` is used. diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index 0374df2fa27..1b9f1587217 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -189,10 +189,11 @@ impl SelfType { } } SelfType::TryFromPyCell(span) => { + let _slf = quote! { _slf }; quote_spanned! { *span => let _cell = #cell; #[allow(clippy::useless_conversion)] // In case _slf is PyCell - let _slf = ::std::convert::TryFrom::try_from(_cell)?; + let #_slf = ::std::convert::TryFrom::try_from(_cell)?; } } } diff --git a/tests/test_methods.rs b/tests/test_methods.rs index effd25a1be1..3f9be0a46a2 100644 --- a/tests/test_methods.rs +++ b/tests/test_methods.rs @@ -1470,6 +1470,33 @@ issue_1506!( ) { } + fn issue_1506_mut( + &mut self, + _py: Python<'_>, + _arg: &PyAny, + _args: &PyTuple, + _kwargs: Option<&PyDict>, + ) { + } + + fn issue_1506_custom_receiver( + _slf: Py, + _py: Python<'_>, + _arg: &PyAny, + _args: &PyTuple, + _kwargs: Option<&PyDict>, + ) { + } + + fn issue_1506_custom_receiver_explicit( + _slf: Py, + _py: Python<'_>, + _arg: &PyAny, + _args: &PyTuple, + _kwargs: Option<&PyDict>, + ) { + } + #[new] fn issue_1506_new( _py: Python<'_>,