Skip to content

Commit 3c9e92c

Browse files
committed
Add or, ror andior to defaultdict
1 parent 6ebb3f5 commit 3c9e92c

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Lib/collections/_defaultdict.py

+20
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,24 @@ def __reduce__(self):
3939
args = ()
4040
return type(self), args, None, None, iter(self.items())
4141

42+
def __or__(self, other):
43+
if not isinstance(other, dict):
44+
raise TypeError(f'unsupported operand type(s) for |: \'collections.{self.__class__.__qualname__}\' and \'{type(other).__qualname__}\'')
45+
46+
new = defaultdict(self.default_factory, self)
47+
new.update(other)
48+
return new
49+
50+
def __ror__(self, other):
51+
if not isinstance(other, dict):
52+
raise TypeError(f'unsupported operand type(s) for |: \'collections.{self.__class__.__qualname__}\' and \'{type(other).__qualname__}\'')
53+
54+
new = defaultdict(self.default_factory, other)
55+
new.update(self)
56+
return new
57+
58+
def __ior__(self, other):
59+
self.update(other)
60+
return self
61+
4262
defaultdict.__module__ = 'collections'

Lib/test/test_defaultdict.py

-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ def test_pickling(self):
150150
o = pickle.loads(s)
151151
self.assertEqual(d, o)
152152

153-
# TODO: RUSTPYTHON
154-
@unittest.expectedFailure
155153
def test_union(self):
156154
i = defaultdict(int, {1: 1, 2: 2})
157155
s = defaultdict(str, {0: "zero", 1: "one"})

0 commit comments

Comments
 (0)