Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c2930e

Browse files
committed
run total_ordering() tests, and fix the function (default comparisons shouldn't be considered)
1 parent 7311729 commit 9c2930e

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

Lib/functools.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ def wraps(wrapped,
6565
return partial(update_wrapper, wrapped=wrapped,
6666
assigned=assigned, updated=updated)
6767

68+
_object_defaults = {object.__lt__, object.__le__, object.__gt__, object.__ge__}
6869
def total_ordering(cls):
6970
"""Class decorator that fills in missing ordering methods"""
7071
convert = {
@@ -81,7 +82,9 @@ def total_ordering(cls):
8182
('__gt__', lambda self, other: not other >= self),
8283
('__lt__', lambda self, other: not self >= other)]
8384
}
84-
roots = set(dir(cls)) & set(convert)
85+
roots = (set(dir(cls)) & set(convert))
86+
# Remove default comparison operations defined on object.
87+
roots -= {meth for meth in roots if getattr(cls, meth) in _object_defaults}
8588
if not roots:
8689
raise ValueError('must define at least one ordering operation: < > <= >=')
8790
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__

Lib/test/test_functools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def test_total_ordering_no_overwrite(self):
481481
# new methods should not overwrite existing
482482
@functools.total_ordering
483483
class A(int):
484-
raise Exception()
484+
pass
485485
self.assert_(A(1) < A(2))
486486
self.assert_(A(2) > A(1))
487487
self.assert_(A(1) <= A(2))
@@ -564,6 +564,7 @@ def test_main(verbose=None):
564564
TestPartialSubclass,
565565
TestPythonPartial,
566566
TestUpdateWrapper,
567+
TestTotalOrdering,
567568
TestWraps,
568569
TestReduce,
569570
TestLRU,

0 commit comments

Comments
 (0)