summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@gmail.com>2020-03-06 17:24:08 (GMT)
committerGitHub <noreply@github.com>2020-03-06 17:24:08 (GMT)
commit57c9d1725689dde068a7fccaa7500772ecd16d2e (patch)
treefb7f7ecce58aa31d998f12e8f7a8b296667e535d /Lib
parent9566842e892c1f600e1dcfadaab4643be8f32901 (diff)
downloadcpython-57c9d1725689dde068a7fccaa7500772ecd16d2e.zip
cpython-57c9d1725689dde068a7fccaa7500772ecd16d2e.tar.gz
cpython-57c9d1725689dde068a7fccaa7500772ecd16d2e.tar.bz2
bpo-36144: Implement defaultdict union (GH-18729)
For PEP 585 (this isn't in the PEP but is an obvious follow-up).
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_defaultdict.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/Lib/test/test_defaultdict.py b/Lib/test/test_defaultdict.py
index b9f1fb9..b48c649 100644
--- a/Lib/test/test_defaultdict.py
+++ b/Lib/test/test_defaultdict.py
@@ -183,5 +183,43 @@ class TestDefaultDict(unittest.TestCase):
o = pickle.loads(s)
self.assertEqual(d, o)
+ def test_union(self):
+ i = defaultdict(int, {1: 1, 2: 2})
+ s = defaultdict(str, {0: "zero", 1: "one"})
+
+ i_s = i | s
+ self.assertIs(i_s.default_factory, int)
+ self.assertDictEqual(i_s, {1: "one", 2: 2, 0: "zero"})
+ self.assertEqual(list(i_s), [1, 2, 0])
+
+ s_i = s | i
+ self.assertIs(s_i.default_factory, str)
+ self.assertDictEqual(s_i, {0: "zero", 1: 1, 2: 2})
+ self.assertEqual(list(s_i), [0, 1, 2])
+
+ i_ds = i | dict(s)
+ self.assertIs(i_ds.default_factory, int)
+ self.assertDictEqual(i_ds, {1: "one", 2: 2, 0: "zero"})
+ self.assertEqual(list(i_ds), [1, 2, 0])
+
+ ds_i = dict(s) | i
+ self.assertIs(ds_i.default_factory, int)
+ self.assertDictEqual(ds_i, {0: "zero", 1: 1, 2: 2})
+ self.assertEqual(list(ds_i), [0, 1, 2])
+
+ with self.assertRaises(TypeError):
+ i | list(s.items())
+ with self.assertRaises(TypeError):
+ list(s.items()) | i
+
+ # We inherit a fine |= from dict, so just a few sanity checks here:
+ i |= list(s.items())
+ self.assertIs(i.default_factory, int)
+ self.assertDictEqual(i, {1: "one", 2: 2, 0: "zero"})
+ self.assertEqual(list(i), [1, 2, 0])
+
+ with self.assertRaises(TypeError):
+ i |= None
+
if __name__ == "__main__":
unittest.main()