Skip to content

Commit

Permalink
refactor: use AsdfPydanticConverter.add_models instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
ketozhang committed Nov 17, 2024
1 parent eb4f83b commit 7df4f90
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ from asdf.extension import Extension
from asdf_pydantic.converter import AsdfPydanticConverter
from mypackage.shapes import Rectangle

AsdfPydanticConverter.add_models(Rectangle)
converter = AsdfPydanticConverter()
converter.add_models(Rectangle)

class ShapesExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/shapes-1.0.0"
converters = [AsdfPydanticConverter()]
tags = [*AsdfPydanticConverter().tags]
converters = [converter]
tags = [*converter.tags]
```

Install the extension either by entry point specification or add it to
Expand Down
7 changes: 4 additions & 3 deletions tests/examples/test_astropy_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class Database(AsdfPydanticModel):
@pytest.fixture()
def asdf_extension():
"""Registers an ASDF extension containing models for this test."""
AsdfPydanticConverter.add_models(Database)
converter = AsdfPydanticConverter()
converter.add_models(Database)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0"

converters = [AsdfPydanticConverter()] # type: ignore
tags = [*AsdfPydanticConverter().tags] # type: ignore
converters = [converter] # type: ignore
tags = [*converter.tags] # type: ignore

asdf.get_config().add_extension(TestExtension())

Expand Down
8 changes: 3 additions & 5 deletions tests/examples/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import textwrap
from typing import Annotated
from unittest.mock import MagicMock, patch

import asdf
Expand All @@ -15,8 +14,6 @@

from asdf_pydantic import AsdfPydanticConverter, AsdfPydanticModel

SelfAnnotatedAsdfNode = Annotated[AsdfTag]


class AsdfNode(AsdfPydanticModel):
"""Model for a node in a graph/tree.
Expand Down Expand Up @@ -57,12 +54,13 @@ class AsdfNode(AsdfPydanticModel):
@pytest.fixture()
def asdf_extension():
"""Registers an ASDF extension containing models for this test."""
AsdfPydanticConverter.add_models(AsdfNode)
converter = AsdfPydanticConverter()
converter.add_models(AsdfNode)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0"

converters = [AsdfPydanticConverter()] # type: ignore
converters = [converter] # type: ignore
tags = [AsdfNode.get_tag_definition()] # type: ignore

with asdf.config_context() as asdf_config:
Expand Down
5 changes: 3 additions & 2 deletions tests/examples/test_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
@pytest.fixture()
def asdf_extension():
"""Registers an ASDF extension containing models for this test."""
AsdfPydanticConverter.add_models(AsdfRectangle)
converter = AsdfPydanticConverter()
converter.add_models(AsdfRectangle)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0"

converters = [AsdfPydanticConverter()] # type: ignore
converters = [converter] # type: ignore
tags = [AsdfRectangle.get_tag_definition()] # type: ignore

with asdf.config_context() as asdf_config:
Expand Down
7 changes: 4 additions & 3 deletions tests/patterns/astropy_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ class DataContainer(AsdfPydanticModel):


def setup_module():
AsdfPydanticConverter.add_models(DataPoint, DataContainer)
converter = AsdfPydanticConverter()
converter.add_models(DataPoint, DataContainer)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0"

converters = [AsdfPydanticConverter()] # type: ignore
tags = [*AsdfPydanticConverter().tags] # type: ignore
converters = [converter] # type: ignore
tags = [*converter.tags] # type: ignore

asdf.get_config().add_extension(TestExtension())

Expand Down
7 changes: 4 additions & 3 deletions tests/patterns/union_type_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class UnionObject(AsdfPydanticModel):


def setup_module():
AsdfPydanticConverter.add_models(UnionObject)
converter = AsdfPydanticConverter()
converter.add_models(UnionObject)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0"

converters = [AsdfPydanticConverter()] # type: ignore
tags = [*AsdfPydanticConverter().tags] # type: ignore
converters = [converter] # type: ignore
tags = [*converter.tags] # type: ignore

asdf.get_config().add_extension(TestExtension())

Expand Down
7 changes: 4 additions & 3 deletions tests/schema_validation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@


def setup_module():
AsdfPydanticConverter.add_models(AsdfRectangle)
converter = AsdfPydanticConverter()
converter.add_models(AsdfRectangle)

class TestExtension(Extension):
extension_uri = "asdf://asdf-pydantic/examples/extensions/test-1.0.0" # type: ignore

tags = [*AsdfPydanticConverter().tags] # type: ignore
converters = [AsdfPydanticConverter()] # type: ignore
tags = [*converter.tags] # type: ignore
converters = [converter] # type: ignore

# HACK: The schema URI should be referenced from `AsdfRectangle._schema`.
# Then there should be a way to automatically add the schema to ASDF
Expand Down

0 comments on commit 7df4f90

Please sign in to comment.