So the documentation for pytest states the following:
Warning:
Changed in version 3.2.In order to avoid inconsistent behavior, TypeError is raised for
>,>=,<and<=comparisons. The example below illustrates the problem: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 expects
approx(0.1).__le__(0.1 + 1e-10)to be called. But instead,approx(0.1).__lt__(0.1 + 1e-10)is used to comparison. This is because the call hierarchy of rich comparisons follows a fixed behavior.
Now I don't know if I'm being stupid, but why would one expect __le__ in the second example? I definitely don't. I expect __lt__.
I don't know what this is trying to state honestly. And I can't see why the functions can't be something like:
def __gt__(self, actual):
return actual > self.expected and other != self
def __lt__(self, actual):
return actual < self.expected and other != self
with the __ge__ and __le__ variants using or instead of and.