What magic method is required to expose the contents of a Python\Pydantic Dict-like object to PyCharm debug view

(I posted this also on: https://stackoverflow.com/questions/73206961/what-magic-method-is-required-to-expose-the-contents-of-a-python-pydantic-dict-l )

How can I view the contents of a Pydantic Dict-like object in PyCharm debug view?

I have a Python Data-Model that is based on Pydantic's BaseModel to create a class that behaves like a dict, but when viewing it in PyCharm debug view, it doesn't display the key-value contents like a normal dict would do.

Using latest PyCharm version "Renderer" functionality I was able to display "ExampleDict" type's "__root__" contents (similar to highlighted e1.__root__), but this will not work if I define a class that inherits from "ExampleDict". For the inherited class I need to declare a new renderer.

In any case, my question is:
How can I make e1 be displayed like e1.root ? What magic method does the debugger use to get the contents of dicts ?

e1.__root__ -> of type dict and is displayed as expected (identical to example_dict)
e1 -> of type ExampleDict - It's contents aren't displayed.

Code

import copy
from typing import Dict

from pydantic import BaseModel

example_dict = {
    'A': {'Hello': 'World'},
    'B': {'Foo': 'Bar'},
}


class ExampleDict(BaseModel):
    __root__: Dict[str, Dict]

    def __iter__(self):
        return iter(self.__root__)

    def __getitem__(self, item):
        return self.__root__[item]

    def keys(self):
        return self.__root__.keys()

    def values(self):
        return self.__root__.values()

    def items(self):
        return self.__root__.items()

    def __copy__(self):
        return self.__root__.__copy__

    # def __dir__(self) -> Iterable[str]:
    #     return self.__root__.__dir__()

    def __repr__(self):
        return self.__root__.__repr__()

    def __deepcopy__(self, memodict={}):
        return copy.deepcopy(self.__root__)


if __name__ == '__main__':
    e1 = ExampleDict.parse_obj(example_dict)
    print('The End')

Please sign in to leave a comment.