-
-
Notifications
You must be signed in to change notification settings - Fork 2.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
Add support for pytest.approx
comparisons between array and scalar
#3313
Conversation
…lder numpy versions
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.
Awesome, thanks @tadeu!
@kalekundert would you like to take a look as well? |
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 took me a while to realize that in the ApproxNumpy
class, both self.expected
and actual
can now be scalars. So I think it'd be good to mention that (and to describe how self.expected
can end up as a scalar, because it's pretty subtle) in the docstring.
It's a little unfortunate that ApproxNumpy
gets so much more complicated having to handle 3 combinations of different expected/actual types. You could possibly simplify things by not allowing self.expected
to be a scalar, and handling the case where the expected value is a scalar and the actual value is an array in ApproxScalar
without invoking ApproxNumpy
, e.g.:
all(
actual[i] == self._approx_scalar(self.expected)
for i in np.ndindex(actual.shape)
)
_pytest/python_api.py
Outdated
@@ -100,8 +103,16 @@ def _yield_comparisons(self, actual): | |||
# We can be sure that `actual` is a numpy array, because it'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.
Can you update this comment to note that self.expected
and actual
can both be either arrays or scalars?
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.
Sure, I'm changing it to
# For both `actual` and `self.expected`, they can independently be
# either a `numpy.array` or a scalar (but both can't be scalar,
# in this case an `ApproxScalar` is used).
# They are treated in `__eq__` before being passed to
# `ApproxBase.__eq__`, which is the only method that calls this one.
@@ -69,9 +73,6 @@ class ApproxNumpy(ApproxBase): | |||
Perform approximate comparisons for numpy arrays. | |||
""" | |||
|
|||
# Tell numpy to use our `__eq__` operator instead of its. | |||
__array_priority__ = 100 | |||
|
|||
def __repr__(self): |
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.
If self.expected
can be a scalar, __repr__
should probably be able to handle that. Although it's more of a philosophical concern than a practical one, since __repr__
shouldn't be called with a scalar self.expected
as the code currently stands. Still, who knows what could happen in the future.
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.
You're right, I'll to improve this
(done)
Hmm, this could be done, but I think it would actually complicate things a liitle bit more, because the part to display a nice message like
would also need to be reimplemented (get which value is different, etc.). |
@kalekundert all done, would you like to take another look? |
Either way, The more I think about it, the more I think Anyways, I'm worried that I'm losing sight of the fact that the code is totally fine. :-) So I'll leave it to your judgment. If you think it'd be better (and worth the effort) to require |
So that `self.expected` in ApproxNumpy is always a numpy array.
Hi @kalekundert, I agree with you, I've changed the code so that |
Awesome, thanks for the PR! |
Great, thanks @tadeu and @kalekundert! |
fixes #3312