This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: types.MappingProxyType and collections.defaultdict
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: guoci, rhettinger
Priority: normal Keywords:

Created on 2021-08-08 19:16 by guoci, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg399234 - (view) Author: Guo Ci Teo (guoci) * Date: 2021-08-08 19:16
`types.MappingProxyType` is documented as 'Read-only proxy of a mapping'.
But if used with a `collections.defaultdict` mapping, it can modify the underlying mapping.
```
import collections, types
dd = collections.defaultdict(set)
mpt = types.MappingProxyType(dd)
mpt['__getitem__'] # key inserted
mpt.get('get') # key not inserted
print(dd.items()) # dict_items([('__getitem__', set())])
```
msg399238 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-08-08 20:19
> `types.MappingProxyType` is documented as 
> 'Read-only proxy of a mapping'.  But if used with a 
> `collections.defaultdict` mapping, it can modify 
> the underlying mapping.

Technically, the underlying mapping is modifying itself.  That is allowed.

Also, there isn't really anything that the mapping proxy can do about it.  All MappingProxy can do it forward calls to methods that are usually non-mutating.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89030
2021-08-08 20:19:44rhettingersetstatus: open -> closed

assignee: rhettinger

nosy: + rhettinger
messages: + msg399238
resolution: not a bug
stage: resolved
2021-08-08 19:16:15guocicreate