[issue44580] pprint does not work for typing.Mapping

Hayden Clark report at bugs.python.org
Wed Jul 7 09:33:00 EDT 2021


New submission from Hayden Clark <pythonbugsmail at clarkccc.com>:

If you make a user-defined type that inherits from typing.Mapping (which is an alias for collections.abc.Mapping), pprint does not dump the contents, it just treats it as an unknown class.
Examining the code, it is explicitly checking for "dict" type, even though it is only reading the data.

Here is a reproduction:
from typing import Mapping
from pprint import pprint


class MyMap(Mapping):
    def __init__(self, **kwargs) -> None:
        self.data = {
            k:v for k, v in kwargs.items()
        }

    def __getitem__(self, k):
        return self.data.get(k)

    def __len__(self):
        return self.data.__len__()

    def __iter__(self):
        return self.data.__iter__()


info = MyMap(
    foo="bar",
    baz=MyMap(bar="foo")
)

pprint(info)

----------

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


More information about the Python-bugs-list mailing list