From 3c9e92cebb911bf6f14aed8e8b9292a8da5b772d Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Fri, 22 Jul 2022 12:16:13 +0900 Subject: [PATCH 1/2] Add `or`, `ror` and`ior` to `defaultdict` --- Lib/collections/_defaultdict.py | 20 ++++++++++++++++++++ Lib/test/test_defaultdict.py | 2 -- 2 files changed, 20 insertions(+), 2 deletions(-) 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 @@ def __reduce__(self): 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 @@ def test_pickling(self): 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"}) From 9c2a5041e90c31986eaa1a41932142c4cae0c6bd Mon Sep 17 00:00:00 2001 From: Gyubong Lee Date: Fri, 22 Jul 2022 13:16:12 +0900 Subject: [PATCH 2/2] Reflect feedbacks --- Lib/collections/_defaultdict.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Lib/collections/_defaultdict.py b/Lib/collections/_defaultdict.py index 19b82bfb19..b9c6c49613 100644 --- a/Lib/collections/_defaultdict.py +++ b/Lib/collections/_defaultdict.py @@ -41,7 +41,7 @@ def __reduce__(self): 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__}\'') + return NotImplemented new = defaultdict(self.default_factory, self) new.update(other) @@ -49,14 +49,10 @@ def __or__(self, other): 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__}\'') + return NotImplemented new = defaultdict(self.default_factory, other) new.update(self) return new - def __ior__(self, other): - self.update(other) - return self - defaultdict.__module__ = 'collections'