-
Notifications
You must be signed in to change notification settings - Fork 493
make __eq__ always return a duck array #1123
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
make __eq__ always return a duck array #1123
Conversation
|
|
||
| # TODO: this might be expensive. Do we even need it? | ||
| if eq(self._magnitude, 0, True) and eq(other._magnitude, 0, True): | ||
| return self.dimensionality == other.dimensionality | ||
| return bool_result(self.dimensionality == other.dimensionality) |
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.
I can't figure out what this block is for. I tried removing the whole block and rerun the tests, but this didn't break any tests. Since this is potentially expensive (we might end up comparing the values multiple times on huge arrays) I'd suggest removing this:
| # TODO: this might be expensive. Do we even need it? | |
| if eq(self._magnitude, 0, True) and eq(other._magnitude, 0, True): | |
| return self.dimensionality == other.dimensionality | |
| return bool_result(self.dimensionality == other.dimensionality) |
|
While I am in principle ok about making the output more predictable and easy to understand, I think it would be good to put here (or in an issue) some examples of current and expected behavior so more people can be aware of this (breaking) change. |
|
okay, then here are the examples (mostly from the test cases): u = np.ones((10,)) * ureg.dimensionless
v = np.zeros_like(u) * ureg.m
w = np.ones_like(u) * ureg.m
# compare a quantity with itself
u == u # array-like, same as before
# compare dimensionless with non-quantity
u == 1 # array-like, before this was a single bool
# compare quantity with dimensionality with non-quantity
v == 1 # array-like, before this was a single bool
# compare quantities with different units and with the values being all 0
np.zeros_like(u) * ureg.m == np.zeros_like(u) * ureg.s
# array-like, before this returned a single bool
# compare two quantities with equal units
v == w # array-like, same as before
# compare two quantities with compatible units
v == w.to("mm") # array-like, same as before
# compare quantities with different units and different values
u == v # array-like, before this was a single boolas a summary: if the other quantity has a different unit the behaviour changed. Before it returned a single bool, now it returns an array filled with that bool. |
which exposed a bug in the bool_result function
|
This looks good to me and there has not been any comment. I am merging so it can be included in the next release bors r+ |
|
Build succeeded: |
When comparing quantities with different units / dimensionalities but containing duck arrays for equality, the returned value tends to change between boolean arrays and python boolean objects depending on the input. This makes it somewhat difficult to write code / tests against it and with this I'd propose to change that to always return arrays if we're using array magnitudes.
I'm not quite sure where
bool_resultshould live, maybecompat.py?black -t py36 . && isort -rc . && flake8with no errors