Skip to content

reprs of nested duck arrays #6637

@keewis

Description

@keewis

Part of the discussion in #5329. I'm opening this issue to make the discussion on this topic a bit more focused.

As mentioned in #5329 (comment), we need to find a way to

  • construct a repr that includes the metadata from all levels of nesting?

Simply including the repr of the wrapped array usually results in reprs that are way too long, so instead we could introduce a protocol (or method) that returns a mapping of type names to metadata, and a function to call that protocol. If a duck array does not implement the protocol, the default would be to return {typename: {}}.
For example calling repr(arr) where arr is a xarray(pint(dask(sparse))) would make xarray call the data's __metadata__ and use that to construct it's repr. This could be something like

def __metadata__(self):
    wrapped_metadata = self._data.__metadata__()
    metadata = {
        "a": str(self.a),
        "b": str(self.b),
    }
    # or something like {**wrapped_metadata, **{type(self).__qualname__: metadata}
    return wrapped_metadata | {type(self).__qualname__: metadata}

In the example, the result of the first call could be something like:

{
    "sparse.COO": {"shape": (30, 1000), "dtype": "float64", "nnz": 768, "fill_value": 0},
    "dask.array.Array": {"chunks": (10, 100), "shape": (30, 1000), "dtype": "float64"},
    "pint.Quantity": {"units": "m", "shape": (30, 1000), "dtype": "float64"},
}

with that, xarray could either manually format the repr or use a helper function (which should probably have a max_width parameter).

This doesn't work for duck arrays wrapping more than a single duck array, though. Also, I'm not sure if the type name should be the type's __qualname__, and if shape and dtype, which every duck array has to implement as properties, should be included.

xref pydata/xarray#4324, pydata/xarray#4248

Metadata

Metadata

Assignees

No one assigned

    Labels

    arraydiscussionDiscussing a topic with no specific actions yetneeds attentionIt's been a while since this was pushed on. Needs attention from the owner or a maintainer.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions