Skip to content

Commit d3ab1b9

Browse files
maiksensinicoddemus
authored andcommitted
Add user documentation
The new doc section explains why we raise a `NotImplementedError`.
1 parent 309152d commit d3ab1b9

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

_pytest/python_api.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import math
22
import sys
3-
43
import py
54

65
from _pytest.compat import isclass, izip
@@ -32,6 +31,12 @@ def __eq__(self, actual):
3231

3332
__hash__ = None
3433

34+
def __gt__(self, actual):
35+
raise NotImplementedError
36+
37+
def __lt__(self, actual):
38+
raise NotImplementedError
39+
3540
def __ne__(self, actual):
3641
return not (actual == self)
3742

@@ -60,6 +65,12 @@ def __repr__(self):
6065
return "approx({0!r})".format(list(
6166
self._approx_scalar(x) for x in self.expected))
6267

68+
def __gt__(self, actual):
69+
raise NotImplementedError
70+
71+
def __lt__(self, actual):
72+
raise NotImplementedError
73+
6374
def __eq__(self, actual):
6475
import numpy as np
6576

@@ -358,6 +369,22 @@ def approx(expected, rel=None, abs=None, nan_ok=False):
358369
is asymmetric and you can think of ``b`` as the reference value. In the
359370
special case that you explicitly specify an absolute tolerance but not a
360371
relative tolerance, only the absolute tolerance is considered.
372+
373+
.. warning::
374+
375+
In order to avoid inconsistent behavior, a ``NotImplementedError`` is
376+
raised for ``__lt__`` and ``__gt__`` comparisons. The example below
377+
illustrates the problem::
378+
379+
assert approx(0.1) > 0.1 + 1e-10 # calls approx(0.1).__gt__(0.1 + 1e-10)
380+
assert 0.1 + 1e-10 > approx(0.1) # calls approx(0.1).__lt__(0.1 + 1e-10)
381+
382+
In the second example one expects ``approx(0.1).__le__(0.1 + 1e-10)``
383+
to be called. But instead, ``approx(0.1).__lt__(0.1 + 1e-10)`` is used to
384+
comparison. This is because the call hierarchy of rich comparisons
385+
follows a fixed behavior. `More information...`__
386+
387+
__ https://docs.python.org/3/reference/datamodel.html#object.__ge__
361388
"""
362389

363390
from collections import Mapping, Sequence

0 commit comments

Comments
 (0)