Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests.
  • Loading branch information
brandtbucher committed Mar 2, 2020
commit c19d9e7fe0a812dda1208350ee361d2a1f2869ab
25 changes: 25 additions & 0 deletions Lib/test/test_defaultdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,30 @@ def test_pickling(self):
o = pickle.loads(s)
self.assertEqual(d, o)

def test_union(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no tests for ‘|=‘?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultdict inherits a perfectly fine |= from dict.

I can add tests for it here if you'd like, but the tests for dict already have the same coverage.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh. Maybe add a comment to this effect?

i = defaultdict(int, {1: 1, 2: 2})
s = defaultdict(str, {0: "zero", 1: "one"})

self.assertIs((i | s).default_factory, int)
self.assertDictEqual(i | s, {1: "one", 2: 2, 0: "zero"})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is defaultdict not order-preserving, like dict? If it is, could the test check it?

@brandtbucher brandtbucher Mar 2, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You found a flaw in my merge logic! I pushed a fix and added tests for key order.

self.assertIs((s | i).default_factory, str)
self.assertDictEqual(s | i, {0: "zero", 1: 1, 2: 2})

self.assertIs((i | dict(s)).default_factory, int)
self.assertDictEqual(i | dict(s), {1: "one", 2: 2, 0: "zero"})
self.assertIs((dict(s) | i).default_factory, int)
self.assertDictEqual(dict(s) | i, {0: "zero", 1: 1, 2: 2})

i |= list(s.items())
self.assertIs(i.default_factory, int)
self.assertDictEqual(i, {1: "one", 2: 2, 0: "zero"})

with self.assertRaises(TypeError):
i |= None
with self.assertRaises(TypeError):
i | list(s.items())
with self.assertRaises(TypeError):
list(s.items()) | i

if __name__ == "__main__":
unittest.main()