https://github.com/python/cpython/commit/3c079a020369173745a16ed4ef19a64f69e8592b
commit: 3c079a020369173745a16ed4ef19a64f69e8592b
branch: main
author: Jelle Zijlstra <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2024-05-08T11:12:00-07:00
summary:

gh-118767: Make bool(NotImplemented) raise TypeError (#118775)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-05-08-09-44-15.gh-issue-118767.iFF5F5.rst
M Doc/library/constants.rst
M Doc/reference/datamodel.rst
M Doc/whatsnew/3.14.rst
M Lib/test/test_builtin.py
M Objects/object.c

diff --git a/Doc/library/constants.rst b/Doc/library/constants.rst
index 93a7244f87de6b..890517c3eb68dc 100644
--- a/Doc/library/constants.rst
+++ b/Doc/library/constants.rst
@@ -57,6 +57,9 @@ A small number of constants live in the built-in namespace.  
They are:
       it currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
       It will raise a :exc:`TypeError` in a future version of Python.
 
+   .. versionchanged:: 3.14
+      Evaluating :data:`!NotImplemented` in a boolean context now raises a 
:exc:`TypeError`.
+
 
 .. index:: single: ...; ellipsis literal
 .. data:: Ellipsis
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index f5e87160732056..fc072b4e75314d 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -174,6 +174,9 @@ for more details.
    it currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
    It will raise a :exc:`TypeError` in a future version of Python.
 
+.. versionchanged:: 3.14
+   Evaluating :data:`NotImplemented` in a boolean context now raises a 
:exc:`TypeError`.
+
 
 Ellipsis
 --------
diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index 14628f666dd079..6a9d0b0d912fb2 100644
--- a/Doc/whatsnew/3.14.rst
+++ b/Doc/whatsnew/3.14.rst
@@ -101,6 +101,9 @@ Deprecated
 Removed
 =======
 
+* Using :data:`NotImplemented` in a boolean context will now raise a 
:exc:`TypeError`.
+  It had previously raised a :exc:`DeprecationWarning` since Python 3.9. 
(Contributed
+  by Jelle Zijlstra in :gh:`118767`.)
 
 
 Porting to Python 3.14
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 230789f29ff788..120814379dd53a 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -2130,14 +2130,11 @@ def test_warning_notimplemented(self):
         # 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 a true value, but
-        # issue a deprecation warning (with the eventual intent to make it
-        # a TypeError).
-        self.assertWarns(DeprecationWarning, bool, NotImplemented)
-        with self.assertWarns(DeprecationWarning):
+        self.assertRaises(TypeError, bool, NotImplemented)
+        with self.assertRaises(TypeError):
             self.assertTrue(NotImplemented)
-        with self.assertWarns(DeprecationWarning):
-            self.assertFalse(not NotImplemented)
+        with self.assertRaises(TypeError):
+            not NotImplemented
 
 
 class TestBreakpoint(unittest.TestCase):
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-05-08-09-44-15.gh-issue-118767.iFF5F5.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-05-08-09-44-15.gh-issue-118767.iFF5F5.rst
new file mode 100644
index 00000000000000..76548effd1449f
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-05-08-09-44-15.gh-issue-118767.iFF5F5.rst 
@@ -0,0 +1,2 @@
+Using :data:`NotImplemented` in a boolean context now raises
+:exc:`TypeError`. Contributed by Jelle Zijlstra in :gh:`118767`.
diff --git a/Objects/object.c b/Objects/object.c
index 8ad0389cbc7626..29183dcc5b4a01 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -2090,13 +2090,9 @@ notimplemented_dealloc(PyObject *notimplemented)
 static int
 notimplemented_bool(PyObject *v)
 {
-    if (PyErr_WarnEx(PyExc_DeprecationWarning,
-                     "NotImplemented should not be used in a boolean context",
-                     1) < 0)
-    {
-        return -1;
-    }
-    return 1;
+    PyErr_SetString(PyExc_TypeError,
+                    "NotImplemented should not be used in a boolean context");
+    return -1;
 }
 
 static PyNumberMethods notimplemented_as_number = {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to