Skip to content

Commit

Permalink
Merge pull request #1031 from eslavich/eslavich-alternate-ndarraytype…
Browse files Browse the repository at this point in the history
…-fix

Remove 'name' and 'version' attributes from NDArrayType instances
  • Loading branch information
eslavich authored Dec 3, 2021
2 parents 930e7f0 + bb0a312 commit 926ffe0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------

Expand Down
12 changes: 12 additions & 0 deletions asdf/tags/core/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 23 additions & 0 deletions asdf/tags/core/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 926ffe0

Please sign in to comment.