mirror of
https://github.com/python/cpython.git
synced 2025-10-03 21:55:41 +00:00
bpo-44605: Teach @total_ordering() to work with metaclasses (GH-27633) (GH-27641)
This commit is contained in:
parent
91f6d38669
commit
fde84170d0
3 changed files with 41 additions and 12 deletions
|
@ -88,84 +88,84 @@ def wraps(wrapped,
|
||||||
|
|
||||||
def _gt_from_lt(self, other, NotImplemented=NotImplemented):
|
def _gt_from_lt(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).'
|
'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).'
|
||||||
op_result = self.__lt__(other)
|
op_result = type(self).__lt__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result and self != other
|
return not op_result and self != other
|
||||||
|
|
||||||
def _le_from_lt(self, other, NotImplemented=NotImplemented):
|
def _le_from_lt(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).'
|
'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).'
|
||||||
op_result = self.__lt__(other)
|
op_result = type(self).__lt__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return op_result or self == other
|
return op_result or self == other
|
||||||
|
|
||||||
def _ge_from_lt(self, other, NotImplemented=NotImplemented):
|
def _ge_from_lt(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a >= b. Computed by @total_ordering from (not a < b).'
|
'Return a >= b. Computed by @total_ordering from (not a < b).'
|
||||||
op_result = self.__lt__(other)
|
op_result = type(self).__lt__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result
|
return not op_result
|
||||||
|
|
||||||
def _ge_from_le(self, other, NotImplemented=NotImplemented):
|
def _ge_from_le(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).'
|
'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).'
|
||||||
op_result = self.__le__(other)
|
op_result = type(self).__le__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result or self == other
|
return not op_result or self == other
|
||||||
|
|
||||||
def _lt_from_le(self, other, NotImplemented=NotImplemented):
|
def _lt_from_le(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).'
|
'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).'
|
||||||
op_result = self.__le__(other)
|
op_result = type(self).__le__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return op_result and self != other
|
return op_result and self != other
|
||||||
|
|
||||||
def _gt_from_le(self, other, NotImplemented=NotImplemented):
|
def _gt_from_le(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a > b. Computed by @total_ordering from (not a <= b).'
|
'Return a > b. Computed by @total_ordering from (not a <= b).'
|
||||||
op_result = self.__le__(other)
|
op_result = type(self).__le__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result
|
return not op_result
|
||||||
|
|
||||||
def _lt_from_gt(self, other, NotImplemented=NotImplemented):
|
def _lt_from_gt(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).'
|
'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).'
|
||||||
op_result = self.__gt__(other)
|
op_result = type(self).__gt__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result and self != other
|
return not op_result and self != other
|
||||||
|
|
||||||
def _ge_from_gt(self, other, NotImplemented=NotImplemented):
|
def _ge_from_gt(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).'
|
'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).'
|
||||||
op_result = self.__gt__(other)
|
op_result = type(self).__gt__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return op_result or self == other
|
return op_result or self == other
|
||||||
|
|
||||||
def _le_from_gt(self, other, NotImplemented=NotImplemented):
|
def _le_from_gt(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a <= b. Computed by @total_ordering from (not a > b).'
|
'Return a <= b. Computed by @total_ordering from (not a > b).'
|
||||||
op_result = self.__gt__(other)
|
op_result = type(self).__gt__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result
|
return not op_result
|
||||||
|
|
||||||
def _le_from_ge(self, other, NotImplemented=NotImplemented):
|
def _le_from_ge(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).'
|
'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).'
|
||||||
op_result = self.__ge__(other)
|
op_result = type(self).__ge__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result or self == other
|
return not op_result or self == other
|
||||||
|
|
||||||
def _gt_from_ge(self, other, NotImplemented=NotImplemented):
|
def _gt_from_ge(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).'
|
'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).'
|
||||||
op_result = self.__ge__(other)
|
op_result = type(self).__ge__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return op_result and self != other
|
return op_result and self != other
|
||||||
|
|
||||||
def _lt_from_ge(self, other, NotImplemented=NotImplemented):
|
def _lt_from_ge(self, other, NotImplemented=NotImplemented):
|
||||||
'Return a < b. Computed by @total_ordering from (not a >= b).'
|
'Return a < b. Computed by @total_ordering from (not a >= b).'
|
||||||
op_result = self.__ge__(other)
|
op_result = type(self).__ge__(self, other)
|
||||||
if op_result is NotImplemented:
|
if op_result is NotImplemented:
|
||||||
return op_result
|
return op_result
|
||||||
return not op_result
|
return not op_result
|
||||||
|
|
|
@ -1153,6 +1153,34 @@ class TestTotalOrdering(unittest.TestCase):
|
||||||
method_copy = pickle.loads(pickle.dumps(method, proto))
|
method_copy = pickle.loads(pickle.dumps(method, proto))
|
||||||
self.assertIs(method_copy, method)
|
self.assertIs(method_copy, method)
|
||||||
|
|
||||||
|
|
||||||
|
def test_total_ordering_for_metaclasses_issue_44605(self):
|
||||||
|
|
||||||
|
@functools.total_ordering
|
||||||
|
class SortableMeta(type):
|
||||||
|
def __new__(cls, name, bases, ns):
|
||||||
|
return super().__new__(cls, name, bases, ns)
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
if not isinstance(other, SortableMeta):
|
||||||
|
pass
|
||||||
|
return self.__name__ < other.__name__
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
if not isinstance(other, SortableMeta):
|
||||||
|
pass
|
||||||
|
return self.__name__ == other.__name__
|
||||||
|
|
||||||
|
class B(metaclass=SortableMeta):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class A(metaclass=SortableMeta):
|
||||||
|
pass
|
||||||
|
|
||||||
|
self.assertTrue(A < B)
|
||||||
|
self.assertFalse(A > B)
|
||||||
|
|
||||||
|
|
||||||
@functools.total_ordering
|
@functools.total_ordering
|
||||||
class Orderable_LT:
|
class Orderable_LT:
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
The @functools.total_ordering() decorator now works with metaclasses.
|
Loading…
Add table
Add a link
Reference in a new issue