Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/library/constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed. And maybe the whole text of the note can be reworded.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll submit another PR addressing your comments.


.. versionchanged:: 3.14
Evaluating :data:`!NotImplemented` in a boolean context now raises a :exc:`TypeError`.


.. index:: single: ...; ellipsis literal
.. data:: Ellipsis
Expand Down
3 changes: 3 additions & 0 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed.


.. versionchanged:: 3.14
Evaluating :data:`NotImplemented` in a boolean context now raises a :exc:`TypeError`.


Ellipsis
--------
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should be renamed.

with self.assertRaises(TypeError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeError is raised when you just call a function with wrong number of arguments, for example:

with self.assertRaises(TypeError):
    self.assertEqual(NotImplemented)

It is better to not use assertTrue() (which does not work here), but directly test the boolean value:

if NotImplemented:
    pass

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll also change it to use assertRaisesRegex to test that we're getting the expected TypeError and not some other one.

self.assertTrue(NotImplemented)
with self.assertWarns(DeprecationWarning):
self.assertFalse(not NotImplemented)
with self.assertRaises(TypeError):
not NotImplemented


class TestBreakpoint(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Using :data:`NotImplemented` in a boolean context now raises
:exc:`TypeError`. Contributed by Jelle Zijlstra in :gh:`118767`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the link to the issue already included in the changelog?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed in the new PR.

10 changes: 3 additions & 7 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading