-
Notifications
You must be signed in to change notification settings - Fork 787
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 #[pyo3(from_python_with = ...)] attribute #1239
Labels
Comments
cc @gilescope |
Hi, I would like to try to implement this, if it's okay. |
Go for it! (quote! macro is usually pretty helpful with proc macros)
…On Tue, 12 Jan 2021 at 11:03, Daniil Konovalenko ***@***.***> wrote:
Hi, I would like to try to implement this, if it's okay.
I haven't touched procedural macros yet though so I think it may take a
while
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1239 (comment)>, or
unsubscribe
</~https://github.com/notifications/unsubscribe-auth/AAGEJCCP4K73EY4DOD2FGELSZQUBNANCNFSM4SRUEOGQ>
.
|
Please feel welcome to take it on! I'm happy to answer any questions you have. |
7 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've been thinking for a while it would be interesting to have an attribute which can be used to customize the from-python conversion in our proc macros. This need has come up in Gitter as well as #884.
The attribute will go on
#[pyfunction]
arguments or#[derive(FromPyObject)]
fields. It takes a value which is a path to a functionfn (&PyAny) -> Result<T, E>
, whereT
is the annotated type andE: Into<PyErr>
. For examples see the detailed design below.This issue is invite users to comment on the design and for anyone interested to volunteer to implement it (before I eventually get around to it).
In detail, the two applications I see for this attribute are:
In
#[pyfunction]
argumentsConsider the following pyfunction:
Currently
chrono::DateTime
does not implementFromPyObject
, so this function won't compile. An existing workaround is to create a newtypeMyDateTime(chrono::DateTime)
and implementFromPyObject
for that, but it's a lot of boilerplate.The attribute would allow the user to leverage a single function without adding extra types:
On
#[derive(FromPyObject)]
fieldsSimilarly, this applies in a
#[derive(FromPyObject)]
struct:The cool thing here is that this lets us add
#[derive(FromPyObject)]
to a struct which has a field which itself doesn't implementFromPyObject
.Bikeshedding
from_py_with
instead offrom_python_with
?&PyAny
? E.g. in the examples above it could be helpful to take&PyDateTime
. But it's also trivial to convertPyAny
with.downcast()
.The text was updated successfully, but these errors were encountered: