Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) -> _,
)?
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ 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)),
}
}

/// 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 {
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that Extractor should be just temporary code which we'll remove again when the GIL Ref API is dead and gone, I'm fine with leaving it here for now 👍

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()),
Expand Down
Loading