Skip to content

Commit

Permalink
Merge pull request #1409 from braingram/deprecate/legacy_extension_fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
WilliamJamieson committed Feb 22, 2023
2 parents fe64b85 + 3cf1a85 commit b94cfae
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The ASDF Standard is at v1.6.0
and impending removal [#1411]
- Deprecate AsdfFile attributes that use the legacy extension api [#1417]
- Add AsdfDeprecationWarning to asdf.types [#1401]
- deprecate default_extensions, get_default_resolver and
get_cached_asdf_extension_list in asdf.extension [#1409]

2.14.3 (2022-12-15)
-------------------
Expand Down
10 changes: 2 additions & 8 deletions asdf/asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@
from ._helpers import validate_version
from .config import config_context, get_config
from .exceptions import AsdfConversionWarning, AsdfDeprecationWarning, AsdfWarning
from .extension import (
AsdfExtension,
AsdfExtensionList,
Extension,
ExtensionProxy,
get_cached_asdf_extension_list,
get_cached_extension_manager,
)
from .extension import AsdfExtension, AsdfExtensionList, Extension, ExtensionProxy, get_cached_extension_manager
from .extension._legacy import get_cached_asdf_extension_list
from .search import AsdfSearchResult
from .tags.core import AsdfObject, ExtensionMetadata, HistoryEntry, Software
from .util import NotSet
Expand Down
66 changes: 58 additions & 8 deletions asdf/extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
Support for plugins that extend asdf to serialize
additional custom types.
"""
import warnings

from asdf.exceptions import AsdfDeprecationWarning

from . import _legacy
from ._compressor import Compressor
from ._converter import Converter, ConverterProxy
from ._extension import Extension, ExtensionProxy
from ._legacy import (
AsdfExtension,
AsdfExtensionList,
BuiltinExtension,
default_extensions,
get_cached_asdf_extension_list,
get_default_resolver,
)
from ._legacy import AsdfExtension, AsdfExtensionList, BuiltinExtension
from ._manager import ExtensionManager, get_cached_extension_manager
from ._manifest import ManifestExtension
from ._tag import TagDefinition
Expand All @@ -38,3 +36,55 @@
"get_default_resolver",
"get_cached_asdf_extension_list",
]


def get_cached_asdf_extension_list(extensions):
"""
Get a previously created AsdfExtensionList for the specified
extensions, or create and cache one if necessary. Building
the type index is expensive, so it helps performance to reuse
the index when possible.
Parameters
----------
extensions : list of asdf.extension.AsdfExtension
Returns
-------
asdf.extension.AsdfExtensionList
"""
from ._legacy import get_cached_asdf_extension_list

warnings.warn(
"get_cached_asdf_extension_list is deprecated. "
"Please see the new extension API "
"https://asdf.readthedocs.io/en/stable/asdf/extending/converters.html",
AsdfDeprecationWarning,
)
return get_cached_asdf_extension_list(extensions)


def get_default_resolver():
"""
Get the resolver that includes mappings from all installed extensions.
"""
from ._legacy import get_default_resolver

warnings.warn(
"get_default_resolver is deprecated. "
"Please see the new extension API "
"https://asdf.readthedocs.io/en/stable/asdf/extending/converters.html",
AsdfDeprecationWarning,
)
return get_default_resolver()


def __getattr__(name):
if name == "default_extensions":
warnings.warn(
"default_extensions is deprecated. "
"Please see the new extension API "
"https://asdf.readthedocs.io/en/stable/asdf/extending/converters.html",
AsdfDeprecationWarning,
)
return _legacy.default_extensions
msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)
9 changes: 5 additions & 4 deletions asdf/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from . import constants, extension, generic_io, reference, tagged, treeutil, util, versioning, yamlutil
from .config import get_config
from .exceptions import AsdfDeprecationWarning, AsdfWarning
from .extension import _legacy
from .util import patched_urllib_parse

YAML_SCHEMA_METASCHEMA_ID = "http://stsci.edu/schemas/yaml-schema/draft-01"
Expand Down Expand Up @@ -242,7 +243,7 @@ def _make_seen_key(self, instance, schema):

@lru_cache
def _create_validator(validators=YAML_VALIDATORS, visit_repeat_nodes=False):
meta_schema = _load_schema_cached(YAML_SCHEMA_METASCHEMA_ID, extension.get_default_resolver(), False, False)
meta_schema = _load_schema_cached(YAML_SCHEMA_METASCHEMA_ID, _legacy.get_default_resolver(), False, False)

type_checker = mvalidators.Draft4Validator.TYPE_CHECKER.redefine_many(
{
Expand Down Expand Up @@ -441,7 +442,7 @@ def load_schema(url, resolver=None, resolve_references=False, resolve_local_refs
if resolver is None:
# We can't just set this as the default in load_schema's definition
# because invoking get_default_resolver at import time leads to a circular import.
resolver = extension.get_default_resolver()
resolver = _legacy.get_default_resolver()

# We want to cache the work that went into constructing the schema, but returning
# the same object is treacherous, because users who mutate the result will not
Expand Down Expand Up @@ -762,9 +763,9 @@ def applicable_validators(schema):
applicable_validators = methodcaller("items")

meta_schema_id = schema.get("$schema", YAML_SCHEMA_METASCHEMA_ID)
meta_schema = _load_schema_cached(meta_schema_id, extension.get_default_resolver(), False, False)
meta_schema = _load_schema_cached(meta_schema_id, _legacy.get_default_resolver(), False, False)

resolver = _make_resolver(extension.get_default_resolver())
resolver = _make_resolver(_legacy.get_default_resolver())

cls = mvalidators.create(
meta_schema=meta_schema,
Expand Down
4 changes: 2 additions & 2 deletions asdf/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from asdf.block import Block
from asdf.constants import YAML_TAG_PREFIX
from asdf.exceptions import AsdfConversionWarning, AsdfDeprecationWarning
from asdf.extension import default_extensions
from asdf.extension import _legacy
from asdf.tags.core import AsdfObject
from asdf.versioning import (
AsdfVersion,
Expand Down Expand Up @@ -98,7 +98,7 @@ def assert_tree_match(old_tree, new_tree, ctx=None, funcname="assert_equal", ign

if ctx is None:
version_string = str(versioning.default_version)
ctx = default_extensions.extension_list
ctx = _legacy.default_extensions.extension_list
else:
version_string = ctx.version_string

Expand Down
3 changes: 2 additions & 1 deletion asdf/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ def test_resolver_deprecations():


def test_get_default_resolver():
resolver = extension.get_default_resolver()
with pytest.warns(AsdfDeprecationWarning, match="get_default_resolver is deprecated"):
resolver = extension.get_default_resolver()

result = resolver("tag:stsci.edu:asdf/core/ndarray-1.0.0")

Expand Down
16 changes: 16 additions & 0 deletions asdf/tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import asdf
import asdf.extension
from asdf._types import CustomType
from asdf.exceptions import AsdfDeprecationWarning
from asdf.tests.helpers import assert_extension_correctness
Expand Down Expand Up @@ -68,3 +69,18 @@ def test_types_module_deprecation():
if "asdf.types" in sys.modules:
del sys.modules["asdf.types"]
import asdf.types # noqa: F401


def test_default_extensions_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="default_extensions is deprecated"):
asdf.extension.default_extensions


def test_default_resolver():
with pytest.warns(AsdfDeprecationWarning, match="get_default_resolver is deprecated"):
asdf.extension.get_default_resolver()


def test_get_cached_asdf_extension_list_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="get_cached_asdf_extension_list is deprecated"):
asdf.extension.get_cached_asdf_extension_list([])
9 changes: 6 additions & 3 deletions asdf/tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,12 @@ def test_converter_proxy():

def test_get_cached_asdf_extension_list():
extension = LegacyExtension()
extension_list = get_cached_asdf_extension_list([extension])
assert get_cached_asdf_extension_list([extension]) is extension_list
assert get_cached_asdf_extension_list([LegacyExtension()]) is not extension_list
with pytest.warns(AsdfDeprecationWarning, match="get_cached_asdf_extension_list is deprecated"):
extension_list = get_cached_asdf_extension_list([extension])
with pytest.warns(AsdfDeprecationWarning, match="get_cached_asdf_extension_list is deprecated"):
assert get_cached_asdf_extension_list([extension]) is extension_list
with pytest.warns(AsdfDeprecationWarning, match="get_cached_asdf_extension_list is deprecated"):
assert get_cached_asdf_extension_list([LegacyExtension()]) is not extension_list


def test_manifest_extension():
Expand Down
15 changes: 9 additions & 6 deletions asdf/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def test_load_schema_with_tag_address(tmp_path):


def test_load_schema_with_file_url(tmp_path):
schema_def = """
with pytest.warns(AsdfDeprecationWarning, match="get_default_resolver is deprecated"):
schema_def = """
%YAML 1.1
%TAG !asdf! tag:stsci.edu:asdf/
---
Expand All @@ -151,9 +152,9 @@ def test_load_schema_with_file_url(tmp_path):
required: [foobar]
...
""".format(
extension.get_default_resolver()("tag:stsci.edu:asdf/core/ndarray-1.0.0"),
)
""".format(
extension.get_default_resolver()("tag:stsci.edu:asdf/core/ndarray-1.0.0"),
)
schema_path = tmp_path / "nugatory.yaml"
schema_path.write_bytes(schema_def.encode())

Expand Down Expand Up @@ -637,9 +638,11 @@ def test_self_reference_resolution():

def test_schema_resolved_via_entry_points():
"""Test that entry points mappings to core schema works"""
r = extension.get_default_resolver()
with pytest.warns(AsdfDeprecationWarning, match="get_default_resolver is deprecated"):
r = extension.get_default_resolver()
tag = types.format_tag("stsci.edu", "asdf", "1.0.0", "fits/fits")
url = extension.default_extensions.extension_list.tag_mapping(tag)
with pytest.warns(AsdfDeprecationWarning, match="default_extensions is deprecated"):
url = extension.default_extensions.extension_list.tag_mapping(tag)

s = schema.load_schema(url, resolver=r, resolve_references=True)
assert tag in repr(s)
Expand Down
2 changes: 1 addition & 1 deletion asdf/tests/test_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from asdf.extension import default_extensions
from asdf.extension._legacy import default_extensions
from asdf.schema import load_schema
from asdf.versioning import (
AsdfSpec,
Expand Down
2 changes: 0 additions & 2 deletions pytest_asdf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,10 @@ def from_parent(cls, parent, schema_path, validate_default=True, **kwargs):

def runtest(self):
from asdf import schema
from asdf.extension import default_extensions

# Make sure that each schema itself is valid.
schema_tree = schema.load_schema(
self.schema_path,
resolver=default_extensions.resolver,
resolve_references=True,
)
schema.check_schema(schema_tree, validate_default=self.validate_default)
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ commands_pre =
pip freeze
commands=
pytest astropy/astropy/io/misc/asdf --open-files --run-slow --remote-data \
-W "ignore::asdf.exceptions.AsdfDeprecationWarning:asdf.types"
-W "ignore::asdf.exceptions.AsdfDeprecationWarning:asdf.types" \
-W "ignore::asdf.exceptions.AsdfDeprecationWarning:asdf.extension"

[testenv:asdf-astropy]
change_dir = {env_tmp_dir}
Expand Down

0 comments on commit b94cfae

Please sign in to comment.