def total_ordering(cls):
"""Class decorator that fills in missing ordering methods"""
convert = {
- '__lt__': [('__gt__', lambda self, other: other < self),
- ('__le__', lambda self, other: not other < self),
+ '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
+ ('__le__', lambda self, other: self < other or self == other),
('__ge__', lambda self, other: not self < other)],
- '__le__': [('__ge__', lambda self, other: other <= self),
- ('__lt__', lambda self, other: not other <= self),
+ '__le__': [('__ge__', lambda self, other: not self <= other or self == other),
+ ('__lt__', lambda self, other: self <= other and not self == other),
('__gt__', lambda self, other: not self <= other)],
- '__gt__': [('__lt__', lambda self, other: other > self),
- ('__ge__', lambda self, other: not other > self),
+ '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
+ ('__ge__', lambda self, other: self > other or self == other),
('__le__', lambda self, other: not self > other)],
- '__ge__': [('__le__', lambda self, other: other >= self),
- ('__gt__', lambda self, other: not other >= self),
+ '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
+ ('__gt__', lambda self, other: self >= other and not self == other),
('__lt__', lambda self, other: not self >= other)]
}
roots = set(dir(cls)) & set(convert)
self.value = value
def __lt__(self, other):
return self.value < other.value
+ def __eq__(self, other):
+ return self.value == other.value
self.assertTrue(A(1) < A(2))
self.assertTrue(A(2) > A(1))
self.assertTrue(A(1) <= A(2))
self.value = value
def __le__(self, other):
return self.value <= other.value
+ def __eq__(self, other):
+ return self.value == other.value
self.assertTrue(A(1) < A(2))
self.assertTrue(A(2) > A(1))
self.assertTrue(A(1) <= A(2))
self.value = value
def __gt__(self, other):
return self.value > other.value
+ def __eq__(self, other):
+ return self.value == other.value
self.assertTrue(A(1) < A(2))
self.assertTrue(A(2) > A(1))
self.assertTrue(A(1) <= A(2))
self.value = value
def __ge__(self, other):
return self.value >= other.value
+ def __eq__(self, other):
+ return self.value == other.value
self.assertTrue(A(1) < A(2))
self.assertTrue(A(2) > A(1))
self.assertTrue(A(1) <= A(2))
class A:
pass
+ def test_bug_10042(self):
+ @functools.total_ordering
+ class TestTO:
+ def __init__(self, value):
+ self.value = value
+ def __eq__(self, other):
+ if isinstance(other, TestTO):
+ return self.value == other.value
+ return False
+ def __lt__(self, other):
+ if isinstance(other, TestTO):
+ return self.value < other.value
+ raise TypeError
+ with self.assertRaises(TypeError):
+ TestTO(8) <= ()
+
def test_main(verbose=None):
test_classes = (
TestPartial,