diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index 46bb81e96d..22142358a0 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -17,8 +17,6 @@ def test_constructors_not_callable(self): self.assertRaises(TypeError, vt, {}) self.assertRaises(TypeError, vt) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_dict_keys(self): d = {1: 10, "a": "ABC"} keys = d.keys() @@ -39,8 +37,6 @@ def test_dict_keys(self): del e["a"] self.assertNotEqual(d.keys(), e.keys()) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_dict_items(self): d = {1: 10, "a": "ABC"} items = d.items() diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index ea31954251..5196d2ecc9 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -746,34 +746,6 @@ macro_rules! dict_view { } } - impl Comparable for $name { - fn cmp( - zelf: &PyObjectView, - other: &PyObject, - op: PyComparisonOp, - vm: &VirtualMachine, - ) -> PyResult { - match_class!(match other { - ref dictview @ Self => { - PyDict::inner_cmp( - &zelf.dict, - &dictview.dict, - op, - !zelf.class().is(&vm.ctx.types.dict_keys_type), - vm, - ) - } - ref _set @ PySet => { - // TODO: Implement comparison for set - Ok(NotImplemented) - } - _ => { - Ok(NotImplemented) - } - }) - } - } - impl PyValue for $name { fn class(vm: &VirtualMachine) -> &PyTypeRef { &vm.ctx.types.$class @@ -985,6 +957,33 @@ trait ViewSetOps: DictView { let inner = zelf.difference(other, vm)?; Ok(PySet { inner }) } + + fn cmp( + zelf: &PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + match_class!(match other { + ref dictview @ Self => { + PyDict::inner_cmp( + zelf.dict(), + dictview.dict(), + op, + !zelf.class().is(&vm.ctx.types.dict_keys_type), + vm, + ) + } + ref _set @ PySet => { + let inner = Self::to_set(zelf.to_owned(), vm)?; + let zelf_set = PySet { inner }.into_object(vm); + PySet::cmp(zelf_set.downcast_ref().unwrap(), other, op, vm) + } + _ => { + Ok(NotImplemented) + } + }) + } } impl ViewSetOps for PyDictKeys {} @@ -997,6 +996,17 @@ impl PyDictKeys { } impl Unconstructible for PyDictKeys {} +impl Comparable for PyDictKeys { + fn cmp( + zelf: &PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + ViewSetOps::cmp(zelf, other, op, vm) + } +} + impl ViewSetOps for PyDictItems {} #[pyimpl(with(DictView, Constructor, Comparable, Iterable, ViewSetOps))] impl PyDictItems { @@ -1022,7 +1032,18 @@ impl PyDictItems { } impl Unconstructible for PyDictItems {} -#[pyimpl(with(DictView, Constructor, Comparable, Iterable))] +impl Comparable for PyDictItems { + fn cmp( + zelf: &PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + ViewSetOps::cmp(zelf, other, op, vm) + } +} + +#[pyimpl(with(DictView, Constructor, Iterable))] impl PyDictValues {} impl Unconstructible for PyDictValues {}