From a80201e975669f4b555d1ffe56da705f7e042e5c Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Mon, 18 Sep 2023 22:28:40 +0200 Subject: [PATCH] Fix `items` and `values` methods returned values type (cast to `benedict`). #333 --- benedict/dicts/__init__.py | 8 ++++++ tests/github/test_issue_0333.py | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/github/test_issue_0333.py diff --git a/benedict/dicts/__init__.py b/benedict/dicts/__init__.py index 1853512..c582149 100644 --- a/benedict/dicts/__init__.py +++ b/benedict/dicts/__init__.py @@ -177,6 +177,10 @@ class benedict(KeyattrDict, KeypathDict, IODict, ParseDict): """ return _invert(self, flat) + def items(self): + for key, value in super().items(): + yield (key, self._cast(value)) + def items_sorted_by_keys(self, reverse=False): """ Return items (key/value list) sorted by keys. @@ -300,6 +304,10 @@ class benedict(KeyattrDict, KeypathDict, IODict, ParseDict): """ _unique(self) + def values(self): + for value in super().values(): + yield self._cast(value) + # fix benedict json dumps support - #57 #59 #61 JSONSerializer.disable_c_make_encoder() diff --git a/tests/github/test_issue_0333.py b/tests/github/test_issue_0333.py new file mode 100644 index 0000000..7ce5478 --- /dev/null +++ b/tests/github/test_issue_0333.py @@ -0,0 +1,49 @@ +import unittest + +from benedict import benedict + + +class github_issue_0333_test_case(unittest.TestCase): + """ + This class describes a github issue 0333 test case. + https://github.com/fabiocaccamo/python-benedict/issues/294 + + To run this specific test: + - Run python -m unittest tests.github.test_issue_0333 + """ + + def test_items_value_type(self): + likes = { + "fruit": { + "apple": "green", + "banana": "yellow", + "raspberry": "red", + } + } + likes = benedict( + likes, + keyattr_enabled=True, + keyattr_dynamic=False, + ) + # print(type(likes.items())) + # print(likes.items()) + for _, v in likes.items(): + self.assertEqual(type(v), benedict) + + def test_values_value_type(self): + likes = { + "fruit": { + "apple": "green", + "banana": "yellow", + "raspberry": "red", + } + } + likes = benedict( + likes, + keyattr_enabled=True, + keyattr_dynamic=False, + ) + # print(type(likes.values())) + # print(likes.values()) + for v in likes.values(): + self.assertEqual(type(v), benedict)