8000 bpo-37579: Return NotImplemented in Python implementation of __eq__ to match C implementation of datetime for timedelta and time by tirkarthi · Pull Request #14726 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37579: Return NotImplemented in Python implementation of __eq__ to match C implementation of datetime for timedelta and time #14726

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 3 commits into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Return NotImplemented for timedelta and time in __eq__ for different …
…types in Python implementation to match C implementation
  • Loading branch information
tirkarthi committed Jul 13, 2019
commit 622909ea02601c7d117da6ccaa464876732d5416
4 changes: 2 additions & 2 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from datetime import time
from datetime import timezone
from datetime import date, datetime
from unittest.mock import ANY
import time as _time

import _testcapi
Expand Down Expand Up @@ -399,6 +400,11 @@ def test_harmless_mixed_comparison(self):
self.assertIn(me, [1, 20, [], me])
self.assertIn([], [me, 1, 20, []])

# Comparison over a type other that is not object's type should return
# NotImplemented and fallback to other.__eq__. In this case ANY.__eq__
# always returns True.
self.assertEqual(me, ANY)

def test_harmful_mixed_comparison(self):
me = self.theclass(1, 1, 1)

Expand Down
0