diff --git a/CHANGES.rst b/CHANGES.rst index f92ac534d..c5b0aa0a6 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) diff --git a/asdf/_asdf.py b/asdf/_asdf.py index 84965681f..ddd306be0 100644 --- a/asdf/_asdf.py +++ b/asdf/_asdf.py @@ -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, @@ -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 @@ -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, @@ -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, @@ -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, @@ -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 ------- @@ -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, diff --git a/asdf/_tests/tags/core/tests/test_integer.py b/asdf/_tests/tags/core/tests/test_integer.py index 2a2b13a7e..dab126ee6 100644 --- a/asdf/_tests/tags/core/tests/test_integer.py +++ b/asdf/_tests/tags/core/tests/test_integer.py @@ -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"] diff --git a/asdf/_tests/test_asdf.py b/asdf/_tests/test_asdf.py index 0ae8dd491..e4a079585 100644 --- a/asdf/_tests/test_asdf.py +++ b/asdf/_tests/test_asdf.py @@ -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) diff --git a/asdf/_tests/test_deprecated.py b/asdf/_tests/test_deprecated.py index 47e478e13..4d3bc1636 100644 --- a/asdf/_tests/test_deprecated.py +++ b/asdf/_tests/test_deprecated.py @@ -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) diff --git a/asdf/commands/diff.py b/asdf/commands/diff.py index a2a58450f..7bce1ba40 100644 --- a/asdf/commands/diff.py +++ b/asdf/commands/diff.py @@ -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: diff --git a/asdf/commands/edit.py b/asdf/commands/edit.py index 05ff0c09a..6d2a43561 100644 --- a/asdf/commands/edit.py +++ b/asdf/commands/edit.py @@ -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:")