Skip to content

Commit b0a782d

Browse files
authored
make error message more clear (#10994)
* make error message more clear * improve error message and update tests
1 parent a987bb1 commit b0a782d

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

xarray/coding/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def safe_setitem(dest, key: Hashable, value, name: T_Name = None):
113113
if key in dest:
114114
var_str = f" on variable {name!r}" if name else ""
115115
raise ValueError(
116-
f"failed to prevent overwriting existing key {key} in attrs{var_str}. "
116+
f"Key '{key}' already exists in attrs{var_str}, and will not be overwritten. "
117117
"This is probably an encoding field used by xarray to describe "
118118
"how a variable is serialized. To proceed, remove this key from "
119119
"the variable's attributes manually."

xarray/tests/test_coding_times.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ def test_timedelta_decode_via_dtype_invalid_encoding() -> None:
20882088
attrs = {"dtype": "timedelta64[s]", "units": "seconds"}
20892089
encoding = {"units": "foo"}
20902090
encoded = Variable(["time"], [0, 1, 2], attrs=attrs, encoding=encoding)
2091-
with pytest.raises(ValueError, match="failed to prevent"):
2091+
with pytest.raises(ValueError, match=r"Key .* already exists"):
20922092
conventions.decode_cf_variable("timedeltas", encoded)
20932093

20942094

@@ -2097,7 +2097,7 @@ def test_timedelta_encode_via_dtype_invalid_attribute(attribute) -> None:
20972097
timedeltas = pd.timedelta_range(0, freq="D", periods=3)
20982098
attrs = {attribute: "foo"}
20992099
variable = Variable(["time"], timedeltas, attrs=attrs)
2100-
with pytest.raises(ValueError, match="failed to prevent"):
2100+
with pytest.raises(ValueError, match=r"Key .* already exists"):
21012101
conventions.encode_cf_variable(variable)
21022102

21032103

0 commit comments

Comments
 (0)