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: pprint does not work for typing.Mapping
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: hclark
Priority: normal Keywords:

Created on 2021-07-07 13:28 by hclark, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg397084 - (view) Author: Hayden Clark (hclark) Date: 2021-07-07 13:33
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)
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88746
2021-07-07 13:33:00hclarksetmessages: + msg397084
2021-07-07 13:28:25hclarkcreate