diff --git a/src/awkward/_attrs.py b/src/awkward/_attrs.py index e0e837eb73..1a904ba9a9 100644 --- a/src/awkward/_attrs.py +++ b/src/awkward/_attrs.py @@ -76,10 +76,10 @@ def to_dict(self): return _unfreeze_attrs(self._data) -def _enforce_str_key(attr: Any) -> str: - if not isinstance(attr, str): - raise TypeError(f"'attrs' keys must be strings: {attr!r}") - return attr +def _enforce_str_key(key: Any) -> str: + if not isinstance(key, str): + raise TypeError(f"'attrs' keys must be strings, got: {key!r}") + return key def _freeze_attrs(attrs: Mapping[str, Any]) -> Mapping[str, Any]: diff --git a/tests/test_3350_enforce_attrs_string_keys.py b/tests/test_3350_enforce_attrs_string_keys.py new file mode 100644 index 0000000000..20c1123c18 --- /dev/null +++ b/tests/test_3350_enforce_attrs_string_keys.py @@ -0,0 +1,17 @@ +# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE +# ruff: noqa: E402 + +from __future__ import annotations + +import pytest +import awkward as ak + + +def test(): + arr = ak.Array([1]) + + with pytest.raises( + TypeError, + match="'attrs' keys must be strings, got: 1", + ): + arr.attrs[1] = "foo"