-
Notifications
You must be signed in to change notification settings - Fork 49
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
hashability of unit_quantity objects #143
Comments
This is happening because under the hood
Fundamentally the problem is that
I'm not sure we can change this without introducing a breaking change, although I don't know offhand if it would actually break any real-world code. It's also a little weird to me that this code works, I don't think it's correct that astropy lets you hash a mutable object, or that the hash of an object can change by doing something to it.
|
We've come across this same use case by chance, and settled on converting values to tuple (although we are bending the rules by even intending to hash mutable objects) |
Some additional context about how hashing in general is difficult and what bad things happen when you allow hashing of mutable objects where equality for the object depends on the object's state: https://www.asmeurer.com/blog/posts/what-happens-when-you-mess-with-hashing-in-python/ And also some related context about why hashing numeric types in particular is also difficult: However, there is probably a way to make hashing of objects that have different units but are nonetheless equal have the same hashes by following Python's approach for numeric types: https://docs.python.org/3/library/stdtypes.html#hashing-of-numeric-types One way forward would be to force If not then another way forward would be to define a new class that represents an immutable scalar quantity, and a way to go from the mutable version to the immutable version when you want to generate hash keys. |
Or maybe just a helper function that returns a tuple of the quantity's value and unit, which are both individually immutable and can be used as dict keys when paired in a tuple. |
While I don’t consider having two types with identical semantics but different implementations in the same package to be an elegant choice, it’s an acceptable compromise. So, is this approach currently implemented? Also, the fact that |
it is not.
|
Description
It is possible to compute the hash of
unyt.unit_object.Unit
, but not ofunyt.array.unyt_quantity
. Is there any reason for this?In astropy this is possible (but only of scalar unit objects)
What I don't like about astropy's hash implementation is that the hashes of numerically equivalent quantities are different
The text was updated successfully, but these errors were encountered: