Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure no forward slashes in names for HDF5-based backends #7953

Merged
merged 7 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions xarray/backends/netCDF4_.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ 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:
kmuehlbauer marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(f"Forward slashes '/' are not allowed in name {name!r} for "
"HDF5-based files ('netcdf4'/'h5netcdf'). Forward slashes "
"are used as hierarchy-separators.")
kmuehlbauer marked this conversation as resolved.
Show resolved Hide resolved


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 +454,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 +478,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