Skip to content

None.__ne__(None) should be NotImplemented #5124

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
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
2 changes: 1 addition & 1 deletion extra_tests/snippets/builtin_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def none2():
assert None.__eq__(3) is NotImplemented
assert None.__ne__(3) is NotImplemented
assert None.__eq__(None) is True
# assert None.__ne__(None) is False # changed in 3.12
assert None.__ne__(None) is NotImplemented
29 changes: 26 additions & 3 deletions vm/src/builtins/singletons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use super::{PyStrRef, PyType, PyTypeRef};
use crate::{
class::PyClassImpl,
convert::ToPyObject,
function::{PyArithmeticValue, PyComparisonValue},
protocol::PyNumberMethods,
types::{AsNumber, Constructor, Representable},
Context, Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
types::{AsNumber, Comparable, Constructor, PyComparisonOp, Representable},
Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
};

#[pyclass(module = false, name = "NoneType")]
Expand Down Expand Up @@ -42,7 +43,7 @@ impl Constructor for PyNone {
}
}

#[pyclass(with(Constructor, AsNumber, Representable))]
#[pyclass(with(Constructor, Comparable, AsNumber, Representable))]
impl PyNone {
#[pymethod(magic)]
fn bool(&self) -> bool {
Expand Down Expand Up @@ -72,6 +73,28 @@ impl AsNumber for PyNone {
}
}

impl Comparable for PyNone {
fn cmp(
_zelf: &Py<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
let value = match op {
PyComparisonOp::Eq => {
if vm.is_none(other) {
PyArithmeticValue::Implemented(true)
} else {
PyArithmeticValue::NotImplemented
}
}
_ => PyComparisonValue::NotImplemented,
};

Ok(value)
}
}

#[pyclass(module = false, name = "NotImplementedType")]
#[derive(Debug)]
pub struct PyNotImplemented;
Expand Down