Skip to content

Commit 0a28118

Browse files
authored
bpo-46388: Test NotImplemented code path for functools.total_ordering (GH-30616)
1 parent d02c5e9 commit 0a28118

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Lib/test/test_functools.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,73 @@ def test_no_operations_defined(self):
10491049
class A:
10501050
pass
10511051

1052+
def test_notimplemented(self):
1053+
# Verify NotImplemented results are correctly handled
1054+
@functools.total_ordering
1055+
class ImplementsLessThan:
1056+
def __init__(self, value):
1057+
self.value = value
1058+
def __eq__(self, other):
1059+
if isinstance(other, ImplementsLessThan):
1060+
return self.value == other.value
1061+
return False
1062+
def __lt__(self, other):
1063+
if isinstance(other, ImplementsLessThan):
1064+
return self.value < other.value
1065+
return NotImplemented
1066+
1067+
@functools.total_ordering
1068+
class ImplementsLessThanEqualTo:
1069+
def __init__(self, value):
1070+
self.value = value
1071+
def __eq__(self, other):
1072+
if isinstance(other, ImplementsLessThanEqualTo):
1073+
return self.value == other.value
1074+
return False
1075+
def __le__(self, other):
1076+
if isinstance(other, ImplementsLessThanEqualTo):
1077+
return self.value <= other.value
1078+
return NotImplemented
1079+
1080+
@functools.total_ordering
1081+
class ImplementsGreaterThan:
1082+
def __init__(self, value):
1083+
self.value = value
1084+
def __eq__(self, other):
1085+
if isinstance(other, ImplementsGreaterThan):
1086+
return self.value == other.value
1087+
return False
1088+
def __gt__(self, other):
1089+
if isinstance(other, ImplementsGreaterThan):
1090+
return self.value > other.value
1091+
return NotImplemented
1092+
1093+
@functools.total_ordering
1094+
class ImplementsGreaterThanEqualTo:
1095+
def __init__(self, value):
1096+
self.value = value
1097+
def __eq__(self, other):
1098+
if isinstance(other, ImplementsGreaterThanEqualTo):
1099+
return self.value == other.value
1100+
return False
1101+
def __ge__(self, other):
1102+
if isinstance(other, ImplementsGreaterThanEqualTo):
1103+
return self.value >= other.value
1104+
return NotImplemented
1105+
1106+
self.assertIs(ImplementsLessThan(1).__le__(1), NotImplemented)
1107+
self.assertIs(ImplementsLessThan(1).__gt__(1), NotImplemented)
1108+
self.assertIs(ImplementsLessThan(1).__ge__(1), NotImplemented)
1109+
self.assertIs(ImplementsLessThanEqualTo(1).__lt__(1), NotImplemented)
1110+
self.assertIs(ImplementsLessThanEqualTo(1).__gt__(1), NotImplemented)
1111+
self.assertIs(ImplementsLessThanEqualTo(1).__ge__(1), NotImplemented)
1112+
self.assertIs(ImplementsGreaterThan(1).__lt__(1), NotImplemented)
1113+
self.assertIs(ImplementsGreaterThan(1).__gt__(1), NotImplemented)
1114+
self.assertIs(ImplementsGreaterThan(1).__ge__(1), NotImplemented)
1115+
self.assertIs(ImplementsGreaterThanEqualTo(1).__lt__(1), NotImplemented)
1116+
self.assertIs(ImplementsGreaterThanEqualTo(1).__le__(1), NotImplemented)
1117+
self.assertIs(ImplementsGreaterThanEqualTo(1).__gt__(1), NotImplemented)
1118+
10521119
def test_type_error_when_not_implemented(self):
10531120
# bug 10042; ensure stack overflow does not occur
10541121
# when decorated types return NotImplemented

0 commit comments

Comments
 (0)