diff -r f6792f734fcc Doc/library/enum.rst --- a/Doc/library/enum.rst Sun Sep 29 00:28:55 2013 +1000 +++ b/Doc/library/enum.rst Sun Sep 29 00:54:43 2013 +1000 @@ -536,19 +536,9 @@ the normal :class:`Enum` invariants (such as not being comparable to other enumerations):: - >>> class OrderedEnum(Enum): - ... def __ge__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value >= other.value - ... return NotImplemented - ... def __gt__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value > other.value - ... return NotImplemented - ... def __le__(self, other): - ... if self.__class__ is other.__class__: - ... return self.value <= other.value - ... return NotImplemented + >>> import functools + >>> @functools.total_ordering + ... class OrderedEnum(Enum): ... def __lt__(self, other): ... if self.__class__ is other.__class__: ... return self.value < other.value diff -r f6792f734fcc Lib/functools.py --- a/Lib/functools.py Sun Sep 29 00:28:55 2013 +1000 +++ b/Lib/functools.py Sun Sep 29 00:54:43 2013 +1000 @@ -89,21 +89,65 @@ ### total_ordering class decorator ################################################################################ +def _not_op(op, other): + # "not a < b" handles "a >= b" + # "not a <= b" handles "a > b" + # "not a >= b" handles "a < b" + # "not a > b" handles "a <= b" + op_result = op(other) + if op_result is NotImplemented: + return NotImplemented + return not op_result + +def _op_or_eq(op, self, other): + # "a < b or a == b" handles "a <= b" + # "a > b or a == b" handles "a >= b" + op_result = op(other) + if op_result is NotImplemented: + return NotImplemented + return op_result or self == other + +def _not_op_and_not_eq(op, self, other): + # "not (a < b or a == b)" handles "a > b" + # "not a < b and a != b" is equivalent + # "not (a > b or a == b)" handles "a < b" + # "not a > b and a != b" is equivalent + op_result = op(other) + if op_result is NotImplemented: + return NotImplemented + return not op_result and self != other + +def _not_op_or_eq(op, self, other): + # "not a <= b or a == b" handles "a >= b" + # "not a >= b or a == b" handles "a <= b" + op_result = op(other) + if op_result is NotImplemented: + return NotImplemented + return not op_result or self == other + +def _op_and_not_eq(op, self, other): + # "a <= b and not a == b" handles "a < b" + # "a >= b and not a == b" handles "a > b" + op_result = op(other) + if op_result is NotImplemented: + return NotImplemented + return op_result and self != other + def total_ordering(cls): """Class decorator that fills in missing ordering methods""" convert = { - '__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: 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: 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: (not self >= other) or self == other), - ('__gt__', lambda self, other: self >= other and not self == other), - ('__lt__', lambda self, other: not self >= other)] + '__lt__': [('__gt__', lambda self, other: _not_op_and_not_eq(self.__lt__, self, other)), + ('__le__', lambda self, other: _op_or_eq(self.__lt__, self, other)), + ('__ge__', lambda self, other: _not_op(self.__lt__, other))], + '__le__': [('__ge__', lambda self, other: _not_op_or_eq(self.__le__, self, other)), + ('__lt__', lambda self, other: _op_and_not_eq(self.__le__, self, other)), + ('__gt__', lambda self, other: _not_op(self.__le__, other))], + '__gt__': [('__lt__', lambda self, other: _not_op_and_not_eq(self.__gt__, self, other)), + ('__ge__', lambda self, other: _op_or_eq(self.__gt__, self, other)), + ('__le__', lambda self, other: _not_op(self.__gt__, other))], + '__ge__': [('__le__', lambda self, other: _not_op_or_eq(self.__ge__, self, other)), + ('__gt__', lambda self, other: _op_and_not_eq(self.__ge__, self, other)), + ('__lt__', lambda self, other: _not_op(self.__ge__, other))] } # Find user-defined comparisons (not those inherited from object). roots = [op for op in convert if getattr(cls, op, None) is not getattr(object, op, None)] diff -r f6792f734fcc Lib/test/test_enum.py --- a/Lib/test/test_enum.py Sun Sep 29 00:28:55 2013 +1000 +++ b/Lib/test/test_enum.py Sun Sep 29 00:54:43 2013 +1000 @@ -2,6 +2,7 @@ import unittest from collections import OrderedDict from pickle import dumps, loads, PicklingError +from functools import total_ordering from enum import Enum, IntEnum, unique # for pickle tests @@ -999,23 +1000,13 @@ self.assertEqual(list(map(int, Color)), [1, 2, 3]) def test_ordered_mixin(self): + @total_ordering class OrderedEnum(Enum): - def __ge__(self, other): - if self.__class__ is other.__class__: - return self._value_ >= other._value_ - return NotImplemented - def __gt__(self, other): - if self.__class__ is other.__class__: - return self._value_ > other._value_ - return NotImplemented - def __le__(self, other): - if self.__class__ is other.__class__: - return self._value_ <= other._value_ - return NotImplemented def __lt__(self, other): if self.__class__ is other.__class__: return self._value_ < other._value_ return NotImplemented + # Simple checks class Grade(OrderedEnum): A = 5 B = 4 @@ -1027,6 +1018,22 @@ self.assertLess(Grade.D, Grade.A) self.assertGreaterEqual(Grade.B, Grade.B) + # Checks between ordered enums + class BinaryGrade(OrderedEnum): + Pass = 2 + Fail = 1 + + self.assertNotEqual(Grade.D, BinaryGrade.Pass) + self.assertNotEqual(Grade.F, BinaryGrade.Fail) + with self.assertRaises(TypeError): + Grade.A > BinaryGrade.Pass + with self.assertRaises(TypeError): + Grade.A >= BinaryGrade.Pass + with self.assertRaises(TypeError): + Grade.A <= BinaryGrade.Pass + with self.assertRaises(TypeError): + Grade.A < BinaryGrade.Pass + def test_extending2(self): class Shade(Enum): def shade(self): diff -r f6792f734fcc Lib/test/test_functools.py --- a/Lib/test/test_functools.py Sun Sep 29 00:28:55 2013 +1000 +++ b/Lib/test/test_functools.py Sun Sep 29 00:54:43 2013 +1000 @@ -584,6 +584,7 @@ self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) + self.assertFalse(A(1) > A(2)) def test_total_ordering_le(self): @functools.total_ordering @@ -600,6 +601,7 @@ self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) + self.assertFalse(A(1) >= A(2)) def test_total_ordering_gt(self): @functools.total_ordering @@ -616,6 +618,7 @@ self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) + self.assertFalse(A(2) < A(1)) def test_total_ordering_ge(self): @functools.total_ordering @@ -632,6 +635,7 @@ self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) + self.assertFalse(A(2) <= A(1)) def test_total_ordering_no_overwrite(self): # new methods should not overwrite existing @@ -651,22 +655,112 @@ class A: pass - def test_bug_10042(self): + def test_type_error_when_not_implemented(self): + # bug 10042; ensure stack overflow does not occur + # when decorated types return NotImplemented @functools.total_ordering - class TestTO: + class ImplementsLessThan: def __init__(self, value): self.value = value def __eq__(self, other): - if isinstance(other, TestTO): + if isinstance(other, ImplementsLessThan): return self.value == other.value return False def __lt__(self, other): - if isinstance(other, TestTO): + if isinstance(other, ImplementsLessThan): return self.value < other.value - raise TypeError - with self.assertRaises(TypeError): - TestTO(8) <= () + return NotImplemented + @functools.total_ordering + class ImplementsGreaterThan: + def __init__(self, value): + self.value = value + def __eq__(self, other): + if isinstance(other, ImplementsGreaterThan): + return self.value == other.value + return False + def __gt__(self, other): + if isinstance(other, ImplementsGreaterThan): + return self.value > other.value + return NotImplemented + + @functools.total_ordering + class ImplementsLessThanEqualTo: + def __init__(self, value): + self.value = value + def __eq__(self, other): + if isinstance(other, ImplementsLessThanEqualTo): + return self.value == other.value + return False + def __le__(self, other): + if isinstance(other, ImplementsLessThanEqualTo): + return self.value <= other.value + return NotImplemented + + @functools.total_ordering + class ImplementsGreaterThanEqualTo: + def __init__(self, value): + self.value = value + def __eq__(self, other): + if isinstance(other, ImplementsGreaterThanEqualTo): + return self.value == other.value + return False + def __ge__(self, other): + if isinstance(other, ImplementsGreaterThanEqualTo): + return self.value >= other.value + return NotImplemented + + @functools.total_ordering + class ComparatorNotImplemented: + def __init__(self, value): + self.value = value + def __eq__(self, other): + if isinstance(other, ComparatorNotImplemented): + return self.value == other.value + return False + def __lt__(self, other): + return NotImplemented + + with self.subTest("LT < 1"), self.assertRaises(TypeError): + ImplementsLessThan(-1) < 1 + + with self.subTest("LT < LE"), self.assertRaises(TypeError): + ImplementsLessThan(0) < ImplementsLessThanEqualTo(0) + + with self.subTest("LT < GT"), self.assertRaises(TypeError): + ImplementsLessThan(1) < ImplementsGreaterThan(1) + + with self.subTest("LE <= LT"), self.assertRaises(TypeError): + ImplementsLessThanEqualTo(2) <= ImplementsLessThan(2) + + with self.subTest("LE <= GE"), self.assertRaises(TypeError): + ImplementsLessThanEqualTo(3) <= ImplementsGreaterThanEqualTo(3) + + with self.subTest("GT > GE"), self.assertRaises(TypeError): + ImplementsGreaterThan(4) > ImplementsGreaterThanEqualTo(4) + + with self.subTest("GT > LT"), self.assertRaises(TypeError): + ImplementsGreaterThan(5) > ImplementsLessThan(5) + + with self.subTest("GE >= GT"), self.assertRaises(TypeError): + ImplementsGreaterThanEqualTo(6) >= ImplementsGreaterThan(6) + + with self.subTest("GE >= LE"), self.assertRaises(TypeError): + ImplementsGreaterThanEqualTo(7) >= ImplementsLessThanEqualTo(7) + + with self.subTest("GE when equal"): + a = ComparatorNotImplemented(8) + b = ComparatorNotImplemented(8) + self.assertEqual(a, b) + with self.assertRaises(TypeError): + a >= b + + with self.subTest("LE when equal"): + a = ComparatorNotImplemented(9) + b = ComparatorNotImplemented(9) + self.assertEqual(a, b) + with self.assertRaises(TypeError): + a <= b class TestLRU(unittest.TestCase):