-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
pytest.approx considers boolean numeric types #9353
Comments
I'm not so sure what to do about this. The idea of #7710 was to use approximate comparison whenever One straightforward solution to special-case the equality fallback, so approximate comparisons are used for all subclasses of |
Fixes #9353 (cherry picked from commit a16e8ea) Co-authored-by: Jakob van Santen <[email protected]>
Fix sherpa#2202 In pytest 8.3.3 and earlier >>> import numpy as np; import pytest >>> pytest.__version__ '8.3.3' >>> np.ones(2, dtype=bool) == pytest.approx([True, True]) True However, pytest 8.3.4 now causes this to fail >>> import numpy as np; import pytest >>> pytest.__version__ '8.3.4' >>> np.ones(2, dtype=bool) == pytest.approx([True, True]) False This is because of "pytest.approx considers boolean numeric types" pytest-dev/pytest#9353 The solution is to make the "expected" value be a ndarray, and so >>> np.ones(2, dtype=bool) == pytest.approx(np.asarray([True, True])) True holds with both pytest 8.3.3 and 8.3.4. So this commit basically goes through and updates the tests so that we use a ndarray for boolean arrays. An alternative would be to change from assert got == pytest.approx(expected) to something like assert np.all(got == expected) However, the error message when the array lengths are different or an element is different are a **lot less** useful, and the change would be even-more invasive than this change.
From the docs:
However, that seems to treat booleans as numerical values too:
results in:
(on Python 3.9.7 and pytest 6.2.5). Note the
False ± 1.0e-12
. While this is technically correct, it doesn't seem like a very reasonable way of comparing booleans.cc @jvansanten who seems to have implemented this originally in #7710.
The text was updated successfully, but these errors were encountered: