gh-118767: Improve tests and docs for bool(NotImplemented) (#118813)

This commit is contained in:
Jelle Zijlstra 2024-05-09 06:52:08 -07:00 committed by GitHub
parent 82acc5f211
commit 35b5eaa176
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 13 deletions

View file

@ -53,12 +53,12 @@ A small number of constants live in the built-in namespace. They are:
See :exc:`NotImplementedError` for details on when to use it. See :exc:`NotImplementedError` for details on when to use it.
.. versionchanged:: 3.9 .. versionchanged:: 3.9
Evaluating :data:`!NotImplemented` in a boolean context is deprecated. While Evaluating :data:`!NotImplemented` in a boolean context was deprecated.
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 .. versionchanged:: 3.14
Evaluating :data:`!NotImplemented` in a boolean context now raises a :exc:`TypeError`. Evaluating :data:`!NotImplemented` in a boolean context now raises a :exc:`TypeError`.
It previously evaluated to :const:`True` and emitted a :exc:`DeprecationWarning`
since Python 3.9.
.. index:: single: ...; ellipsis literal .. index:: single: ...; ellipsis literal

View file

@ -170,12 +170,12 @@ See
for more details. for more details.
.. versionchanged:: 3.9 .. versionchanged:: 3.9
Evaluating :data:`NotImplemented` in a boolean context is deprecated. While Evaluating :data:`NotImplemented` in a boolean context was deprecated.
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 .. versionchanged:: 3.14
Evaluating :data:`NotImplemented` in a boolean context now raises a :exc:`TypeError`. Evaluating :data:`NotImplemented` in a boolean context now raises a :exc:`TypeError`.
It previously evaluated to :const:`True` and emitted a :exc:`DeprecationWarning`
since Python 3.9.
Ellipsis Ellipsis

View file

@ -2125,15 +2125,17 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, tp, 1, 2) self.assertRaises(TypeError, tp, 1, 2)
self.assertRaises(TypeError, tp, a=1, b=2) self.assertRaises(TypeError, tp, a=1, b=2)
def test_warning_notimplemented(self): def test_bool_notimplemented(self):
# Issue #35712: NotImplemented is a sentinel value that should never # GH-79893: NotImplemented is a sentinel value that should never
# 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).
self.assertRaises(TypeError, bool, NotImplemented) msg = "NotImplemented should not be used in a boolean context"
with self.assertRaises(TypeError): self.assertRaisesRegex(TypeError, msg, bool, NotImplemented)
self.assertTrue(NotImplemented) with self.assertRaisesRegex(TypeError, msg):
with self.assertRaises(TypeError): if NotImplemented:
pass
with self.assertRaisesRegex(TypeError, msg):
not NotImplemented not NotImplemented

View file

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