Merge pull request #3947 from jopemachine/edit-defaultdict

Add `or`, `ror` and`ior` to `defaultdict`
This commit is contained in:
Jeong YunWon
2022-07-22 18:17:27 +09:00
committed by GitHub
2 changed files with 16 additions and 2 deletions

View File

@@ -39,4 +39,20 @@ class defaultdict(dict):
args = ()
return type(self), args, None, None, iter(self.items())
def __or__(self, other):
if not isinstance(other, dict):
return NotImplemented
new = defaultdict(self.default_factory, self)
new.update(other)
return new
def __ror__(self, other):
if not isinstance(other, dict):
return NotImplemented
new = defaultdict(self.default_factory, other)
new.update(self)
return new
defaultdict.__module__ = 'collections'

View File

@@ -150,8 +150,6 @@ class TestDefaultDict(unittest.TestCase):
o = pickle.loads(s)
self.assertEqual(d, o)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_union(self):
i = defaultdict(int, {1: 1, 2: 2})
s = defaultdict(str, {0: "zero", 1: "one"})