-
-
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
bugfix in ApproxNumpy initialisation, use keywords for arguments to fix #3696
bugfix in ApproxNumpy initialisation, use keywords for arguments to fix #3696
Conversation
Let's add a quick test since the current code doesn't actually ensure |
You think add a test to check tolerances, or just make the current tests such that abs & rel tolerances aren't equivalent. I can push a commit either way this eve. |
Anything that fails before your change and passes afterwards. Either approach is fine though the "different params" one is what I'd probably go for. |
Something else is broken here. I typo'd the last rel argument, so it's 10% instead of 1%. |
update test to include both np.array(actual) and np.array(expected)
Justification for f0db64a: currently pytest will calculate the tolerances against either value if it's a numpy array. With the earlier commits, the below will still pass. Unless I'm missing something the approx should be 1±1 therefore it should be wrong. pytest/src/_pytest/python_api.py Line 214 in b7419bd
as we already have our self.expected and self.tolerance. so I'm not sure why there would be another Approx initialization. This may not elegantly handle nans but should just fail. |
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.
LGTM, thanks a lot @abrammer!
testing/python/approx.py
Outdated
assert np.array(actual) == pytest.approx(expected, abs=0, rel=0.01) | ||
assert actual == pytest.approx(np.array(expected), abs=0, rel=0.01) | ||
assert np.array(actual) == pytest.approx(np.array(expected), abs=0, rel=0.01) | ||
|
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.
Thanks for the fix @abrammer!
What do you think about improving this test so that it is even more complete?
I'm thinking about adding these complementary checks also:
assert 0.099 == pytest.approx(0.1, abs=0.01, rel=0)
assert np.array(0.099) == pytest.approx(0.1, abs=0.01, rel=0)
# ... other variants with np.array in other positions ...
assert 0.099 != pytest.approx(0.1, abs=0, rel=0.005)
assert np.array(0.099) != pytest.approx(0.1, abs=0, rel=0.005)
# ... other variants with np.array in other positions ...
also, perhaps, let's add the equivalent for all tests, but with pytest.approx
on the left-side. It would be a total of 32 asserts, but the more tested it is, the better ;)
The extra I think the best way to write this line would be like this: if _is_numpy_array(actual):
return all(a == self for a in actual) Thanks for the PR and the tests (clearly this code needs more tests)! |
Thanks @kalekundert for the suggestion! I implemented it myself, I think we can merge this for now, it has been waiting here long enough. I encourage others to open PRs later to improve the tests if needed! 👍 |
I have changes and tests coming, away from my computer for a couple days so will update with respect to the @kalekundert suggestion and tests on Sunday. @nicoddemus that will fail on a 0d(?) array. E.g. np.array(100). If you can hold off until Sunday I'll get a commit in with chnages to cover that and tests for nans as well. |
@abrammer sure thing, let's wait for you to get around to this then! Thanks a lot for following up with this. 👍 |
Just brainstorming, but it would be less errorprone to maintain if all corner cases are tested (it's hard to remember all corner cases when maintaining the code):
Perhaps an approach with |
Looks good to me. I think one of |
Yeah you're right. Flatten copies, flat just iterates. Nice. |
@abrammer, awesome improvement on the tests ;) |
With respect to #3695 , flip the arguments in the initialization and also use keywords so it's more obvious and less likely to happen in future.
Can add tests along the lines of the code in #3695 if desired but it seems like an obvious typo fix