render_dictionary_wrapper

render_dictionary_wrapper#

treescope.repr_lib.render_dictionary_wrapper(object_type: type[Any], wrapped_dict: Mapping[str, Any], path: str | None, subtree_renderer: renderers.TreescopeSubtreeRenderer, roundtrippable: bool = False, color: str | None = None) rendering_parts.Rendering[source]#

Renders an object in “wrapped dictionary format”.

This produces a rendering like Foo({"bar": 1, "baz": 2}), where Foo identifies the type of the object, and “bar” and “baz” are the keys in the dictionary that Foo acts like. It is a requirement that these are accessible through __getitem__, e.g. as obj["bar"] or similar; otherwise, the path renderings will break.

This can be used from within a __treescope_repr__ implementation via

def __treescope_repr__(self, path, subtree_renderer):
  return repr_lib.render_dictionary_wrapper(
      object_type=type(self),
      wrapped_dict=<dict of items here>,
      path=path,
      subtree_renderer=subtree_renderer,
  )
Parameters:
  • object_type – The type of the object.

  • wrapped_dict – The dictionary that the object wraps.

  • path – The path to the object. When render_object_constructor is called from __treescope_repr__, this should come from the path argument to __treescope_repr__.

  • subtree_renderer – The renderer to use to render subtrees. When render_object_constructor is called from __treescope_repr__, this should come from the subtree_renderer argument to __treescope_repr__.

  • roundtrippable – Whether evaluating the rendering as Python code will produce an object that is equal to the original object. This implies that the constructor for object_type takes a single argument, which is a dictionary, and that object_type then acts like that dictionary.

  • color – The background color to use for the object rendering. If None, does not use a background color. A utility for assigning a random color based on a string key is given in treescope.formatting_util. (By convention, wrapped dictionaries aren’t usually assigned a color in Treescope.)

Returns:

A rendering of the object, suitable for returning from __treescope_repr__.