Skip to content

Commit 0fe8a06

Browse files
authored
Dict keys isdisjoint
1 parent c7e1742 commit 0fe8a06

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

Lib/test/test_dictviews.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ def test_dict_repr(self):
9090
self.assertTrue(r == "dict_values(['ABC', 10])" or
9191
r == "dict_values([10, 'ABC'])")
9292

93-
# TODO: RUSTPYTHON
94-
@unittest.expectedFailure
9593
def test_keys_set_operations(self):
9694
d1 = {'a': 1, 'b': 2}
9795
d2 = {'b': 3, 'c': 2}
@@ -148,8 +146,6 @@ def test_keys_set_operations(self):
148146
self.assertTrue(de.keys().isdisjoint(de.keys()))
149147
self.assertTrue(de.keys().isdisjoint([1]))
150148

151-
# TODO: RUSTPYTHON
152-
@unittest.expectedFailure
153149
def test_items_set_operations(self):
154150
d1 = {'a': 1, 'b': 2}
155151
d2 = {'a': 2, 'b': 2}

extra_tests/snippets/builtin_dict.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,18 @@
4747
assert 10 not in d.keys()
4848
assert (1, 10) not in d.keys()
4949
assert "abc" not in d.keys()
50-
assert ((3,4),5) not in d.keys()
50+
assert ((3,4),5) not in d.keys()
51+
52+
d1 = {"a": 1, "b": 2}
53+
d2 = {"c": 3, "d": 4}
54+
assert d1.items().isdisjoint(d2.items())
55+
assert d1.keys().isdisjoint(d2.keys())
56+
d2 = {"b": 3, "d": 4}
57+
assert d1.items().isdisjoint(d2.items())
58+
assert not d1.keys().isdisjoint(d2.keys())
59+
d2 = {"c": 2, "d": 4}
60+
assert d1.items().isdisjoint(d2.items())
61+
assert d1.keys().isdisjoint(d2.keys())
62+
d2 = {"b": 2, "d": 4}
63+
assert not d1.items().isdisjoint(d2.items())
64+
assert not d1.keys().isdisjoint(d2.keys())

vm/src/builtins/dict.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,14 @@ trait ViewSetOps: DictView {
987987
}
988988
})
989989
}
990+
991+
#[pymethod]
992+
fn isdisjoint(zelf: PyRef<Self>, other: ArgIterable, vm: &VirtualMachine) -> PyResult<bool> {
993+
// TODO: to_set is an expensive operation. After merging #3316 rewrite implementation using PySequence_Contains.
994+
let zelf = Self::to_set(zelf, vm)?;
995+
let result = zelf.isdisjoint(other, vm)?;
996+
Ok(result)
997+
}
990998
}
991999

9921000
impl ViewSetOps for PyDictKeys {}

vm/src/builtins/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl PySetInner {
195195
self.compare(&other_set, PyComparisonOp::Le, vm)
196196
}
197197

198-
fn isdisjoint(&self, other: ArgIterable, vm: &VirtualMachine) -> PyResult<bool> {
198+
pub(super) fn isdisjoint(&self, other: ArgIterable, vm: &VirtualMachine) -> PyResult<bool> {
199199
for item in other.iter(vm)? {
200200
if self.contains(&*item?, vm)? {
201201
return Ok(false);

0 commit comments

Comments
 (0)