From f47b4bb215b914888ae8b443a4123b6060b9bcbc Mon Sep 17 00:00:00 2001 From: kujiy Date: Wed, 27 Apr 2022 11:37:38 +0900 Subject: [PATCH 1/3] Add AttrList.to_list() --- elasticsearch_dsl/utils.py | 2 ++ tests/test_utils.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/elasticsearch_dsl/utils.py b/elasticsearch_dsl/utils.py index 3b231d256..652e4ef12 100644 --- a/elasticsearch_dsl/utils.py +++ b/elasticsearch_dsl/utils.py @@ -101,6 +101,8 @@ def __getstate__(self): def __setstate__(self, state): self._l_, self._obj_wrapper = state + def to_list(self): + return [x for x in self.__iter__()] class AttrDict: """ diff --git a/tests/test_utils.py b/tests/test_utils.py index 73931d433..73d122d61 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -100,3 +100,8 @@ def test_recursive_to_dict(): assert utils.recursive_to_dict({"k": [1, (1.0, {"v": Q("match", key="val")})]}) == { "k": [1, (1.0, {"v": {"match": {"key": "val"}}})] } + +def test_attrlist_to_list(): + l = utils.AttrList([{}, {}]).to_list() + assert isinstance(l, list) + assert l == [{}, {}] From e006f2cace8bfdc4d454962ba176c502884710f0 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Wed, 24 Apr 2024 16:44:15 +0100 Subject: [PATCH 2/3] simplify to_list() implementation --- elasticsearch_dsl/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/elasticsearch_dsl/utils.py b/elasticsearch_dsl/utils.py index 652e4ef12..1a8e89732 100644 --- a/elasticsearch_dsl/utils.py +++ b/elasticsearch_dsl/utils.py @@ -102,7 +102,8 @@ def __setstate__(self, state): self._l_, self._obj_wrapper = state def to_list(self): - return [x for x in self.__iter__()] + return self._l_ + class AttrDict: """ From 7a98c499b29efea124b1b04faed518289c483eb2 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Wed, 24 Apr 2024 16:46:34 +0100 Subject: [PATCH 3/3] code formatting --- tests/test_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 73d122d61..615b4c248 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -101,6 +101,7 @@ def test_recursive_to_dict(): "k": [1, (1.0, {"v": {"match": {"key": "val"}}})] } + def test_attrlist_to_list(): l = utils.AttrList([{}, {}]).to_list() assert isinstance(l, list)