Skip to content

Commit 60be5a2

Browse files
gh-157217: Add mappingproxy dict() and update tests
1 parent 784eeb1 commit 60be5a2

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lib/test/test_dict_mappingproxy.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Tests for dict() / update / unpacking of types.MappingProxyType.
2+
3+
gh-157217: merging from a mappingproxy wrapping a dict should take the
4+
locked dict-to-dict path instead of iterating the proxy unlocked.
5+
"""
6+
7+
import collections
8+
import types
9+
import unittest
10+
11+
12+
class MappingProxyDictMergeTests(unittest.TestCase):
13+
def test_update_from_mappingproxy_dict(self):
14+
d = {}
15+
d.update(types.MappingProxyType({1: 1, 2: 2, 3: 3}))
16+
self.assertEqual(d, {1: 1, 2: 2, 3: 3})
17+
18+
def test_dict_constructor_from_mappingproxy(self):
19+
view = types.MappingProxyType({'a': 1, 'b': 2})
20+
self.assertEqual(dict(view), {'a': 1, 'b': 2})
21+
self.assertEqual({**view}, {'a': 1, 'b': 2})
22+
dest = {'z': 0}
23+
dest.update(view)
24+
self.assertEqual(dest, {'z': 0, 'a': 1, 'b': 2})
25+
26+
def test_dict_constructor_from_mappingproxy_userdict(self):
27+
self.assertEqual(
28+
dict(types.MappingProxyType(collections.UserDict(x=1))),
29+
{'x': 1},
30+
)
31+
32+
33+
if __name__ == '__main__':
34+
unittest.main()

0 commit comments

Comments
 (0)