Skip to content

Commit fd71647

Browse files
authored
Merge pull request RustPython#3271 from Jack-R-lantern/dict_key/__contains__
Add contains in dict_keys
2 parents ec91e6e + ab43f91 commit fd71647

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

extra_tests/snippets/builtin_dict.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,15 @@
3636
assert ('a', 123) in d.items()
3737
assert ('b', 456) in d.items()
3838
assert ('a', 123, 3) not in d.items()
39-
assert ('a', 123, 'b', 456) not in d.items()
39+
assert ('a', 123, 'b', 456) not in d.items()
40+
41+
d = {1: 10, "a": "ABC", (3,4): 5}
42+
assert 1 in d.keys()
43+
assert (1) in d.keys()
44+
assert "a" in d.keys()
45+
assert (3,4) in d.keys()
46+
assert () not in d.keys()
47+
assert 10 not in d.keys()
48+
assert (1, 10) not in d.keys()
49+
assert "abc" not in d.keys()
50+
assert ((3,4),5) not in d.keys()

vm/src/builtins/dict.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,12 @@ trait ViewSetOps: DictView {
956956

957957
impl ViewSetOps for PyDictKeys {}
958958
#[pyimpl(with(DictView, Comparable, Iterable, ViewSetOps))]
959-
impl PyDictKeys {}
959+
impl PyDictKeys {
960+
#[pymethod(magic)]
961+
fn contains(zelf: PyRef<Self>, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
962+
zelf.dict().contains(key, vm)
963+
}
964+
}
960965

961966
impl ViewSetOps for PyDictItems {}
962967
#[pyimpl(with(DictView, Comparable, Iterable, ViewSetOps))]

0 commit comments

Comments
 (0)