-
Notifications
You must be signed in to change notification settings - Fork 784
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
Type error when using (None)
as the default value in function signature
#3460
Comments
Unfortunately, I’m not sure how to fix this without removing the special treatment of |
I have mixed feelings about the I agree that this edge case is a bit horrible though, and in general I don't like implementations which rely on recognising tokens at text. For example, if you put I suggest two ways forward:
|
I’m currently just forwarding this if default_value_s == "None" {
quote!(#name = None)
} else {
quote!(#name = #default_value.convert())
} Aliasing Some trait-based solution would be interesting. Maybe some approach using deref specialization to use wrapping trait if available or fall back to identity function? I’ll try to write an example later today. |
@davidhewitt This looks like a proper solution: I probably won’t have time in the near future to actually implement this though. |
Note that it could trivially be abstracted over the wrapper type: |
Thanks! Having been successfully nerd-sniped I managed to reduce the trait to a relatively simple form to solve the exact problem we needed. #3461 |
Thanks for the fix! It looks like it would solve the problem. As a sidenote though, deref-based version doesn't rely on textually detecting |
Agreed, unfortunately we also detect text The deref-based version you shared ran into other complexities - e.g. we needed to specify the type annotation in the macro, but we need to strip all lifetimes from the type annotation first because we're moving it from a function signature into the caller. Possibly a future refinement. |
Bug Description
This part in pyo3 macros:
pyo3/pyo3-macros-backend/src/params.rs
Lines 192 to 201 in 8f4a26a
special-cases the ident
None
when it’s used as the default value for a parameter. Unfortunately, it doesn’t work for any other expression evaluating toNone
, e.g.(None)
.This line looks like it also should be fixed:
pyo3/pyo3-macros-backend/src/pyfunction/signature.rs
Line 483 in 8f4a26a
Steps to Reproduce
Try to compile this code:
Your operating system and version
NixOS
Your Python version (
python --version
)Python 3.10
Your Rust version (
rustc --version
)rustc 1.74.0-nightly (e3abbd499 2023-09-06)
Your PyO3 version
0.19.2
How did you install python? Did you use a virtualenv?
System repositories
Additional Info
Using arbitrary expressions evaluating to
None
may seem like a strange thing to want, but I’m autogenerating Python bindings and my defaults are something likeNone.convert_to_type_that_impls_IntoPy()
. I’d rather not special-caseNone
here.The text was updated successfully, but these errors were encountered: