Implement bool(NotImplemented)

This commit is contained in:
Jeong, YunWon committed 2026-01-17 19:21:10 +09:00
1 parent 280caea579
commit 8d901a7300
3 files changed
+17 -17

No files matched your search

+9 -12
View File
@@ -2010,21 +2010,18 @@ class BuiltinTest(unittest.TestCase):
self.assertRaises(TypeError, tp, 1, 2)
self.assertRaises(TypeError, tp, a=1, b=2)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_warning_notimplemented(self):
# Issue #35712: NotImplemented is a sentinel value that should never
def test_bool_notimplemented(self):
# GH-79893: NotImplemented is a sentinel value that should never
# 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.assertTrue(NotImplemented)
with self.assertWarns(DeprecationWarning):
self.assertFalse(not NotImplemented)
msg = "NotImplemented should not be used in a boolean context"
self.assertRaisesRegex(TypeError, msg, bool, NotImplemented)
with self.assertRaisesRegex(TypeError, msg):
if NotImplemented:
pass
with self.assertRaisesRegex(TypeError, msg):
not NotImplemented
class TestBreakpoint(unittest.TestCase):
+5 -4
View File
@@ -108,11 +108,12 @@ impl PyNotImplemented {
impl AsNumber for PyNotImplemented {
fn as_number() -> &'static PyNumberMethods {
// TODO: As per https://bugs.python.org/issue35712, using NotImplemented
// in boolean contexts will need to raise a DeprecationWarning in 3.9
// and, eventually, a TypeError.
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|_number, _vm| Ok(true)),
boolean: Some(|_number, vm| {
Err(vm.new_type_error(
"NotImplemented should not be used in a boolean context".to_owned(),
))
}),
..PyNumberMethods::NOT_IMPLEMENTED
};
&AS_NUMBER
+3 -1
View File
@@ -18,7 +18,9 @@ assert bool() is False
assert bool(1) is True
assert bool({}) is False
assert bool(NotImplemented) is True
# NotImplemented cannot be used in a boolean context (Python 3.14+)
with assert_raises(TypeError):
bool(NotImplemented)
assert bool(...) is True
if not 1: