Skip to content

Commit 3c079a0

Browse files
gh-118767: Make bool(NotImplemented) raise TypeError (#118775)
1 parent aac6b01 commit 3c079a0

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

Doc/library/constants.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ A small number of constants live in the built-in namespace. They are:
5757
it currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
5858
It will raise a :exc:`TypeError` in a future version of Python.
5959

60+
.. versionchanged:: 3.14
61+
Evaluating :data:`!NotImplemented` in a boolean context now raises a :exc:`TypeError`.
62+
6063

6164
.. index:: single: ...; ellipsis literal
6265
.. data:: Ellipsis

Doc/reference/datamodel.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ for more details.
174174
it currently evaluates as true, it will emit a :exc:`DeprecationWarning`.
175175
It will raise a :exc:`TypeError` in a future version of Python.
176176

177+
.. versionchanged:: 3.14
178+
Evaluating :data:`NotImplemented` in a boolean context now raises a :exc:`TypeError`.
179+
177180

178181
Ellipsis
179182
--------

Doc/whatsnew/3.14.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ Deprecated
101101
Removed
102102
=======
103103

104+
* Using :data:`NotImplemented` in a boolean context will now raise a :exc:`TypeError`.
105+
It had previously raised a :exc:`DeprecationWarning` since Python 3.9. (Contributed
106+
by Jelle Zijlstra in :gh:`118767`.)
104107

105108

106109
Porting to Python 3.14

Lib/test/test_builtin.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,14 +2130,11 @@ def test_warning_notimplemented(self):
21302130
# be evaluated in a boolean context (virtually all such use cases
21312131
# are a result of accidental misuse implementing rich comparison
21322132
# operations in terms of one another).
2133-
# For the time being, it will continue to evaluate as a true value, but
2134-
# issue a deprecation warning (with the eventual intent to make it
2135-
# a TypeError).
2136-
self.assertWarns(DeprecationWarning, bool, NotImplemented)
2137-
with self.assertWarns(DeprecationWarning):
2133+
self.assertRaises(TypeError, bool, NotImplemented)
2134+
with self.assertRaises(TypeError):
21382135
self.assertTrue(NotImplemented)
2139-
with self.assertWarns(DeprecationWarning):
2140-
self.assertFalse(not NotImplemented)
2136+
with self.assertRaises(TypeError):
2137+
not NotImplemented
21412138

21422139

21432140
class TestBreakpoint(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Using :data:`NotImplemented` in a boolean context now raises
2+
:exc:`TypeError`. Contributed by Jelle Zijlstra in :gh:`118767`.

Objects/object.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,13 +2090,9 @@ notimplemented_dealloc(PyObject *notimplemented)
20902090
static int
20912091
notimplemented_bool(PyObject *v)
20922092
{
2093-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2094-
"NotImplemented should not be used in a boolean context",
2095-
1) < 0)
2096-
{
2097-
return -1;
2098-
}
2099-
return 1;
2093+
PyErr_SetString(PyExc_TypeError,
2094+
"NotImplemented should not be used in a boolean context");
2095+
return -1;
21002096
}
21012097

21022098
static PyNumberMethods notimplemented_as_number = {

0 commit comments

Comments
 (0)