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

This commit is contained in:
Jelle Zijlstra 2024-05-08 11:12:00 -07:00 committed by GitHub
parent aac6b019fe
commit 3c079a0203
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 18 additions and 14 deletions

View 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 currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
It will raise a :exc:`TypeError` in a future version of Python. 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 .. index:: single: ...; ellipsis literal
.. data:: Ellipsis .. data:: Ellipsis

View File

@ -174,6 +174,9 @@ for more details.
it currently evaluates as true, it will emit a :exc:`DeprecationWarning`. it currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
It will raise a :exc:`TypeError` in a future version of Python. 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 Ellipsis
-------- --------

View File

@ -101,6 +101,9 @@ Deprecated
Removed 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 Porting to Python 3.14

View File

@ -2130,14 +2130,11 @@ class BuiltinTest(unittest.TestCase):
# be evaluated in a boolean context (virtually all such use cases # be evaluated in a boolean context (virtually all such use cases
# are a result of accidental misuse implementing rich comparison # are a result of accidental misuse implementing rich comparison
# operations in terms of one another). # operations in terms of one another).
# For the time being, it will continue to evaluate as a true value, but self.assertRaises(TypeError, bool, NotImplemented)
# issue a deprecation warning (with the eventual intent to make it with self.assertRaises(TypeError):
# a TypeError).
self.assertWarns(DeprecationWarning, bool, NotImplemented)
with self.assertWarns(DeprecationWarning):
self.assertTrue(NotImplemented) self.assertTrue(NotImplemented)
with self.assertWarns(DeprecationWarning): with self.assertRaises(TypeError):
self.assertFalse(not NotImplemented) not NotImplemented
class TestBreakpoint(unittest.TestCase): class TestBreakpoint(unittest.TestCase):

View File

@ -0,0 +1,2 @@
Using :data:`NotImplemented` in a boolean context now raises
:exc:`TypeError`. Contributed by Jelle Zijlstra in :gh:`118767`.

View File

@ -2090,13 +2090,9 @@ notimplemented_dealloc(PyObject *notimplemented)
static int static int
notimplemented_bool(PyObject *v) notimplemented_bool(PyObject *v)
{ {
if (PyErr_WarnEx(PyExc_DeprecationWarning, PyErr_SetString(PyExc_TypeError,
"NotImplemented should not be used in a boolean context", "NotImplemented should not be used in a boolean context");
1) < 0)
{
return -1; return -1;
}
return 1;
} }
static PyNumberMethods notimplemented_as_number = { static PyNumberMethods notimplemented_as_number = {