[Python-checkins] bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726)
Miss Islington (bot)
webhook-mailer at python.org
Sat Jul 13 09:59:40 EDT 2019
https://github.com/python/cpython/commit/143672cf028740fc549e532c049559c522930c95
commit: 143672cf028740fc549e532c049559c522930c95
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-07-13T06:59:37-07:00
summary:
bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726)
Returns NotImplemented for timedelta and time in __eq__ for different types in Python implementation, which matches the C implementation.
This also adds tests to enforce that these objects will fall back to the right hand side's __eq__ and/or __ne__ implementation.
bpo-37579
(cherry picked from commit e6b46aafad3427463d6264a68824df4797e682f1)
Co-authored-by: Xtreak <tir.karthi at gmail.com>
files:
A Misc/NEWS.d/next/Library/2019-07-13-10-59-43.bpo-37579.B1Tq9i.rst
M Lib/datetime.py
M Lib/test/datetimetester.py
diff --git a/Lib/datetime.py b/Lib/datetime.py
index 0e64815944db..e35ee0554c1f 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -733,7 +733,7 @@ def __eq__(self, other):
if isinstance(other, timedelta):
return self._cmp(other) == 0
else:
- return False
+ return NotImplemented
def __le__(self, other):
if isinstance(other, timedelta):
@@ -1310,7 +1310,7 @@ def __eq__(self, other):
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):
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 53de0658773c..37ddd4b1a8bc 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -53,6 +53,19 @@
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
@@ -399,6 +412,13 @@ def test_harmless_mixed_comparison(self):
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)
diff --git a/Misc/NEWS.d/next/Library/2019-07-13-10-59-43.bpo-37579.B1Tq9i.rst b/Misc/NEWS.d/next/Library/2019-07-13-10-59-43.bpo-37579.B1Tq9i.rst
new file mode 100644
index 000000000000..ad52cf2a06cc
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-07-13-10-59-43.bpo-37579.B1Tq9i.rst
@@ -0,0 +1,4 @@
+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.
More information about the Python-checkins
mailing list