]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.7] bpo-37579: Improve equality behavior for pure Python datetime and time (GH...
authorXtreak <tir.karthi@gmail.com>
Sun, 14 Jul 2019 10:13:59 +0000 (15:43 +0530)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 14 Jul 2019 10:13:59 +0000 (03:13 -0700)
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](https://bugs.python.org/issue37579)
(cherry picked from commit e6b46aafad3427463d6264a68824df4797e682f1)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
https://bugs.python.org/issue37579

Lib/datetime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2019-07-13-10-59-43.bpo-37579.B1Tq9i.rst [new file with mode: 0644]

index a964b202e3c7a9856c46014fd98fb5f11d9b93d6..03beb055a00be51249f73318a51b209a76ff1256 100644 (file)
@@ -718,7 +718,7 @@ class timedelta:
         if isinstance(other, timedelta):
             return self._cmp(other) == 0
         else:
-            return False
+            return NotImplemented
 
     def __le__(self, other):
         if isinstance(other, timedelta):
@@ -1261,7 +1261,7 @@ class time:
         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):
index 2f8975d8c07fc4cc667c2899d806ef2322914f08..5814417386586441d219ebdbf19e2f70b0a93993 100644 (file)
@@ -53,6 +53,19 @@ OTHERSTUFF = (10, 34.5, "abc", {}, [], ())
 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 @@ class HarmlessMixedComparison:
         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 (file)
index 0000000..ad52cf2
--- /dev/null
@@ -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.