From 3bd825c4ac35137a8344f200a0262f7e27145cca Mon Sep 17 00:00:00 2001 From: Ed Slavich Date: Wed, 2 Feb 2022 15:48:59 -0500 Subject: [PATCH] Restore TagDefinition.schema_uri property but add deprecation warning --- CHANGES.rst | 2 +- asdf/extension/_tag.py | 33 +++++++++++++++++++++++++++++++-- asdf/tests/test_extension.py | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 0d5787349..b76e5ccfc 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -16,7 +16,7 @@ not been run. [#1057] - Add ability for tags to correspond to multiple schema_uri, with an - implied allOf among the schema_uris. [#1058] + implied allOf among the schema_uris. [#1058, #1069] - Add the URI of the file being parsed to ``SerializationContext``. [#1065] diff --git a/asdf/extension/_tag.py b/asdf/extension/_tag.py index 0a45df5c5..f9ce53c1e 100644 --- a/asdf/extension/_tag.py +++ b/asdf/extension/_tag.py @@ -1,3 +1,8 @@ +import warnings + +from asdf.exceptions import AsdfDeprecationWarning + + class TagDefinition: """ Container for properties of a custom YAML tag. @@ -43,15 +48,39 @@ def tag_uri(self): return self._tag_uri @property - def schema_uris(self): + def schema_uri(self): """ + DEPRECATED + Get the URI of the schema that should be used to validate - objects wtih this tag. + objects with this tag. Returns ------- str or None """ + warnings.warn( + "The TagDefinition.schema_uri property is deprecated. Use TagDefinition.schema_uris instead.", + AsdfDeprecationWarning + ) + + if len(self._schema_uris) == 0: + return None + elif len(self._schema_uris) == 1: + return self._schema_uris[0] + else: + raise RuntimeError("Cannot use TagDefinition.schema_uri when multiple schema URIs are present") + + @property + def schema_uris(self): + """ + Get the URIs of the schemas that should be used to validate + objects with this tag. + + Returns + ------- + list + """ return self._schema_uris @property diff --git a/asdf/tests/test_extension.py b/asdf/tests/test_extension.py index 0806233fd..692a6e575 100644 --- a/asdf/tests/test_extension.py +++ b/asdf/tests/test_extension.py @@ -17,6 +17,7 @@ ) from asdf import config_context +from asdf.exceptions import AsdfDeprecationWarning from asdf.types import CustomType from asdf.tests.helpers import assert_extension_correctness @@ -437,6 +438,21 @@ def test_tag_definition(): assert "URI: asdf://somewhere.org/extensions/foo/tags/foo-1.0" in repr(tag_def) + with pytest.warns(DeprecationWarning): + assert tag_def.schema_uri == "asdf://somewhere.org/extensions/foo/schemas/foo-1.0" + + tag_def = TagDefinition( + "asdf://somewhere.org/extensions/foo/tags/foo-1.0", + schema_uris=["asdf://somewhere.org/extensions/foo/schemas/foo-1.0", "asdf://somewhere.org/extensions/foo/schemas/base-1.0"], + title="Some title", + description="Some description", + ) + + assert tag_def.schema_uris == ["asdf://somewhere.org/extensions/foo/schemas/foo-1.0", "asdf://somewhere.org/extensions/foo/schemas/base-1.0"] + with pytest.warns(DeprecationWarning): + with pytest.raises(RuntimeError): + tag_def.schema_uri + with pytest.raises(ValueError): TagDefinition("asdf://somewhere.org/extensions/foo/tags/foo-*")