-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Remove Quantity.__eq__, __ne__ so that == and != always return True, False #2328
Conversation
p.s. An additional advantage is that |
Yeah, I guess it makes sense for this to follow the more common behavior, so it seems fine to me. You should add this to "backwards-incompatible changes" in the 0.4 what's new document, though, as it's a potential "gotcha" for legacy code. |
oh, but it would be good to hear from @adrn, @astrofrog, and/or @mdboom or this one... |
You can have a Quantity storing a structured array? I thought we explicitly only allow |
This looks good. One other thing this fixes (and we should add an explicit test for) is:
Before, one would have to explicitly fall back on Unit comparison:
|
No, it is not explicitly excluded (and I am much in favour of not |
I guess I'm missing how a structured array in a Quantity is sensible. What's the use case? |
@mdboom - so far, I've only been thinking about a vector, where the components would be in a structured array. But it is not obvious that this is handier than having x,y,z components in the last dimension. Anyway, this PR is not specifically for that. |
👍 |
👍 from me too, so if @mdboom is happy, feel free to merge |
@mdboom - OK to merge? If so, I can remove |
with pytest.raises(u.UnitsError): | ||
u.Quantity(1, unit='m') == u.Quantity(1, unit='s') | ||
# for ==, !=, return False, True if units do not match | ||
assert u.Quantity(1100, unit=u.m) != u.Quantity(1, unit=u.s) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's important that we maintain a subtle feature of this fix. If the units are convertible, we get an array of results (and it's a vectorized array comparison), and if the units are not convertible, NotImplemented
is raised and the result is False
In [4]: 3.0 * u.s == 3.0 * u.m
Out[4]: False
In [5]: 3.0 * u.m == 280.0 * u.cm
Out[5]: array(False, dtype=bool)
So maybe this line could be:
assert (u.Quantity(1100, unit=u.m) != u.Quantity(1, unit=u.s)) is True
assert (u.Quantity(1100, unit=u.m) == u.Quantity(1, unit=u.s)) is False
Other than my comment about tightening up the tests, I think this is good to go. |
@mdboom - thanks, implemented. As it passed travis just now, I'll merge. |
Remove Quantity.__eq__, __ne__ so that == and != always return True, False
Currently,
q1==q2
andq1!=q2
raiseUnitsError
if the units do not match. This, however, would seem overly protective of the user, and inconsistent with python usage elsewhere, where==
always returnsFalse
is objects are incompatible. Simply removing the__eq__
and__ne__
fromQuantity
, and thus lettingndarray
take care, solves this.As a byproduct, one now gets the same result for
unit == (1.*unit)
and(1.*unit) == unit
, sinceQuantity
returnsNotImplemented
andUnit
decides that it can turn the quantity into a unit (arguably, it shouldn't, but that is a problem forUnit
, and at least it should be symmetric!).