diff --git a/Lib/collections/_defaultdict.py b/Lib/collections/_defaultdict.py index 007ca9104e..19b82bfb19 100644 --- a/Lib/collections/_defaultdict.py +++ b/Lib/collections/_defaultdict.py @@ -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' diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py index 65376a37ef..68fc449780 100644 --- a/Lib/test/test_defaultdict.py +++ b/Lib/test/test_defaultdict.py @@ -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"})