Skip to content

Commit

Permalink
convert as_tagged to tree_type
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Dec 6, 2023
1 parent af8480c commit b34abb3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The ASDF Standard is at v1.6.0
- Deprecate ``asdf.asdf`` and ``AsdfFile.resolve_and_inline`` [#1690]

- Deprecate ``_force_raw_types`` argument to ``asdf.open`` and replace
with ``as_tagged`` (also deprecate ``force_raw_types`` argument
with ``tree_type`` (also deprecate ``force_raw_types`` argument
to ``yamlutil.tagged_tree_to_custom_tree`` with no replacement) [#1677]

3.0.1 (2023-10-30)
Expand Down
31 changes: 21 additions & 10 deletions asdf/_asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ def _open_asdf(
fd,
validate_checksums=False,
extensions=None,
tree_type="custom",
_get_yaml_content=False,
_force_raw_types=NotSet,
strict_extension_check=False,
Expand Down Expand Up @@ -853,14 +854,21 @@ def _open_asdf(

if _force_raw_types is not NotSet:
warnings.warn(
"_force_raw_types is deprecated and will be replaced by as_tagged", AsdfDeprecationWarning
"_force_raw_types is deprecated and will be replaced by tree_type", AsdfDeprecationWarning
)
as_tagged = _force_raw_types
tree_type = "tagged" if _force_raw_types else "custom"

if not as_tagged:
if tree_type is None:
tree_type = "custom"

if tree_type not in ("custom", "tagged"):
msg = f"Unsupported tree type {tree_type}"
raise ValueError(msg)

if tree_type == "custom":
tree = yamlutil.tagged_tree_to_custom_tree(tree, self)

if not (ignore_missing_extensions or as_tagged):
if tree_type == "custom" and not ignore_missing_extensions:
self._check_extensions(tree, strict=strict_extension_check)

self._tree = tree
Expand All @@ -876,6 +884,7 @@ def _open_impl(
mode="r",
validate_checksums=False,
extensions=None,
tree_type=None,
_get_yaml_content=False,
_force_raw_types=NotSet,
strict_extension_check=False,
Expand All @@ -890,7 +899,7 @@ def _open_impl(
generic_file,
validate_checksums=validate_checksums,
extensions=extensions,
as_tagged=as_tagged,
tree_type=tree_type,
_get_yaml_content=_get_yaml_content,
_force_raw_types=_force_raw_types,
strict_extension_check=strict_extension_check,
Expand Down Expand Up @@ -1495,7 +1504,7 @@ def open_asdf(
extensions=None,
ignore_version_mismatch=True,
ignore_unrecognized_tag=False,
as_tagged=False,
tree_type=None,
_force_raw_types=NotSet,
copy_arrays=False,
lazy_load=True,
Expand Down Expand Up @@ -1569,9 +1578,11 @@ def open_asdf(
contains metadata about extensions that are not available. Defaults
to `False`.
as_tagged : bool, optional
When `True` do not convert the ASDF tree to custom types and instead
returned `asdf.tagged` objects.
tree_type : str, optional
Controls the tree type for the returned AsdfFile instance. Supported
values are:
"custom" (default), for a tree with custom objects
"tagged", for a tree with `asdf.tagged` objects
Returns
-------
Expand Down Expand Up @@ -1607,7 +1618,7 @@ def open_asdf(
mode=mode,
validate_checksums=validate_checksums,
extensions=extensions,
as_tagged=as_tagged,
tree_type=tree_type,
_get_yaml_content=_get_yaml_content,
_force_raw_types=_force_raw_types,
strict_extension_check=strict_extension_check,
Expand Down
2 changes: 1 addition & 1 deletion asdf/_tests/tags/core/tests/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_integer_storage(tmpdir, inline):
with asdf.AsdfFile(tree) as af:
af.write_to(tmpfile)

with asdf.open(tmpfile, as_tagged=True) as rf:
with asdf.open(tmpfile, tree_type="tagged") as rf:
if inline:
assert "source" not in rf.tree["integer"]["words"]
assert "data" in rf.tree["integer"]["words"]
Expand Down
13 changes: 9 additions & 4 deletions asdf/_tests/test_asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,18 @@ def test_fsspec_http(httpserver):
assert_tree_match(tree, af.tree)


@pytest.mark.parametrize("as_tagged", [True, False])
def test_asdf_open_as_tagged(tmp_path, as_tagged):
@pytest.mark.parametrize("tree_type", [None, "custom", "tagged", "unknown"])
def test_asdf_open_tree_type(tmp_path, tree_type):
fn = tmp_path / "test.asdf"
asdf.AsdfFile({"a": np.zeros(3)}).write_to(fn)

with asdf.open(fn, as_tagged=as_tagged) as af:
if as_tagged:
if tree_type == "unknown":
with pytest.raises(ValueError, match=f"Unsupported tree type {tree_type}"), asdf.open(fn, tree_type=tree_type):
pass
return

with asdf.open(fn, tree_type=tree_type) as af:
if tree_type == "tagged":
assert isinstance(af["a"], asdf.tagged.TaggedDict)
else:
assert isinstance(af["a"], asdf.tags.core.ndarray.NDArrayType)
2 changes: 1 addition & 1 deletion asdf/_tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_tagged_tree_to_custom_tree_force_raw_types_deprecation(tmp_path, force_
fn = tmp_path / "test.asdf"
asdf.AsdfFile({"a": np.zeros(3)}).write_to(fn)

with asdf.open(fn, as_tagged=True) as af:
with asdf.open(fn, tree_type="tagged") as af:
with pytest.warns(AsdfDeprecationWarning, match="force_raw_types is deprecated"):
asdf.yamlutil.tagged_tree_to_custom_tree(af.tree, af, force_raw_types)

Expand Down
4 changes: 2 additions & 2 deletions asdf/commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ def diff(filenames, minimal, iostream=sys.stdout, ignore=None):
ignore_expressions = [] if ignore is None else [jmespath.compile(e) for e in ignore]

try:
with asdf.open(filenames[0], as_tagged=True) as asdf0, asdf.open(
with asdf.open(filenames[0], tree_type="tagged") as asdf0, asdf.open(
filenames[1],
as_tagged=True,
tree_type="tagged",
) as asdf1:
ignore_ids = set()
for expression in ignore_expressions:
Expand Down
2 changes: 1 addition & 1 deletion asdf/commands/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def edit(path):
# Blocks are not read during validation, so this will not raise
# an error even though we're only opening the YAML portion of
# the file.
with open_asdf(io.BytesIO(new_content), as_tagged=True):
with open_asdf(io.BytesIO(new_content), tree_type="tagged"):
pass
except yaml.YAMLError as e:
print("Error: failed to parse updated YAML:")
Expand Down

0 comments on commit b34abb3

Please sign in to comment.