From 8b05daad7ef4aaf53d67db414f7abf2d30e66ab7 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Mon, 18 Sep 2023 22:47:20 +0200 Subject: [PATCH] Fix union with assignment operator inconsistent results (compared to `dict`). #334 --- benedict/dicts/base/base_dict.py | 10 ++++++++++ tests/github/test_issue_0334.py | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 tests/github/test_issue_0334.py diff --git a/benedict/dicts/base/base_dict.py b/benedict/dicts/base/base_dict.py index b1b00dc..031c346 100644 --- a/benedict/dicts/base/base_dict.py +++ b/benedict/dicts/base/base_dict.py @@ -58,6 +58,11 @@ class BaseDict(dict): return self._dict[key] return super().__getitem__(key) + def __ior__(self, other): + if self._pointer: + return self._dict.__ior__(other) + return super().__ior__(other) + def __iter__(self): if self._pointer: return iter(self._dict) @@ -68,6 +73,11 @@ class BaseDict(dict): return len(self._dict) return super().__len__() + def __or__(self, other): + if self._pointer: + return self._dict.__or__(other) + return super().__or__(other) + def __repr__(self): if self._pointer: return repr(self._dict) diff --git a/tests/github/test_issue_0334.py b/tests/github/test_issue_0334.py new file mode 100644 index 0000000..a3595a0 --- /dev/null +++ b/tests/github/test_issue_0334.py @@ -0,0 +1,25 @@ +import unittest + +from benedict import benedict + + +class github_issue_0334_test_case(unittest.TestCase): + """ + This class describes a github issue 0334 test case. + https://github.com/fabiocaccamo/python-benedict/issues/294 + + To run this specific test: + - Run python -m unittest tests.github.test_issue_0334 + """ + + def test_union_with_assignement_operator(self): + a = {"a": "a", "b": "b"} + b = {"b": "b", "c": "c"} + + ab = benedict(a.copy()) + bb = benedict(b.copy()) + self.assertEqual(a | b, ab | bb) + + a |= b.copy() + ab |= bb.copy() + self.assertEqual(a, ab)