Skip to content

Commit 4acc629

Browse files
yonmilkKaratuss
andcommitted
Add mappingproxy richcompare
Co-Authored-By: Hyunmin Shin <shm1193@gmail.com>
1 parent 715dac9 commit 4acc629

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Lib/test/test_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,8 +982,6 @@ def test_or_type_operator_reference_cycle(self):
982982
class MappingProxyTests(unittest.TestCase):
983983
mappingproxy = types.MappingProxyType
984984

985-
# TODO: RUSTPYTHON
986-
@unittest.expectedFailure
987985
def test_constructor(self):
988986
class userdict(dict):
989987
pass

vm/src/builtins/mappingproxy.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRe
22
use crate::{
33
class::PyClassImpl,
44
convert::ToPyObject,
5-
function::{ArgMapping, OptionalArg},
5+
function::{ArgMapping, OptionalArg, PyComparisonValue},
66
protocol::{PyMapping, PyMappingMethods, PyNumberMethods, PySequence, PySequenceMethods},
7-
types::{AsMapping, AsNumber, AsSequence, Constructor, Iterable},
7+
types::{AsMapping, AsNumber, AsSequence, Comparable, Constructor, Iterable, PyComparisonOp},
88
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
99
};
1010

@@ -64,7 +64,7 @@ impl Constructor for PyMappingProxy {
6464
}
6565
}
6666

67-
#[pyimpl(with(AsMapping, Iterable, Constructor, AsSequence))]
67+
#[pyimpl(with(AsMapping, Iterable, Constructor, AsSequence, Comparable))]
6868
impl PyMappingProxy {
6969
fn get_inner(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
7070
let opt = match &self.mapping {
@@ -185,6 +185,20 @@ impl PyMappingProxy {
185185
}
186186
}
187187

188+
impl Comparable for PyMappingProxy {
189+
fn cmp(
190+
zelf: &crate::Py<Self>,
191+
other: &PyObject,
192+
op: PyComparisonOp,
193+
vm: &VirtualMachine,
194+
) -> PyResult<PyComparisonValue> {
195+
let obj = zelf.to_object(vm)?;
196+
Ok(PyComparisonValue::Implemented(
197+
obj.rich_compare_bool(other, op, vm)?,
198+
))
199+
}
200+
}
201+
188202
impl AsMapping for PyMappingProxy {
189203
const AS_MAPPING: PyMappingMethods = PyMappingMethods {
190204
length: Some(|mapping, vm| Self::mapping_downcast(mapping).len(vm)),

0 commit comments

Comments
 (0)