Skip to content

HTML formatting of non-string attribute names fails #5146

Closed
@bcbnz

Description

@bcbnz

Working in a notebook (and presumably, anywhere else that uses the HTML formatter to show an array), non-string attribute keys cause an exception. The output then falls back to the repr value.

In [1]: import xarray as xr

In [2]: data = xr.DataArray([1, 2, 3], attrs={1: 3.14})

In [3]: data.attrs
Out[3]: {1: 3.14}

In [4]: data
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/lib/python3.9/site-packages/IPython/core/formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

~/software/external/xarray/xarray/core/common.py in _repr_html_(self)
    148         if OPTIONS["display_style"] == "text":
    149             return f"<pre>{escape(repr(self))}</pre>"
--> 150         return formatting_html.array_repr(self)
    151 
    152     def _iter(self: Any) -> Iterator[Any]:

~/software/external/xarray/xarray/core/formatting_html.py in array_repr(arr)
    269         sections.append(coord_section(arr.coords))
    270 
--> 271     sections.append(attr_section(arr.attrs))
    272 
    273     return _obj_repr(arr, header_components, sections)

~/software/external/xarray/xarray/core/formatting_html.py in _mapping_section(mapping, name, details_func, max_items_collapse, enabled)
    171     return collapsible_section(
    172         name,
--> 173         details=details_func(mapping),
    174         n_items=n_items,
    175         enabled=enabled,

~/software/external/xarray/xarray/core/formatting_html.py in summarize_attrs(attrs)
     47 
     48 def summarize_attrs(attrs):
---> 49     attrs_dl = "".join(
     50         f"<dt><span>{escape(k)} :</span></dt>" f"<dd>{escape(str(v))}</dd>"
     51         for k, v in attrs.items()

~/software/external/xarray/xarray/core/formatting_html.py in <genexpr>(.0)
     48 def summarize_attrs(attrs):
     49     attrs_dl = "".join(
---> 50         f"<dt><span>{escape(k)} :</span></dt>" f"<dd>{escape(str(v))}</dd>"
     51         for k, v in attrs.items()
     52     )

/usr/lib/python3.9/html/__init__.py in escape(s, quote)
     17     translated.
     18     """
---> 19     s = s.replace("&", "&amp;") # Must be done first!
     20     s = s.replace("<", "&lt;")
     21     s = s.replace(">", "&gt;")

AttributeError: 'int' object has no attribute 'replace'
Out[4]: <xarray.DataArray (dim_0: 3)>
        array([1, 2, 3])
        Dimensions without coordinates: dim_0
        Attributes:
            1:        3.14

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: c91983d
python: 3.9.2 (default, Feb 20 2021, 18:40:11)
[GCC 10.2.0]
python-bits: 64
OS: Linux
OS-release: 5.11.13-arch1-1
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_NZ.UTF-8
LOCALE: en_NZ.UTF-8
libhdf5: 1.12.0
libnetcdf: 4.7.4

xarray: 0.17.0
pandas: 1.2.3
numpy: 1.20.1
scipy: 1.6.2
netCDF4: 1.5.6
pydap: None
h5netcdf: 0.10.0
h5py: 3.2.1
Nio: None
zarr: None
cftime: 1.4.1
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.2.2
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2021.03.0
distributed: 2021.03.0
matplotlib: 3.4.1
cartopy: 0.18.0
seaborn: None
numbagg: None
pint: None
setuptools: 54.2.0
pip: 20.3.1
conda: None
pytest: 6.2.3
IPython: 7.22.0
sphinx: 3.5.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions