[issue43828] MappingProxyType accepts string

Raymond Hettinger report at bugs.python.org
Tue Apr 13 23:09:09 EDT 2021


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

This isn't a bug because the MappingProxy doesn't promise to do early detection and because it can't really be accomplished cleanly.

Perfect detection of non-mappings isn't possible.  Some easy, minimal checks are made for early detection using PyMapping_Check, PyList_Check, and PyTuple_Check.  Anything that gets past those checks will just need to be duck-typed downstream when it is used.

We could add a few other specific exclusions but that isn't a general solution and it slows down instantiation.  Further it could break code that is intended to work (subclasses of str that mapping methods) or that just happen to work:

    >>> from types import MappingProxyType
    >>> mp = MappingProxyType('abc')
    >>> mp[0]
    'a'

----------
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43828>
_______________________________________


More information about the Python-bugs-list mailing list