Skip to content

Commit

Permalink
ignore missing BuiltinExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Aug 31, 2023
1 parent 77627df commit e428962
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
23 changes: 23 additions & 0 deletions asdf/_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,29 @@ class FooExtension:
af._check_extensions(tree)


def test_extension_check_no_warning_on_builtin():
"""
Prior to asdf 3.0 files were written using the asdf.extension.BuiltinExtension
(which used the legacy extension api). This extension was removed in
asdf 3.0. We don't want to warn that this extension is missing for every
file that is opened so make sure _check_extensions doesn't warn
that BuiltinExtension is missing.
"""
af = asdf.AsdfFile()
tree = {
"history": {
"extensions": [
asdf.tags.core.ExtensionMetadata(
extension_class="asdf.extension.BuiltinExtension",
software=asdf.tags.core.Software(name="asdf", version="2.15.1"),
),
],
},
}
with assert_no_warnings():
af._check_extensions(tree)


@pytest.mark.parametrize(
("array_inline_threshold", "inline_blocks", "internal_blocks"),
[
Expand Down
43 changes: 27 additions & 16 deletions asdf/_tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import asdf
from asdf.exceptions import AsdfWarning, ValidationError
from asdf.extension import Converter, Extension
from asdf.extension import Converter, Extension, ExtensionProxy
from asdf.tags.core import HistoryEntry
from asdf.testing import helpers

Expand Down Expand Up @@ -131,30 +131,41 @@ def test_missing_extension_warning():


def test_extension_version_warning():
yaml = """
uri = "asdf://somewhere.org/extensions/foo-1.0.0"
package_name = "foo"
file_package_version = "2.0.0"
installed_package_version = "1.0.0"

class FooExtension:
extension_uri = uri

yaml = f"""
history:
extensions:
- !core/extension_metadata-1.0.0
extension_class: asdf.extension.BuiltinExtension
extension_class: {FooExtension.__qualname__}
extension_uri: {uri}
software: !core/software-1.0.0
name: asdf
version: 100.0.3
name: {package_name}
version: {file_package_version}
"""

buff = helpers.yaml_to_asdf(yaml)
with pytest.warns(
AsdfWarning,
match=r"File was created with extension class 'asdf.extension.BuiltinExtension'",
), asdf.open(buff):
pass
with asdf.config_context() as cfg:
cfg.add_extension(ExtensionProxy(FooExtension(), package_name, installed_package_version))
with pytest.warns(
AsdfWarning,
match=f"older package \\({package_name}=={installed_package_version}\\)",
), asdf.open(buff):
pass

buff.seek(0)
buff.seek(0)

# Make sure suppressing the warning works too
with warnings.catch_warnings():
warnings.simplefilter("error")
with asdf.open(buff, ignore_missing_extensions=True):
pass
# Make sure suppressing the warning works too
with warnings.catch_warnings():
warnings.simplefilter("error")
with asdf.open(buff, ignore_missing_extensions=True):
pass


def test_strict_extension_check():
Expand Down
8 changes: 8 additions & 0 deletions asdf/asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def __init__(
files follow custom conventions beyond those enforced by the
standard.
"""
self._fname = ""

# Don't use the version setter here; it tries to access
# the extensions, which haven't been assigned yet.
if version is None:
Expand Down Expand Up @@ -292,6 +294,12 @@ def _check_extensions(self, tree, strict=False):
return

for extension in tree["history"]["extensions"]:
# asdf 3.0 removed the BuiltinExtension and handles all
# core objects with a ManifestExtension so don't warn if
# a file is opened that was created with an older asdf
# which used the BuiltinExtension (which is no longer installed)
if extension.get("extension_class", None) == "asdf.extension.BuiltinExtension":
continue
installed = None
for ext in self._user_extensions + self._plugin_extensions:
if (
Expand Down

0 comments on commit e428962

Please sign in to comment.