-
Notifications
You must be signed in to change notification settings - Fork 779
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
implicit conversions #2482
Comments
Hi, thanks for a great question. I'm aware of the One thing to note is that Rust as a language chose not to have any implicit conversions. So if you view packages created by PyO3 as exposing Rust APIs then it may not be relevant to consider implicit conversions. Similarly In my opinion Python as a language is also moving away from duck-typed functions to static typing. So the fit for implicit conversions on the Python side is perhaps less than it was when When I last considered it myself I didn't think we need this right now and was undecided on whether I'd want it in the long term. If a majority of users would like to have this feature, we can of course consider it. |
It's perhaps worth pointing out that without implicit conversions built into the PyO3 framework, you can always write functions which take |
i agree on function overloading not being a good fit.
on the other side, libraries like pydantic are popular as never before and they are indeed casting types implicitly when possible
Maybe we could have a macro to annotate implicit conversion? Changing the function signature to take pyobjectany would always require calling functions with a python object, even from within rust? |
Sorry for the delay.
So there are two ways a crate other than PyO3 could already support this:
So at the moment I don't see a need to invest in this for the PyO3 core. I propose we revisit in the future if a crate is published with this functionality and proves to be extremely popular. |
I can see one other solution: Make a |
Thanks for the great work you do on PyO3.
Pybind11 has a feature called 'implicit conversions'
https://pybind11.readthedocs.io/en/stable/advanced/classes.html#implicit-conversions
This can be quite handy, let me give an example:
In Pybind11 Vector3D could be declared as implicitly convertible from list and
do_something([1,2,3])
would be valid and implicitly casting the list to a Vector3D object.Is this something that could be done in pyo3? It is quite handy and can save a lot of verbosity...
The text was updated successfully, but these errors were encountered: