-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
dtype richcompare doesn't respect protocol #5345
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
Comments
Ugh. dtype hashing is... kinda a mess. Dtypes are hashable objects that are nonetheless mutable (though if isinstance(other, np.dtype): np.dtype coercion itself is happy to interpret a startling variety of And, of course, dtype.hash makes no attempt to be consistent with In theory we could try to clean up bits of this (like, maybe suppress Any better ideas? On Wed, Dec 3, 2014 at 2:58 PM, Antoine Pitrou notifications@github.com wrote:
Nathaniel J. Smith |
Would it? It's not any worse then doing it when the object being compared is a plain string. |
As for heterogenous dicts, that's why I brought the lru_cache() example. There are situations where it can make sense to have different key types in a dict. |
Yeah, in an ideal world dtype.eq probably wouldn't try to coerce I definitely see the use case for heterogenous dicts. I just don't see a
|
Ouch, I just found this out by trying to put a dtype into a type that wants to be hashable. Thanks for the clear writeup! I understand why equality is violates the python API, but couldn't hash be fixed? I'm imagining
I can definitely see an argument for hashing dtypes differently from builtin types even though the equality function would return true in that case (which would help me immensely for my use case). In this case, maybe you could return the hash of immutable parameters, like byte order, alignment, size, and builtin type used to construct you. Is this reasonable? Further, as a workaround, for my custom type, is it sufficient to do something like this?
|
Closing this as a duplicate of gh-7242 (both issues have quite a lot of discussion). |
When a type's
__eq__
(ortp_richcompare
in C) doesn't understand the other type, it should return NotImplemented so that the other type's__eq__
has a chance to be called. Unfortunately, NumPy's ArrayDesrc doesn't respect that convention and raises TypeError instead, making comparisons fragile:It is generally considered bad practice to raise TypeError in a comparison method, and can prevent advanced uses: such as defining a custom type comparable with ArrayDescr, or simply mixing ArrayDescr with other types in e.g. a dictionary.
Indeed let's say I use the standard
lru_cache
, then hash collisions can produce unwanted errors because of the dictionary used internally comparing the keys:The text was updated successfully, but these errors were encountered: