Skip to content

Commit

Permalink
fix __asdf_traverse__ for non-tagged object
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Feb 27, 2024
1 parent 698bd30 commit 083641e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion asdf/_node_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,13 @@ def from_root_node(cls, key, root_identifier, root_node, schema=None, refresh_ex

if cls.traversable(node):
t_node = node.__asdf_traverse__()
info.set_schema_from_node(node, extension_manager)
if hasattr(node, "_tag") and isinstance(node._tag, str):
try:
info.set_schema_from_node(node, extension_manager)
except KeyError:
# if _tag is not a valid tag, no schema will be found
# and a KeyError will be raised
pass

else:
t_node = node
Expand Down
34 changes: 34 additions & 0 deletions asdf/_tests/_regtests/test_1738.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import asdf


def test_info_on_non_tagged_asdf_traverse_object():
"""
Calling info with a tree containing an object that implements
__asdf_traverse__ but does not have a _tag results in an
error
https://github.com/asdf-format/asdf/issues/1738
"""

class MyContainer:
def __init__(self, data):
self.data = data

def __asdf_traverse__(self):
return self.data

c = MyContainer([1, 2, 3])
af = asdf.AsdfFile()
af["c"] = c

# info should not error out
af.info()

# and search should work with the container
assert af.search(type_=int).paths == ["root['c'][0]", "root['c'][1]", "root['c'][2]"]

# this should work even if _tag exists (and is not a tag)
c._tag = {}
assert af.search(type_=int).paths == ["root['c'][0]", "root['c'][1]", "root['c'][2]"]
c._tag = "a"
assert af.search(type_=int).paths == ["root['c'][0]", "root['c'][1]", "root['c'][2]"]

0 comments on commit 083641e

Please sign in to comment.