Add __contains__ in dict_items

This commit is contained in:
Jack-R-lantern
2021-10-09 17:32:03 +09:00
parent d59d5e17e6
commit aef89ceaec
2 changed files with 16 additions and 4 deletions

View File

@@ -16,7 +16,7 @@ assert {'a': 123}.get('b') == None
assert {'a': 123}.get('b', 456) == 456
d = {'a': 123, 'b': 456}
assert list(reversed(d)) == ['b', 'a']
assert list(reversed(d.keys())) == ['b', 'a']
assert list(reversed(d.values())) == [456, 123]
assert list(reversed(d.items())) == [('b', 456), ('a', 123)]
@@ -27,7 +27,13 @@ with assert_raises(StopIteration):
assert 'dict' in dict().__doc__
d = {'a': 123, 'b': 456}
assert 1 not in d.items()
assert 'a' not in d.items()
assert 'a', 123 not in d.items()
assert () not in d.items()
assert (1) not in d.items()
assert ('a') not in d.items()
assert ('a', 123) in d.items()
assert ('b', 456) in d.items()
assert ('a', 123, 3) not in d.items()
assert () not in d.items()
assert ('a', 123, 'b', 456) not in d.items()

View File

@@ -3,7 +3,7 @@ use super::{
PyTypeRef,
};
use crate::{
builtins::PyTupleRef,
builtins::PyTuple,
common::ascii,
dictdatatype::{self, DictKey},
function::{ArgIterable, FuncArgs, IntoPyObject, KwArgs, OptionalArg},
@@ -985,7 +985,13 @@ impl PyDictItems {
}
#[pymethod(magic)]
fn contains(zelf: PyRef<Self>, needle: PyTupleRef, vm: &VirtualMachine) -> PyResult<bool> {
fn contains(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
let needle = match_class! {
match needle {
tuple @ PyTuple => tuple,
_ => return Ok(false),
}
};
if needle.len() != 2 {
return Ok(false);
}