bpo-35712: Make using NotImplemented in a boolean context issue a deprecation warning (GH-13195)
diff --git a/Lib/functools.py b/Lib/functools.py
index 535fa04..e230175 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -96,6 +96,8 @@
def _le_from_lt(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).'
op_result = self.__lt__(other)
+ if op_result is NotImplemented:
+ return op_result
return op_result or self == other
def _ge_from_lt(self, other, NotImplemented=NotImplemented):
@@ -136,6 +138,8 @@
def _ge_from_gt(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).'
op_result = self.__gt__(other)
+ if op_result is NotImplemented:
+ return op_result
return op_result or self == other
def _le_from_gt(self, other, NotImplemented=NotImplemented):
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 9c47405..7024339 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -1398,7 +1398,7 @@
def __eq__(self, other):
address_equal = IPv4Address.__eq__(self, other)
- if not address_equal or address_equal is NotImplemented:
+ if address_equal is NotImplemented or not address_equal:
return address_equal
try:
return self.network == other.network
@@ -2096,7 +2096,7 @@
def __eq__(self, other):
address_equal = IPv6Address.__eq__(self, other)
- if not address_equal or address_equal is NotImplemented:
+ if address_equal is NotImplemented or not address_equal:
return address_equal
try:
return self.network == other.network
@@ -2109,7 +2109,7 @@
def __lt__(self, other):
address_less = IPv6Address.__lt__(self, other)
if address_less is NotImplemented:
- return NotImplemented
+ return address_less
try:
return (self.network < other.network or
self.network == other.network and address_less)
diff --git a/Lib/test/test_buffer.py b/Lib/test/test_buffer.py
index 6178ffd..2ddca06 100644
--- a/Lib/test/test_buffer.py
+++ b/Lib/test/test_buffer.py
@@ -2528,7 +2528,7 @@
values = [INT(9), IDX(9),
2.2+3j, Decimal("-21.1"), 12.2, Fraction(5, 2),
[1,2,3], {4,5,6}, {7:8}, (), (9,),
- True, False, None, NotImplemented,
+ True, False, None, Ellipsis,
b'a', b'abc', bytearray(b'a'), bytearray(b'abc'),
'a', 'abc', r'a', r'abc',
f, lambda x: x]
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 5c553a9..e50c273 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1666,6 +1666,20 @@
self.assertRaises(TypeError, tp, 1, 2)
self.assertRaises(TypeError, tp, a=1, b=2)
+ def test_warning_notimplemented(self):
+ # Issue #35712: NotImplemented is a sentinel value that should never
+ # be evaluated in a boolean context (virtually all such use cases
+ # are a result of accidental misuse implementing rich comparison
+ # operations in terms of one another).
+ # For the time being, it will continue to evaluate as truthy, but
+ # issue a deprecation warning (with the eventual intent to make it
+ # a TypeError).
+ self.assertWarns(DeprecationWarning, bool, NotImplemented)
+ with self.assertWarns(DeprecationWarning):
+ self.assertTrue(NotImplemented)
+ with self.assertWarns(DeprecationWarning):
+ self.assertFalse(not NotImplemented)
+
class TestBreakpoint(unittest.TestCase):
def setUp(self):
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index d2e1218..96cc8de2 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -2526,9 +2526,9 @@
except TypeError:
pass
- # Two essentially featureless objects, just inheriting stuff from
- # object.
- self.assertEqual(dir(NotImplemented), dir(Ellipsis))
+ # Two essentially featureless objects, (Ellipsis just inherits stuff
+ # from object.
+ self.assertEqual(dir(object()), dir(Ellipsis))
# Nasty test case for proxied objects
class Wrapper(object):