[Python-checkins] bpo-44605: Teach @total_ordering() to work with metaclasses (GH-27633) (GH-27640)
rhettinger
webhook-mailer at python.org
Fri Aug 6 16:11:52 EDT 2021
https://github.com/python/cpython/commit/66dd1a0e645f26b074547dccc92448169cb32410
commit: 66dd1a0e645f26b074547dccc92448169cb32410
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: rhettinger <rhettinger at users.noreply.github.com>
date: 2021-08-06T15:11:44-05:00
summary:
bpo-44605: Teach @total_ordering() to work with metaclasses (GH-27633) (GH-27640)
files:
A Misc/NEWS.d/next/Library/2021-08-06-09-43-50.bpo-44605.q4YSBZ.rst
M Lib/functools.py
M Lib/test/test_functools.py
diff --git a/Lib/functools.py b/Lib/functools.py
index 357c1dfd909fa..77ec852805c10 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -88,84 +88,84 @@ def wraps(wrapped,
def _gt_from_lt(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result and self != other
def _le_from_lt(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return op_result or self == other
def _ge_from_lt(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result
def _ge_from_le(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result or self == other
def _lt_from_le(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return op_result and self != other
def _gt_from_le(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result
def _lt_from_gt(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result and self != other
def _ge_from_gt(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return op_result or self == other
def _le_from_gt(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result
def _le_from_ge(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result or self == other
def _gt_from_ge(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return op_result and self != other
def _lt_from_ge(self, other, NotImplemented=NotImplemented):
'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:
return op_result
return not op_result
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 78a8a5fcc0fea..fbf5578872e6b 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1163,6 +1163,34 @@ def test_pickle(self):
method_copy = pickle.loads(pickle.dumps(method, proto))
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
class Orderable_LT:
def __init__(self, value):
diff --git a/Misc/NEWS.d/next/Library/2021-08-06-09-43-50.bpo-44605.q4YSBZ.rst b/Misc/NEWS.d/next/Library/2021-08-06-09-43-50.bpo-44605.q4YSBZ.rst
new file mode 100644
index 0000000000000..93783923e15b3
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-08-06-09-43-50.bpo-44605.q4YSBZ.rst
@@ -0,0 +1 @@
+The @functools.total_ordering() decorator now works with metaclasses.
More information about the Python-checkins
mailing list