From bb0a312e015c473c84dbec2bb4f02d4a0ea5db58 Mon Sep 17 00:00:00 2001 From: Ed Slavich Date: Thu, 2 Dec 2021 11:19:44 -0500 Subject: [PATCH] Remove 'name' and 'version' attributes from NDArrayType instances --- CHANGES.rst | 3 +++ asdf/tags/core/ndarray.py | 12 ++++++++++++ asdf/tags/core/tests/test_ndarray.py | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 04079b2a3..3b0f108ef 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -13,6 +13,9 @@ - Fix schema URI resolving when the URI prefix is also claimed by a legacy extension. [#1029] +- Remove 'name' and 'version' attributes from NDArrayType + instances. [#1031] + 2.8.1 (2021-06-09) ------------------ diff --git a/asdf/tags/core/ndarray.py b/asdf/tags/core/ndarray.py index acdb9cb02..93dc43c04 100644 --- a/asdf/tags/core/ndarray.py +++ b/asdf/tags/core/ndarray.py @@ -368,6 +368,18 @@ def __setitem__(self, *args): self._array = None raise e from None + def __getattribute__(self, name): + # The presence of these attributes on an NDArrayType instance + # can cause problems when the array is passed to other + # libraries. + # See https://github.com/asdf-format/asdf/issues/1015 + if name in ("name", "version"): + raise AttributeError( + f"'{self.__class__.name}' object has no attribute '{name}'" + ) + else: + return super().__getattribute__(name) + @classmethod def from_tree(cls, node, ctx): if isinstance(node, list): diff --git a/asdf/tags/core/tests/test_ndarray.py b/asdf/tags/core/tests/test_ndarray.py index 6ae6887c4..243aacfa3 100644 --- a/asdf/tags/core/tests/test_ndarray.py +++ b/asdf/tags/core/tests/test_ndarray.py @@ -895,3 +895,26 @@ def test_block_data_change(tmpdir): af.update() array_after = af.tree["data"].__array__() assert array_before is not array_after + + +def test_problematic_class_attributes(tmp_path): + """ + The presence of the "name" and "version" attributes + in NDArrayType cause problems when our arrays are used + with other libraries. + + See https://github.com/asdf-format/asdf/issues/1015 + """ + file_path = tmp_path / "test.asdf" + with asdf.AsdfFile() as af: + af["arr"] = np.arange(100) + af.write_to(file_path) + + with asdf.open(file_path) as af: + assert isinstance(af["arr"], ndarray.NDArrayType) + + with pytest.raises(AttributeError): + af["arr"].name + + with pytest.raises(AttributeError): + af["arr"].version