-
Notifications
You must be signed in to change notification settings - Fork 765
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
add IntoPyObjectExt
trait
#4708
Conversation
@@ -180,6 +180,9 @@ pub trait IntoPy<T>: Sized { | |||
/// | |||
/// It functions similarly to std's [`TryInto`] trait, but requires a [GIL token](Python) | |||
/// as an argument. | |||
/// | |||
/// The [`into_pyobject`][IntoPyObject::into_pyobject] method is designed for maximum flexibility and efficiency; it | |||
/// - allows for a concrete Python type to be returned (the [`Target`][IntoPyObject::Target] associated type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to finish this list, had to run from my pc just now
src/conversion.rs
Outdated
/// This is typically only useful when the resulting output is going to be passed | ||
/// to another function that only needs to borrow the output. | ||
#[inline] | ||
fn into_bound_object_py_any( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this method is open to bikeshedding, I think I'm happy with this but there's probably a better one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we still want to tell users about BoundObject
in the IntoPyObject
docs in the meantime, but it looks like trait probably covers most uses I've seen in downstream code, so the docs I'm adding in #4703 will probably need an update in this PR
Ok(obj) => Ok(obj.into_any()), | ||
Err(err) => Err(err.into()), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think into_unbound_py_any
would also probably be useful (there's a customer in Coroutine::new
it looks like).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable to me; it certainly is common to .unbind()
in pydantic-core
(so that data can be stored in structs, mostly).
I realise that this is non-breaking to add in 0.23.1 as long as we delay prelude addition to 0.24. So while I think something like this is helpful both internally and for users, there is no need to rush a merge. (Especially as users can just copy this trait into their crate downstream in the meanwhile.) |
@Icxolu would love to hear your thoughts on this idea. Do you see alternative ways we could offer simple conveniences on top of |
I was thinking about this for a while now. I agree we should offer some convieniences here, this patter does come up alot. I'm still struggeling a bit with the names. The current ones feel either a bit imprecise or a bit long to me (but I don't have suggestions that I'm completely happy with either). From a functionality point of view I would have 3 methods ins mind:
Other combinations feel less common and should probably go through |
Thanks, I am happy to go with those names with one small adjustment; I went for Agreed about keeping it a separate trait, I had the same thinking. I named your option 3 I think there's some more additional documentation and internal uses to add, if you're happy with this current set of names I'll seek to finish up and push later. |
I also think there is probably space for a separate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I think the diff shows nicely already that this does decrease boilerplate and improves readability. I'm reasonable happy with the names and if other suggestions come up at some point, we can always refactor then.
I also think there is probably space for a separate IntoPyObjectInfallible trait, but I would prefer to wait with that to see how the 2024 edition changes interactions with ! and Infallible.
I agree. I think min_exhaustive_patterns
(1.82+) already helps alot with this and we can afford to wait and see what the 2024 editions brings to the table.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks much nicer overall. Just a small doc suggestion.
Co-authored-by: Icxolu <[email protected]>
Possibly related to #4703
I was wondering how we might be able to simplify uses of
IntoPyObject
in generic code. This PR proposes a helperIntoPyObjectExt
trait which adds some methods which erase the concrete python type and force creation ofPyErr
results.Overall this seems like a good amount of simplification at call sites, so I'll probably use this in
pydantic-core
if we don't land this here until 0.24.Draft because the docs are unfinished and there's also a ton more locations internally this can be used. The kids are sick and need me to comfort them a lot tonight so I think I'm done for the day.