Fix union with assignment operator inconsistent results (compared to `dict`). #334

This commit is contained in:
Fabio Caccamo 2023-09-18 22:47:20 +02:00
parent a80201e975
commit 8b05daad7e
2 changed files with 35 additions and 0 deletions

View File

@ -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)

View File

@ -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)