Skip to content

Commit

Permalink
Implement keys() and items() for AttrDict (#1784) (#1810)
Browse files Browse the repository at this point in the history
(cherry picked from commit a6ecf0b)

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
  • Loading branch information
github-actions[bot] and miguelgrinberg committed Apr 29, 2024
1 parent 6c33976 commit ed58aee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions elasticsearch_dsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ def __iter__(self):
def to_dict(self):
return self._d_

def keys(self):
return self._d_.keys()

def items(self):
return self._d_.items()


class DslMeta(type):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration/_async/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ async def test_update_object_field(async_write_client):
)
await w.save()

assert "updated" == await w.update(owner=[{"name": "Honza"}, {"name": "Nick"}])
assert "updated" == await w.update(owner=[{"name": "Honza"}, User(name="Nick")])
assert w.owner[0].name == "Honza"
assert w.owner[1].name == "Nick"

Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration/_sync/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def test_update_object_field(write_client):
)
w.save()

assert "updated" == w.update(owner=[{"name": "Honza"}, {"name": "Nick"}])
assert "updated" == w.update(owner=[{"name": "Honza"}, User(name="Nick")])
assert w.owner[0].name == "Honza"
assert w.owner[1].name == "Nick"

Expand Down
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class MyAttrDict(utils.AttrDict):
assert isinstance(l[:][0], MyAttrDict)


def test_attrdict_keys_items():
a = utils.AttrDict({"a": {"b": 42, "c": 47}, "d": "e"})
assert list(a.keys()) == ["a", "d"]
assert list(a.items()) == [("a", {"b": 42, "c": 47}), ("d", "e")]


def test_merge():
a = utils.AttrDict({"a": {"b": 42, "c": 47}})
b = {"a": {"b": 123, "d": -12}, "e": [1, 2, 3]}
Expand Down

0 comments on commit ed58aee

Please sign in to comment.