-
-
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
Approx with inequalities #2003
Comments
I started some drafts on it. Just wondering, what would be a best way to handle sequences? I've considered:
Inequality is likely a misleading name, but I don't know a general english word for >, >=, <, <= operations. Decision made here also should be complaint with whatever will be decided about #1994. |
I dont think we want a situation where these give wildly different results: assert arr <= approx(0.5)
assert arr <= 0.5 Perhaps the best is to simply defer the evaluation to the corresponding operator of the underlying type? |
I think unconditionally raising a |
The same logic could be applied to the very existence of |
IMHO no, because for equality there's more logic involved, the equivalent code would be (roughly) Or is there a use case for |
@nicoddemus |
Hmm I see, thanks. Well if it is simple to implement I guess it wouldn't hurt supporting it then. |
This is exactly what I was aiming at. |
I'd really like to see this to be supported thus I had a quick look into it. I encountered some issue with the implementation though: assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10)
assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10) In the second example one could expect The Python docs write the following about it:
I don't see another way but to restrict that This is not very intuitive at all and will eventually lead to confusion - IMHO, even if the documentation about it would be sufficient to understand it. Any thoughts about it? |
I agree that users might get tripped by this, but given that the language itself doesn't allow for us to implement the operator when Perhaps @kalekundert (who added |
Yeah, I agree. I'll finish the work I've done so far and I'll submit a PR for review in the next few days, if that's okay. |
Hmmm, I don't understand why one would expect |
My expectation from approx would be that it should always be "on the generous side" towards As an example of where I would use approx for stuff like this, consider this: for i in range(100):
assert 1 + 2 ** (-i) > 1 which (at |
I think I see where you're coming from now ( I think |
Another option which is perfectly valid would be to simply use strict checking for >>> pytest.approx(1.0 + 1e-5) > 1.0
False just because we wanted to be generous with >>> pytest.approx(1.0 + 1e-5) <= 1.0
True |
I would be ok with me if we just implemented |
I agree, simply raising |
I followed the discussion briefly and while at EuroPython I thought why not give it a shot as this is marked easy :). |
Looks good to me. But you should pull from the features branch again before making a pull request. I did some pretty significant refactoring to the |
Okay good to know. I will do that. I wasn't sure, as this was tagged as "bug" and the contribution guidelines say that one should branch from master (https://github.com/pytest-dev/pytest/blob/master/CONTRIBUTING.rst#preparing-pull-requests-on-github) then. |
after taking a look again, its quite clear that my original labeling was a mistake |
Thanks @RonnyPfannschmidt for clarifying this. I will submit a PR in the next few days then. |
Guys could you take a look at the latest version of #2576? In that form we are raising a |
…r-lt-gt-in-approx #2003 Change behavior of `approx.py` to only support `__eq__` comparison
Fixed by #2576 |
The new
approx
function is great, but does not seem to work with inequalities, instead failing silently.I.e.
gives
It would be great if inequality testing was also supported with
approx
, and if not then this should give a better error message.The text was updated successfully, but these errors were encountered: