allow `from_py_with` on function args to take a `fn(&Bound) -> PyResult` (#3837)

This commit is contained in:
Icxolu 2024-02-14 23:03:04 +01:00 committed by GitHub
parent 0c12d9137f
commit 9902633116
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 12 deletions

View File

@ -217,18 +217,18 @@ fn impl_arg_param(
quote_arg_span! {
#[allow(clippy::redundant_closure)]
_pyo3::impl_::extract_argument::from_py_with_with_default(
#arg_value,
#arg_value.map(_pyo3::PyNativeType::as_borrowed).as_deref(),
#name_str,
#expr_path,
#expr_path as fn(_) -> _,
|| #default
)?
}
} else {
quote_arg_span! {
_pyo3::impl_::extract_argument::from_py_with(
_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value),
&_pyo3::impl_::extract_argument::unwrap_required_argument(#arg_value).as_borrowed(),
#name_str,
#expr_path,
#expr_path as fn(_) -> _,
)?
}
}

View File

@ -133,12 +133,12 @@ where
/// Alternative to [`extract_argument`] used when the argument has a `#[pyo3(from_py_with)]` annotation.
#[doc(hidden)]
pub fn from_py_with<'py, T>(
obj: &'py PyAny,
pub fn from_py_with<'a, 'py, T>(
obj: &'a Bound<'py, PyAny>,
arg_name: &str,
extractor: fn(&'py PyAny) -> PyResult<T>,
extractor: impl Into<super::frompyobject::Extractor<'a, 'py, T>>,
) -> PyResult<T> {
match extractor(obj) {
match extractor.into().call(obj) {
Ok(value) => Ok(value),
Err(e) => Err(argument_extraction_error(obj.py(), arg_name, e)),
}
@ -146,10 +146,10 @@ pub fn from_py_with<'py, T>(
/// Alternative to [`extract_argument`] used when the argument has a `#[pyo3(from_py_with)]` annotation and also a default value.
#[doc(hidden)]
pub fn from_py_with_with_default<'py, T>(
obj: Option<&'py PyAny>,
pub fn from_py_with_with_default<'a, 'py, T>(
obj: Option<&'a Bound<'py, PyAny>>,
arg_name: &str,
extractor: fn(&'py PyAny) -> PyResult<T>,
extractor: impl Into<super::frompyobject::Extractor<'a, 'py, T>>,
default: fn() -> T,
) -> PyResult<T> {
match obj {

View File

@ -20,7 +20,7 @@ impl<'a, T> From<fn(&'a PyAny) -> PyResult<T>> for Extractor<'a, '_, T> {
}
impl<'a, 'py, T> Extractor<'a, 'py, T> {
fn call(self, obj: &'a Bound<'py, PyAny>) -> PyResult<T> {
pub(crate) fn call(self, obj: &'a Bound<'py, PyAny>) -> PyResult<T> {
match self {
Extractor::Bound(f) => f(obj),
Extractor::GilRef(f) => f(obj.as_gil_ref()),