Skip to content

Commit 18c6926

Browse files
authored
Add variable/attribute names to netCDF validation errors (#4500)
* Add variable/attribute names to netCDF validation errors This should result in a better user experience, e.g., specifically pointing out the attribute with an invalid value. * fix lint
1 parent 49e3032 commit 18c6926

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

xarray/backends/api.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,15 @@ def check_name(name):
191191
if isinstance(name, str):
192192
if not name:
193193
raise ValueError(
194-
"Invalid name for DataArray or Dataset key: "
194+
f"Invalid name {name!r} for DataArray or Dataset key: "
195195
"string must be length 1 or greater for "
196196
"serialization to netCDF files"
197197
)
198198
elif name is not None:
199199
raise TypeError(
200-
"DataArray.name or Dataset key must be either a "
201-
"string or None for serialization to netCDF files"
200+
f"Invalid name {name!r} for DataArray or Dataset key: "
201+
"must be either a string or None for serialization to netCDF "
202+
"files"
202203
)
203204

204205
for k in dataset.variables:
@@ -214,22 +215,22 @@ def check_attr(name, value):
214215
if isinstance(name, str):
215216
if not name:
216217
raise ValueError(
217-
"Invalid name for attr: string must be "
218+
f"Invalid name for attr {name!r}: string must be "
218219
"length 1 or greater for serialization to "
219220
"netCDF files"
220221
)
221222
else:
222223
raise TypeError(
223-
"Invalid name for attr: {} must be a string for "
224-
"serialization to netCDF files".format(name)
224+
f"Invalid name for attr: {name!r} must be a string for "
225+
"serialization to netCDF files"
225226
)
226227

227228
if not isinstance(value, (str, Number, np.ndarray, np.number, list, tuple)):
228229
raise TypeError(
229-
"Invalid value for attr: {} must be a number, "
230+
f"Invalid value for attr {name!r}: {value!r} must be a number, "
230231
"a string, an ndarray or a list/tuple of "
231232
"numbers/strings for serialization to netCDF "
232-
"files".format(value)
233+
"files"
233234
)
234235

235236
# Check attrs on the dataset itself

xarray/tests/test_backends.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,11 +907,13 @@ def test_invalid_dataarray_names_raise(self):
907907
ve = (ValueError, "string must be length 1 or")
908908
data = np.random.random((2, 2))
909909
da = xr.DataArray(data)
910-
for name, e in zip([0, (4, 5), True, ""], [te, te, te, ve]):
910+
for name, (error, msg) in zip([0, (4, 5), True, ""], [te, te, te, ve]):
911911
ds = Dataset({name: da})
912-
with raises_regex(*e):
912+
with pytest.raises(error) as excinfo:
913913
with self.roundtrip(ds):
914914
pass
915+
excinfo.match(msg)
916+
excinfo.match(repr(name))
915917

916918
def test_encoding_kwarg(self):
917919
ds = Dataset({"x": ("y", np.arange(10.0))})
@@ -4307,17 +4309,17 @@ def new_dataset_and_coord_attrs():
43074309
ds, attrs = new_dataset_and_attrs()
43084310

43094311
attrs[123] = "test"
4310-
with raises_regex(TypeError, "Invalid name for attr"):
4312+
with raises_regex(TypeError, "Invalid name for attr: 123"):
43114313
ds.to_netcdf("test.nc")
43124314

43134315
ds, attrs = new_dataset_and_attrs()
43144316
attrs[MiscObject()] = "test"
4315-
with raises_regex(TypeError, "Invalid name for attr"):
4317+
with raises_regex(TypeError, "Invalid name for attr: "):
43164318
ds.to_netcdf("test.nc")
43174319

43184320
ds, attrs = new_dataset_and_attrs()
43194321
attrs[""] = "test"
4320-
with raises_regex(ValueError, "Invalid name for attr"):
4322+
with raises_regex(ValueError, "Invalid name for attr '':"):
43214323
ds.to_netcdf("test.nc")
43224324

43234325
# This one should work
@@ -4328,12 +4330,12 @@ def new_dataset_and_coord_attrs():
43284330

43294331
ds, attrs = new_dataset_and_attrs()
43304332
attrs["test"] = {"a": 5}
4331-
with raises_regex(TypeError, "Invalid value for attr"):
4333+
with raises_regex(TypeError, "Invalid value for attr 'test'"):
43324334
ds.to_netcdf("test.nc")
43334335

43344336
ds, attrs = new_dataset_and_attrs()
43354337
attrs["test"] = MiscObject()
4336-
with raises_regex(TypeError, "Invalid value for attr"):
4338+
with raises_regex(TypeError, "Invalid value for attr 'test'"):
43374339
ds.to_netcdf("test.nc")
43384340

43394341
ds, attrs = new_dataset_and_attrs()

0 commit comments

Comments
 (0)