Skip to content
Draft
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
21 changes: 9 additions & 12 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,21 +2012,18 @@ def test_construct_singletons(self):
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):
Expand Down
23 changes: 17 additions & 6 deletions crates/vm/src/builtins/singletons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,11 @@ impl Constructor for PyNotImplemented {
}
}

#[pyclass(with(Constructor))]
#[pyclass(with(Constructor, AsNumber))]
impl PyNotImplemented {
// 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.
#[pymethod]
const fn __bool__(&self) -> bool {
true
fn __bool__(&self, vm: &VirtualMachine) -> PyResult<bool> {
Err(vm.new_type_error("NotImplemented should not be used in a boolean context".to_owned()))
}

#[pymethod]
Expand All @@ -119,6 +116,20 @@ impl PyNotImplemented {
}
}

impl AsNumber for PyNotImplemented {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
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
}
}

impl Representable for PyNotImplemented {
#[inline]
fn repr(_zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
Expand Down
4 changes: 3 additions & 1 deletion extra_tests/snippets/builtin_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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:
Expand Down
Loading