FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement bool(NotImplemented) - 3.14 by youknowone · Pull Request #6543 · RustPython/RustPython · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rs  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
21 changes: 9 additions & 12 deletions Lib/test/test_builtin.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL