diff --git a/pyo3-macros-backend/src/params.rs b/pyo3-macros-backend/src/params.rs index 9517d35b25c..f967149c725 100644 --- a/pyo3-macros-backend/src/params.rs +++ b/pyo3-macros-backend/src/params.rs @@ -262,9 +262,10 @@ pub(crate) fn impl_regular_arg_param( )? } } else { + let unwrap = quote! {unsafe { #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value) }}; quote_arg_span! { #pyo3_path::impl_::extract_argument::from_py_with( - #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value), + #unwrap, #name_str, #from_py_with as fn(_) -> _, )? diff --git a/tests/ui/forbid_unsafe.rs b/tests/ui/forbid_unsafe.rs index 660f5fa36c0..4ab7639d658 100644 --- a/tests/ui/forbid_unsafe.rs +++ b/tests/ui/forbid_unsafe.rs @@ -28,4 +28,16 @@ mod gh_4394 { pub struct Version; } +mod from_py_with { + use pyo3::prelude::*; + use pyo3::types::PyBytes; + + fn bytes_from_py(bytes: &Bound<'_, PyAny>) -> PyResult> { + Ok(bytes.downcast::()?.as_bytes().to_vec()) + } + + #[pyfunction] + fn f(#[pyo3(from_py_with = "bytes_from_py")] _bytes: Vec) {} +} + fn main() {}