Open CalCraven opened 1 year ago
I don't understand the purpose of comparing a unyt_array to a string. What context does this pop into ?
I'm also not sure this is something we can or should fix, because it was never documented as reliable or tested.
Yes that's a fair point. We have a package that has used it previously to compare many attributes of an object, some of which are defaulted to a null string, and only write out ones that have been modified. It's easy enough for us to change this comparison to something a little more rigorous, just thought it should be brought to everyone attention in case anyone else has the same bug coming up.
I think it would probably be ok to just universally return False for equality comparisons with types that aren’t instances of unyt_array
. Seems sensible to me.
I think it would probably be ok to just universally return False for equality comparisons with types that aren’t instances of unyt_array.
this would break any existing comparison between an unyt_array
(a
) and a np.ndarray
(b
), which currently works the same as comparing a.ndview
with b
.
Of course we’d need to treat ndarrays differently, I meant any random python type like string or other unrelated object.
Makes sense to me. How about comparing scalar arrays (unyt_quantity) with stuff like int
or float
?
Probably just needs a call to np.asanyarray
in __eq__
to handle all array-likes, then anything that ends up with a string or object dtype gets rejected.
strings may need extra care too
>>> import numpy as np
>>> np.asanyarray("hello")
array('hello', dtype='<U5')
Right, that's why I said,
then anything that ends up with a string or object dtype gets rejected
I guess also datetimes too. U5
is a string dtype.
Description
Comparing unyt arrays to strings has been deprecated by 1.25.0 release of numpy. According to the patch notes, universal functions for numpy arrays can be overwritten by a subclass, causing differences in comparison. This comes into play by raising an error if you try to compare a
unyt_array
object to a null string, which works with numpy 1.24.2 and below as far as we've tested.What I Did
Outputs
Working code
Outputs