Skip to content

Commit

Permalink
ensure no forward slashes in names for HDF5-based backends (#7953)
Browse files Browse the repository at this point in the history
* ensure no forward slashes in names for HDF5-based backends

* fix mypy

* Update xarray/backends/netCDF4_.py

Co-authored-by: Joe Hamman <jhamman1@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Joe Hamman <jhamman1@gmail.com>
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 6, 2023
1 parent 58096a6 commit 17c9e8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Deprecations
Bug fixes
~~~~~~~~~

- Ensure no forward slashes in variable and dimension names for HDF5-based engines.
(:issue:`7943`, :pull:`7953`) By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions xarray/backends/h5netcdf_.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from xarray.backends.netCDF4_ import (
BaseNetCDF4Array,
_encode_nc4_variable,
_ensure_no_forward_slash_in_name,
_extract_nc4_variable_encoding,
_get_datatype,
_nc4_require_group,
Expand Down Expand Up @@ -256,6 +257,7 @@ def get_encoding(self):
}

def set_dimension(self, name, length, is_unlimited=False):
_ensure_no_forward_slash_in_name(name)
if is_unlimited:
self.ds.dimensions[name] = None
self.ds.resize_dimension(name, length)
Expand All @@ -273,6 +275,7 @@ def prepare_variable(
):
import h5py

_ensure_no_forward_slash_in_name(name)
attrs = variable.attrs.copy()
dtype = _get_datatype(variable, raise_on_invalid_encoding=check_encoding)

Expand Down
12 changes: 12 additions & 0 deletions xarray/backends/netCDF4_.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ def _nc4_require_group(ds, group, mode, create_group=_netcdf4_create_group):
return ds


def _ensure_no_forward_slash_in_name(name):
if "/" in name:
raise ValueError(
f"Forward slashes '/' are not allowed in variable and dimension names (got {name!r}). "
"Forward slashes are used as hierarchy-separators for "
"HDF5-based files ('netcdf4'/'h5netcdf')."
)


def _ensure_fill_value_valid(data, attributes):
# work around for netCDF4/scipy issue where _FillValue has the wrong type:
# https://github.com/Unidata/netcdf4-python/issues/271
Expand Down Expand Up @@ -447,6 +456,7 @@ def get_encoding(self):
}

def set_dimension(self, name, length, is_unlimited=False):
_ensure_no_forward_slash_in_name(name)
dim_length = length if not is_unlimited else None
self.ds.createDimension(name, size=dim_length)

Expand All @@ -470,6 +480,8 @@ def encode_variable(self, variable):
def prepare_variable(
self, name, variable, check_encoding=False, unlimited_dims=None
):
_ensure_no_forward_slash_in_name(name)

datatype = _get_datatype(
variable, self.format, raise_on_invalid_encoding=check_encoding
)
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,20 @@ def test_encoding_unlimited_dims(self) -> None:
assert actual.encoding["unlimited_dims"] == set("y")
assert_equal(ds, actual)

def test_raise_on_forward_slashes_in_names(self) -> None:
# test for forward slash in variable names and dimensions
# see GH 7943
data_vars: list[dict[str, Any]] = [
{"PASS/FAIL": (["PASSFAIL"], np.array([0]))},
{"PASS/FAIL": np.array([0])},
{"PASSFAIL": (["PASS/FAIL"], np.array([0]))},
]
for dv in data_vars:
ds = Dataset(data_vars=dv)
with pytest.raises(ValueError, match="Forward slashes '/' are not allowed"):
with self.roundtrip(ds):
pass


@requires_netCDF4
class TestNetCDF4Data(NetCDF4Base):
Expand Down

0 comments on commit 17c9e8f

Please sign in to comment.