Skip to content

bpo-46388:Improve functools.total_ordering test coverage #30616

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
Jan 15, 2022
Merged
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
67 changes: 67 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,73 @@ def test_no_operations_defined(self):
class A:
pass

def test_notimplemented(self):
# Verify NotImplemented results are correctly handled
@functools.total_ordering
class ImplementsLessThan:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if isinstance(other, ImplementsLessThan):
return self.value == other.value
return False
def __lt__(self, other):
if isinstance(other, ImplementsLessThan):
return self.value < other.value
return NotImplemented

@functools.total_ordering
class ImplementsLessThanEqualTo:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if isinstance(other, ImplementsLessThanEqualTo):
return self.value == other.value
return False
def __le__(self, other):
if isinstance(other, ImplementsLessThanEqualTo):
return self.value <= other.value
return NotImplemented

@functools.total_ordering
class ImplementsGreaterThan:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if isinstance(other, ImplementsGreaterThan):
return self.value == other.value
return False
def __gt__(self, other):
if isinstance(other, ImplementsGreaterThan):
return self.value > other.value
return NotImplemented

@functools.total_ordering
class ImplementsGreaterThanEqualTo:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if isinstance(other, ImplementsGreaterThanEqualTo):
return self.value == other.value
return False
def __ge__(self, other):
if isinstance(other, ImplementsGreaterThanEqualTo):
return self.value >= other.value
return NotImplemented

self.assertIs(ImplementsLessThan(1).__le__(1), NotImplemented)
self.assertIs(ImplementsLessThan(1).__gt__(1), NotImplemented)
self.assertIs(ImplementsLessThan(1).__ge__(1), NotImplemented)
self.assertIs(ImplementsLessThanEqualTo(1).__lt__(1), NotImplemented)
self.assertIs(ImplementsLessThanEqualTo(1).__gt__(1), NotImplemented)
self.assertIs(ImplementsLessThanEqualTo(1).__ge__(1), NotImplemented)
self.assertIs(ImplementsGreaterThan(1).__lt__(1), NotImplemented)
self.assertIs(ImplementsGreaterThan(1).__gt__(1), NotImplemented)
self.assertIs(ImplementsGreaterThan(1).__ge__(1), NotImplemented)
self.assertIs(ImplementsGreaterThanEqualTo(1).__lt__(1), NotImplemented)
self.assertIs(ImplementsGreaterThanEqualTo(1).__le__(1), NotImplemented)
self.assertIs(ImplementsGreaterThanEqualTo(1).__gt__(1), NotImplemented)

def test_type_error_when_not_implemented(self):
# bug 10042; ensure stack overflow does not occur
# when decorated types return NotImplemented
Expand Down