bpo-36144: Dictionary Union (PEP 584) (#12088)
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 178cdb1..1aa7d10 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -994,6 +994,26 @@
# Now, add the methods in dicts but not in MutableMapping
def __repr__(self): return repr(self.data)
+
+ def __or__(self, other):
+ if isinstance(other, UserDict):
+ return self.__class__(self.data | other.data)
+ if isinstance(other, dict):
+ return self.__class__(self.data | other)
+ return NotImplemented
+ def __ror__(self, other):
+ if isinstance(other, UserDict):
+ return self.__class__(other.data | self.data)
+ if isinstance(other, dict):
+ return self.__class__(other | self.data)
+ return NotImplemented
+ def __ior__(self, other):
+ if isinstance(other, UserDict):
+ self.data |= other.data
+ else:
+ self.data |= other
+ return self
+
def __copy__(self):
inst = self.__class__.__new__(self.__class__)
inst.__dict__.update(self.__dict__)
diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
index de483ab..d5a3d9e 100644
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -37,6 +37,38 @@
dictliteral = '{' + ', '.join(formatted_items) + '}'
self.assertEqual(eval(dictliteral), dict(items))
+ def test_merge_operator(self):
+
+ a = {0: 0, 1: 1, 2: 1}
+ b = {1: 1, 2: 2, 3: 3}
+
+ c = a.copy()
+ c |= b
+
+ self.assertEqual(a | b, {0: 0, 1: 1, 2: 2, 3: 3})
+ self.assertEqual(c, {0: 0, 1: 1, 2: 2, 3: 3})
+
+ c = b.copy()
+ c |= a
+
+ self.assertEqual(b | a, {1: 1, 2: 1, 3: 3, 0: 0})
+ self.assertEqual(c, {1: 1, 2: 1, 3: 3, 0: 0})
+
+ c = a.copy()
+ c |= [(1, 1), (2, 2), (3, 3)]
+
+ self.assertEqual(c, {0: 0, 1: 1, 2: 2, 3: 3})
+
+ self.assertIs(a.__or__(None), NotImplemented)
+ self.assertIs(a.__or__(()), NotImplemented)
+ self.assertIs(a.__or__("BAD"), NotImplemented)
+ self.assertIs(a.__or__(""), NotImplemented)
+
+ self.assertRaises(TypeError, a.__ior__, None)
+ self.assertEqual(a.__ior__(()), {0: 0, 1: 1, 2: 1})
+ self.assertRaises(ValueError, a.__ior__, "BAD")
+ self.assertEqual(a.__ior__(""), {0: 0, 1: 1, 2: 1})
+
def test_bool(self):
self.assertIs(not {}, True)
self.assertTrue({1: 2})