From fbe48f2cc083a43c8eb1be7772f5b49e73bbf926 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 22 Aug 2023 11:32:36 -0400 Subject: [PATCH] work around Converter virtual subclass support add test for warning or error when a converter supports multiple tags but does not implement select_tag --- asdf/_tests/test_extension.py | 11 ++++++++--- asdf/extension/_converter.py | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/asdf/_tests/test_extension.py b/asdf/_tests/test_extension.py index c45125d5a..5f5559712 100644 --- a/asdf/_tests/test_extension.py +++ b/asdf/_tests/test_extension.py @@ -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] @@ -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) diff --git a/asdf/extension/_converter.py b/asdf/extension/_converter.py index 857cb02c4..8116ea3d6 100644 --- a/asdf/extension/_converter.py +++ b/asdf/extension/_converter.py @@ -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