if isinstance(other, timedelta):
return self._cmp(other) == 0
else:
- return False
+ return NotImplemented
def __le__(self, other):
if isinstance(other, timedelta):
if isinstance(other, time):
return self._cmp(other, allow_mixed=True) == 0
else:
- return False
+ return NotImplemented
def __le__(self, other):
if isinstance(other, time):
INF = float("inf")
NAN = float("nan")
+
+class ComparesEqualClass(object):
+ """
+ A class that is always equal to whatever you compare it to.
+ """
+
+ def __eq__(self, other):
+ return True
+
+ def __ne__(self, other):
+ return False
+
+
#############################################################################
# module tests
self.assertIn(me, [1, 20, [], me])
self.assertIn([], [me, 1, 20, []])
+ # Comparison to objects of unsupported types should return
+ # NotImplemented which falls back to the right hand side's __eq__
+ # method. In this case, ComparesEqualClass.__eq__ always returns True.
+ # ComparesEqualClass.__ne__ always returns False.
+ self.assertTrue(me == ComparesEqualClass())
+ self.assertFalse(me != ComparesEqualClass())
+
def test_harmful_mixed_comparison(self):
me = self.theclass(1, 1, 1)
--- /dev/null
+Return :exc:`NotImplemented` in Python implementation of ``__eq__`` for
+:class:`~datetime.timedelta` and :class:`~datetime.time` when the other
+object being compared is not of the same type to match C implementation.
+Patch by Karthikeyan Singaravelan.