Skip to content

Commit

Permalink
Merge pull request #1464 from braingram/remove_legacy_extension_api
Browse files Browse the repository at this point in the history
Remove legacy extension api
  • Loading branch information
braingram authored May 8, 2023
2 parents 0e3b3e4 + 19dc360 commit 587542e
Show file tree
Hide file tree
Showing 36 changed files with 176 additions and 1,496 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The ASDF Standard is at v1.6.0
``all_array_compression_kwargs`` to ``asdf.config.AsdfConfig`` [#1468]
- Move built-in tags to converters (except ndarray and integer). [#1474]
- Add block storage support to Converter [#1508]
- Remove deprecated legacy extension API [#1464]

2.15.0 (2023-03-28)
-------------------
Expand All @@ -27,7 +28,7 @@ The ASDF Standard is at v1.6.0
ones that we can successfully build and test against. [#1360]
- Provide more informative filename when failing to open a file [#1357]
- Add new plugin type for custom schema validators. [#1328]
- Add AsdfDeprecationWarning to `~asdf.types.CustomType` [#1359]
- Add AsdfDeprecationWarning to ``asdf.types.CustomType`` [#1359]
- Throw more useful error when provided with a path containing an
extra leading slash [#1356]
- Add AsdfDeprecationWarning to AsdfInFits. Support for reading and
Expand Down
14 changes: 0 additions & 14 deletions asdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

__all__ = [
"AsdfFile",
"CustomType",
"AsdfExtension",
"Stream",
"open",
"IntegerType",
Expand All @@ -22,22 +20,10 @@
from jsonschema import ValidationError

from ._convenience import info
from ._types import CustomType
from ._version import version as __version__
from .asdf import AsdfFile
from .asdf import open_asdf as open
from .config import config_context, get_config
from .stream import Stream
from .tags.core import IntegerType
from .tags.core.external_reference import ExternalArrayReference


def __getattr__(name):
if name == "AsdfExtension":
# defer import to only issue deprecation warning when
# asdf.AsdfExtension is used
from asdf import extension

return extension.AsdfExtension
msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)
2 changes: 1 addition & 1 deletion asdf/_tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def assert_extension_correctness(extension):
Parameters
----------
extension : asdf.AsdfExtension
extension : asdf._AsdfExtension
The extension to validate
"""
__tracebackhide__ = True
Expand Down
6 changes: 2 additions & 4 deletions asdf/_tests/commands/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from asdf import AsdfFile
from asdf.commands import list_tags
from asdf.exceptions import AsdfDeprecationWarning


@pytest.mark.parametrize("display_classes", [True, False])
Expand All @@ -20,8 +19,7 @@ def test_all_tags_present():
tags = {line.strip() for line in iostream.readlines()}

af = AsdfFile()
with pytest.warns(AsdfDeprecationWarning, match="AsdfFile.type_index is deprecated"):
for tag in af.type_index._type_by_tag:
assert tag in tags
for tag in af._type_index._type_by_tag:
assert tag in tags
for tag in af.extension_manager._converters_by_tag:
assert tag in tags
3 changes: 2 additions & 1 deletion asdf/_tests/objects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from asdf import CustomType, util
from asdf import util
from asdf._types import CustomType
from asdf.exceptions import AsdfDeprecationWarning

from ._helpers import get_test_data_path
Expand Down
4 changes: 1 addition & 3 deletions asdf/_tests/tags/core/tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from asdf import util
from asdf._tests import _helpers as helpers
from asdf._tests.objects import CustomTestType
from asdf.exceptions import AsdfDeprecationWarning
from asdf.tags.core import ndarray

from . import data as test_data
Expand Down Expand Up @@ -131,8 +130,7 @@ def test_dont_load_data():

buff.seek(0)
with asdf.open(buff) as ff:
with pytest.warns(AsdfDeprecationWarning, match="AsdfFile.run_hook is deprecated"):
ff.run_hook("reserve_blocks")
ff._run_hook("reserve_blocks")

# repr and str shouldn't load data
str(ff.tree["science_data"])
Expand Down
17 changes: 3 additions & 14 deletions asdf/_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
from numpy.testing import assert_array_equal

import asdf
import asdf.extension._legacy as _legacy_extension
from asdf import _resolver as resolver
from asdf import config_context, extension, get_config, schema, treeutil, versioning
from asdf import config_context, get_config, treeutil, versioning
from asdf.exceptions import AsdfDeprecationWarning, AsdfWarning
from asdf.extension import ExtensionProxy

Expand Down Expand Up @@ -256,16 +257,6 @@ def test_copy(tmp_path):
assert_array_equal(ff2.tree["my_array"], ff2.tree["my_array"])


def test_tag_to_schema_resolver_deprecation():
ff = asdf.AsdfFile()
with pytest.warns(AsdfDeprecationWarning):
ff.tag_to_schema_resolver("foo")

with pytest.warns(AsdfDeprecationWarning):
extension_list = extension.default_extensions.extension_list
extension_list.tag_to_schema_resolver("foo")


def test_access_tree_outside_handler(tmp_path):
tempname = str(tmp_path / "test.asdf")

Expand Down Expand Up @@ -472,15 +463,13 @@ def test_resolver_deprecations():
resolver.default_resolver,
resolver.default_tag_to_url_mapping,
resolver.default_url_mapping,
schema.default_ext_resolver,
]:
with pytest.warns(AsdfDeprecationWarning):
resolver_method("foo")


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

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

Expand Down
12 changes: 3 additions & 9 deletions asdf/_tests/test_asdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ def test_asdf_file_extensions():
af.extensions = arg
assert af.extensions == [ExtensionProxy(extension)]

msg = (
r"[The extensions parameter must be an extension.*, "
r"Extension must implement the Extension or AsdfExtension interface]"
)
msg = r"[The extensions parameter must be an extension.*, Extension must implement the Extension interface]"
for arg in (object(), [object()]):
with pytest.raises(TypeError, match=msg):
AsdfFile(extensions=arg)
Expand Down Expand Up @@ -185,10 +182,7 @@ def test_open_asdf_extensions(tmp_path):
with open_asdf(path, extensions=arg) as af:
assert af.extensions == [ExtensionProxy(extension)]

msg = (
r"[The extensions parameter must be an extension.*, "
r"Extension must implement the Extension or AsdfExtension interface]"
)
msg = r"[The extensions parameter must be an extension.*, Extension must implement the Extension interface]"
for arg in (object(), [object()]):
with pytest.raises(TypeError, match=msg), open_asdf(path, extensions=arg) as af:
pass
Expand All @@ -211,7 +205,7 @@ def test_serialization_context():

assert context.url == context._url == "file://test.asdf"

with pytest.raises(TypeError, match=r"Extension must implement the Extension or AsdfExtension interface"):
with pytest.raises(TypeError, match=r"Extension must implement the Extension interface"):
context._mark_extension_used(object())

with pytest.raises(ValueError, match=r"ASDF Standard version .* is not supported by asdf==.*"):
Expand Down
96 changes: 0 additions & 96 deletions asdf/_tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import asdf._types
import asdf.extension
import asdf.testing.helpers
from asdf import entry_points
from asdf._tests._helpers import assert_extension_correctness
from asdf._tests.objects import CustomExtension
from asdf._types import CustomType
Expand All @@ -22,113 +21,18 @@ class NewCustomType(CustomType):
pass


def test_resolver_module_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="^asdf.resolver is deprecated.*$"):
# importlib.reload doesn't appear to work here likely because of the
# sys.module and __file__ changes in asdf.resolver
if "asdf.resolver" in sys.modules:
del sys.modules["asdf.resolver"]
import asdf.resolver
# resolver does not define an __all__ so we will define one here
# for testing purposes
resolver_all = [
"Resolver",
"ResolverChain",
"DEFAULT_URL_MAPPING",
"DEFAULT_TAG_TO_URL_MAPPING",
"default_url_mapping",
"default_tag_to_url_mapping",
"default_resolver",
]
for attr in dir(asdf.resolver):
if attr not in resolver_all:
continue
with pytest.warns(AsdfDeprecationWarning, match="^asdf.resolver is deprecated.*$"):
getattr(asdf.resolver, attr)


def test_assert_extension_correctness_deprecation():
extension = CustomExtension()
with pytest.warns(AsdfDeprecationWarning, match="assert_extension_correctness is deprecated.*"):
assert_extension_correctness(extension)


def test_type_index_module_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="^asdf.type_index is deprecated.*$"):
# importlib.reload doesn't appear to work here likely because of the
# sys.module and __file__ changes in asdf.type_index
if "asdf.type_index" in sys.modules:
del sys.modules["asdf.type_index"]
import asdf.type_index
for attr in asdf.type_index.__all__:
with pytest.warns(AsdfDeprecationWarning, match="^asdf.type_index is deprecated.*$"):
getattr(asdf.type_index, attr)


@pytest.mark.parametrize("attr", ["url_mapping", "tag_mapping", "resolver", "extension_list", "type_index"])
def test_asdffile_legacy_extension_api_attr_deprecations(attr):
with asdf.AsdfFile() as af, pytest.warns(AsdfDeprecationWarning, match=f"AsdfFile.{attr} is deprecated"):
getattr(af, attr)


def test_asdfile_run_hook_deprecation():
with asdf.AsdfFile() as af, pytest.warns(AsdfDeprecationWarning, match="AsdfFile.run_hook is deprecated"):
af.run_hook("foo")


def test_asdfile_run_modifying_hook_deprecation():
with asdf.AsdfFile() as af, pytest.warns(AsdfDeprecationWarning, match="AsdfFile.run_modifying_hook is deprecated"):
af.run_modifying_hook("foo")


def test_types_module_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="^asdf.types is deprecated.*$"):
if "asdf.types" in sys.modules:
del sys.modules["asdf.types"]
import asdf.types
for attr in asdf.types.__all__:
with pytest.warns(AsdfDeprecationWarning, match="^asdf.types is deprecated.*$"):
getattr(asdf.types, attr)


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([])


def test_asdf_type_format_tag():
with pytest.warns(AsdfDeprecationWarning, match="asdf.types.format_tag is deprecated"):
asdf._types.format_tag
asdf.testing.helpers.format_tag


@pytest.mark.parametrize("name", ["AsdfExtension", "AsdfExtensionList", "BuiltinExtension"])
def test_extension_class_deprecation(name):
with pytest.warns(AsdfDeprecationWarning, match=f"{name} is deprecated"):
getattr(asdf.extension, name)


def test_top_level_asdf_extension_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="AsdfExtension is deprecated"):
asdf.AsdfExtension


def test_deprecated_entry_point(mock_entry_points): # noqa: F811
mock_entry_points.append(("asdf_extensions", "legacy", "asdf.tests.test_entry_points:LegacyExtension"))
with pytest.warns(AsdfDeprecationWarning, match=".* uses the deprecated entry point asdf_extensions"):
entry_points.get_extensions()


def test_asdf_tests_helpers_deprecation():
with pytest.warns(AsdfDeprecationWarning, match="asdf.tests.helpers is deprecated"):
if "asdf.tests.helpers" in sys.modules:
Expand Down
16 changes: 5 additions & 11 deletions asdf/_tests/test_entry_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from asdf import entry_points
from asdf._version import version as asdf_package_version
from asdf.exceptions import AsdfDeprecationWarning, AsdfWarning
from asdf.exceptions import AsdfWarning
from asdf.extension import ExtensionProxy
from asdf.resource import ResourceMappingProxy

Expand Down Expand Up @@ -151,24 +151,18 @@ def test_get_extensions(mock_entry_points):
)
with pytest.warns(
AsdfWarning,
match=r"TypeError: Extension must implement the Extension or AsdfExtension interface",
match=r"TypeError: Extension must implement the Extension interface",
):
extensions = entry_points.get_extensions()
assert len(extensions) == 2

mock_entry_points.clear()
mock_entry_points.append(("asdf_extensions", "legacy", "asdf._tests.test_entry_points:LegacyExtension"))
with pytest.warns(AsdfDeprecationWarning, match=".* uses the deprecated entry point asdf_extensions"):
extensions = entry_points.get_extensions()
assert len(extensions) == 1
for e in extensions:
assert isinstance(e, ExtensionProxy)
assert e.package_name == "asdf"
assert e.package_version == asdf_package_version
assert e.legacy is True
extensions = entry_points.get_extensions()
assert len(extensions) == 0 # asdf_extensions is no longer supported

mock_entry_points.clear()
mock_entry_points.append(("asdf_extensions", "failing", "asdf._tests.test_entry_points:FauxLegacyExtension"))
mock_entry_points.append(("asdf.extensions", "failing", "asdf._tests.test_entry_points:FauxLegacyExtension"))
with pytest.warns(AsdfWarning, match=r"TypeError"):
extensions = entry_points.get_extensions()
assert len(extensions) == 0
18 changes: 7 additions & 11 deletions asdf/_tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
ManifestExtension,
TagDefinition,
Validator,
get_cached_asdf_extension_list,
get_cached_extension_manager,
)
from asdf.extension._legacy import AsdfExtension, BuiltinExtension
from asdf.extension._legacy import BuiltinExtension, _AsdfExtension, get_cached_asdf_extension_list


def test_builtin_extension():
Expand Down Expand Up @@ -163,7 +162,7 @@ def test_extension_proxy_maybe_wrap():
assert proxy.delegate is extension
assert ExtensionProxy.maybe_wrap(proxy) is proxy

with pytest.raises(TypeError, match=r"Extension must implement the Extension or AsdfExtension interface"):
with pytest.raises(TypeError, match=r"Extension must implement the Extension interface"):
ExtensionProxy.maybe_wrap(object())


Expand All @@ -173,7 +172,7 @@ def test_extension_proxy():
proxy = ExtensionProxy(extension)

assert isinstance(proxy, Extension)
assert isinstance(proxy, AsdfExtension)
assert isinstance(proxy, _AsdfExtension)

assert proxy.extension_uri == "asdf://somewhere.org/extensions/minimum-1.0"
assert proxy.legacy_class_names == set()
Expand Down Expand Up @@ -242,7 +241,7 @@ def test_extension_proxy():
assert proxy.class_name == "asdf._tests.test_extension.FullExtension"

# Should fail when the input is not one of the two extension interfaces:
with pytest.raises(TypeError, match=r"Extension must implement the Extension or AsdfExtension interface"):
with pytest.raises(TypeError, match=r"Extension must implement the Extension interface"):
ExtensionProxy(object)

# Should fail with a bad converter:
Expand Down Expand Up @@ -606,12 +605,9 @@ def test_converter_proxy():

def test_get_cached_asdf_extension_list():
extension = LegacyExtension()
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
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


def test_manifest_extension():
Expand Down
Loading

0 comments on commit 587542e

Please sign in to comment.