bpo-25732: Make functools.total_ordering implementing __ne__. (#3748)
Patch by Raymond Hettinger.
diff --git a/Lib/functools.py b/Lib/functools.py
index 53680b8..5d755d4 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -55,23 +55,28 @@
convert = {
'__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
('__le__', lambda self, other: self < other or self == other),
+ ('__ne__', lambda self, other: not 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),
+ ('__ne__', lambda self, other: 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),
+ ('__ne__', lambda self, other: not 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),
+ ('__ne__', lambda self, other: not self == other),
('__lt__', lambda self, other: not self >= other)]
}
- roots = set(dir(cls)) & set(convert)
+ defined_methods = set(dir(cls))
+ roots = defined_methods & set(convert)
if not roots:
raise ValueError('must define at least one ordering operation: < > <= >=')
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
for opname, opfunc in convert[root]:
- if opname not in roots:
+ if opname not in defined_methods:
opfunc.__name__ = opname
opfunc.__doc__ = getattr(int, opname).__doc__
setattr(cls, opname, opfunc)