Add or, ror andior to defaultdict

This commit is contained in:
Gyubong Lee
2022-07-22 12:16:13 +09:00
parent 6ebb3f59ad
commit 3c9e92cebb
2 changed files with 20 additions and 2 deletions

View File

@@ -39,4 +39,24 @@ class defaultdict(dict):
args = ()
return type(self), args, None, None, iter(self.items())
def __or__(self, other):
if not isinstance(other, dict):
raise TypeError(f'unsupported operand type(s) for |: \'collections.{self.__class__.__qualname__}\' and \'{type(other).__qualname__}\'')
new = defaultdict(self.default_factory, self)
new.update(other)
return new
def __ror__(self, other):
if not isinstance(other, dict):
raise TypeError(f'unsupported operand type(s) for |: \'collections.{self.__class__.__qualname__}\' and \'{type(other).__qualname__}\'')
new = defaultdict(self.default_factory, other)
new.update(self)
return new
def __ior__(self, other):
self.update(other)
return self
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"})