Skip to content

Commit 651b247

Browse files
Implement keys() and items() for AttrDict (#1784)
(cherry picked from commit a6ecf0b)
1 parent 6c33976 commit 651b247

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

elasticsearch_dsl/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ def __iter__(self):
187187
def to_dict(self):
188188
return self._d_
189189

190+
def keys(self):
191+
return self._d_.keys()
192+
193+
def items(self):
194+
return self._d_.items()
195+
190196

191197
class DslMeta(type):
192198
"""

tests/test_integration/_async/test_document.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ async def test_update_object_field(async_write_client):
214214
)
215215
await w.save()
216216

217-
assert "updated" == await w.update(owner=[{"name": "Honza"}, {"name": "Nick"}])
217+
assert "updated" == await w.update(owner=[{"name": "Honza"}, User(name="Nick")])
218218
assert w.owner[0].name == "Honza"
219219
assert w.owner[1].name == "Nick"
220220

tests/test_integration/_sync/test_document.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def test_update_object_field(write_client):
214214
)
215215
w.save()
216216

217-
assert "updated" == w.update(owner=[{"name": "Honza"}, {"name": "Nick"}])
217+
assert "updated" == w.update(owner=[{"name": "Honza"}, User(name="Nick")])
218218
assert w.owner[0].name == "Honza"
219219
assert w.owner[1].name == "Nick"
220220

tests/test_utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class MyAttrDict(utils.AttrDict):
4444
assert isinstance(l[:][0], MyAttrDict)
4545

4646

47+
def test_attrdict_keys_items():
48+
a = utils.AttrDict({"a": {"b": 42, "c": 47}, "d": "e"})
49+
assert list(a.keys()) == ["a", "d"]
50+
assert list(a.items()) == [("a", {"b": 42, "c": 47}), ("d", "e")]
51+
52+
4753
def test_merge():
4854
a = utils.AttrDict({"a": {"b": 42, "c": 47}})
4955
b = {"a": {"b": 123, "d": -12}, "e": [1, 2, 3]}

0 commit comments

Comments
 (0)