Skip to content

Commit

Permalink
work around Converter virtual subclass support
Browse files Browse the repository at this point in the history
add test for warning or error when a converter
supports multiple tags but does not implement
select_tag
  • Loading branch information
braingram committed Sep 11, 2023
1 parent 00e88ec commit c8e45ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 8 additions & 3 deletions asdf/_tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,11 +913,14 @@ def from_yaml_tree(self, node, tag, ctx):
roundtrip_object(obj)


def test_warning_for_default_select_tag():
@pytest.mark.parametrize("is_subclass", [True, False])
def test_warning_or_error_for_default_select_tag(is_subclass):
class Foo:
pass

class FooConverter(Converter):
ParentClass = Converter if is_subclass else object

class FooConverter(ParentClass):
tags = ["asdf://somewhere.org/tags/foo-*"]
types = [Foo]

Expand All @@ -932,6 +935,8 @@ def from_yaml_tree(self, node, tag, ctx):
"asdf://somewhere.org/tags/foo-2.0.0",
]
extension = FullExtension(converters=[FooConverter()], tags=tags)
ctx_type = pytest.warns if is_subclass else pytest.raises
exception_class = AsdfWarning if is_subclass else RuntimeError
with config_context() as config:
with pytest.warns(AsdfWarning, match="Converter handles multiple tags"):
with ctx_type(exception_class, match="Converter handles multiple tags"):
config.add_extension(extension)
4 changes: 3 additions & 1 deletion asdf/extension/_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def __init__(self, delegate, extension):
raise TypeError(msg)

if len(relevant_tags) > 1 and not hasattr(delegate, "select_tag"):
if isinstance(delegate, Converter):
# we cannot use isinstance here because Converter supports
# virtual subclasses
if Converter in delegate.__class__.__bases__:
# prior to asdf 3.0 Converter provided a default select_tag
# to provide backwards compatibility allow Converter subclasses
# to be registered with >1 tag but produce a warning
Expand Down

0 comments on commit c8e45ad

Please sign in to comment.