]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118767: Make bool(NotImplemented) raise TypeError (#118775)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Wed, 8 May 2024 18:12:00 +0000 (11:12 -0700)
committerGitHub <noreply@github.com>
Wed, 8 May 2024 18:12:00 +0000 (11:12 -0700)
Doc/library/constants.rst
Doc/reference/datamodel.rst
Doc/whatsnew/3.14.rst
Lib/test/test_builtin.py
Misc/NEWS.d/next/Core and Builtins/2024-05-08-09-44-15.gh-issue-118767.iFF5F5.rst [new file with mode: 0644]
Objects/object.c

index 93a7244f87de6b963c92bbc40e1dc2bdb78b90e3..890517c3eb68dc36f85902690c4d304292476342 100644 (file)
@@ -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
index f5e87160732056862e82230ec94063c7359ab7a0..fc072b4e75314d647299c273449ccf42d18e3c18 100644 (file)
@@ -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
 --------
index 14628f666dd07987dcd285597448e0797df1f1c3..6a9d0b0d912fb26b935cfc51a6716569cf9098e6 100644 (file)
@@ -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
index 230789f29ff788d4f63749be40ea7e5d76e1c508..120814379dd53ab65e305e2c44dd3078525dc00b 100644 (file)
@@ -2130,14 +2130,11 @@ class BuiltinTest(unittest.TestCase):
         # 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 (file)
index 0000000..76548ef
--- /dev/null
@@ -0,0 +1,2 @@
+Using :data:`NotImplemented` in a boolean context now raises
+:exc:`TypeError`. Contributed by Jelle Zijlstra in :gh:`118767`.
index 8ad0389cbc7626c026bcb0039e4bcee460528080..29183dcc5b4a010e7616628984feee83103d7fa1 100644 (file)
@@ -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 = {