-
Notifications
You must be signed in to change notification settings - Fork 792
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
ToPyObject
for HashMap<K, V>
panics if ToPyObject
for K
is not Python hashable
#2782
Comments
ToPyObject
for HashMap<K, V>
panics if ToPyObject<K>
is not Python hashableToPyObject
for HashMap<K, V>
panics if ToPyObject
for K
is not Python hashable
Ugh, this is ugly. I don't see a good solution here, except for making all of the conversion traits fallible. |
I wouldn't want to ask for overhead on every other usage of the library for something that seems like a pretty niche edge case. For my purposes, I'd be happy with specific impls for If that's a possibility, and doesn't sound like too much of a dangerous special case, I'd be happy to have a go at implementing it. If not, we have a workaround in place on our side - it's not in a performance-critical place, so it's ok for us to just collect the The |
I think the need for fallible conversion traits is desirable for reasons like this. We've spoken about them previously but just not fleshed out a design. I think adding overriding implementations of In the meanwhile you can always write your own conversion helper at the boundary. Sounds like you are already doing this. |
Thanks for the response - I'd misunderstood how concrete/generic |
Solved with the new |
Bug Description
The conversion traits into Python for hashmaps (both
std::collections
andhashbrown
) fail withPanicException
if the natural conversion type of the key is not hashable in Python. Of course, some sort of failure in general is completely sensible, but a runtimeTypeError
might be more appropriate, since I think there's currently no sensible way of determining at Rust compile-time whether the converted type will be hashable in Python space.Steps to Reproduce
Given a PyO3 project that looks like
This will build in Rust, and install in Python space without issue. From Python space, I can call this function as
For comparison, if I pass an invalid type (like a
float
) for conversion to thecapacity
argument, I get theTypeError
I'd expect.Backtrace
The backtrace from the example above:
Your operating system and version
macOS 12.6
Your Python version (
python --version
)3.10.6
Your Rust version (
rustc --version
)rustc 1.64.0 (a55dd71d5 2022-09-19)
Your PyO3 version
0.17.3
How did you install python? Did you use a virtualenv?
Built from source, executing from within a venv made with
python -m venv
in this case.Additional Info
Given Python-space
tuple[int, int]
can be converted into the Rust-space[usize; 2]
, it could be a useful feature if there were some impls ofHashMap<K, V>
andHashSet<K>
whereK
can be converted to a Pythontuple
instead oflist
- the conversion of these types back to Rust would already work as expected. I'd be interested in trying to contribute that, if it would fit in PyO3.The text was updated successfully, but these errors were encountered: