8000 [3.7] bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726) by tirkarthi · Pull Request #14745 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.7] bpo-37579: Improve equality behavior for pure Python datetime and time (GH-14726) #14745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,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):
Expand Down Expand Up @@ -1261,7 +1261,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):
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
0