Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ketozhang committed Jun 17, 2024
2 parents 6a09af4 + bdd09c0 commit 134bc77
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release package

on:
release:
types:
- created
workflow_dispatch:

jobs:
publish:
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/p/<your-pypi-project-name>
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
python -m pip install hatch
hatch env create
- name: Build
run: hatch build
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
16 changes: 8 additions & 8 deletions docs/model.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
```{currentmodule} asdf_pydantic
```
# ASDF Pydantic Model

The {py:class}`AsdfPydanticModel` is a data model
[`pydantic.BaseModel`](https://docs.pydantic.dev/usage/models/) that is readily
serializable to ASDF.
# Writing ASDF Pydantic models to define tagged objects

The {py:class}`AsdfPydanticModel` is a class that combines the feature of
[`pydantic.BaseModel`](https://docs.pydantic.dev/usage/models/) and ASDF
to be readily serializable an tagged object in ASDF.

```py
class Rectangle(AsdfPydanticModel):
Expand All @@ -15,10 +14,11 @@ class Rectangle(AsdfPydanticModel):
height: float
```

`_tag` is a globally unique **ASDF tag** (see [naming best
This `Rectangle` model will be referenced in ASDF's YAML file as `!rectangle-1.0.0`
specified by the `_tag` field. `_tag` is a globally unique ASDF tag (see [naming best
practices](https://asdf.readthedocs.io/en/stable/asdf/extending/uris.html#tags))
that identifies the data model. This model contains two **fields**: `width` and
`height` and their **field type** are both `float`.
that identifies the ASDF Pydantic model. This model contains two fields: `width` and
`height` and their field type are both `float`.

## Field Types

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"asdf>=3",
"asdf>=2",
"pydantic>=2",
]
dynamic = ["version"]
Expand Down
28 changes: 16 additions & 12 deletions tests/schema_validation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@
import pydantic
import pytest
import yaml
from asdf.extension import TagDefinition
from asdf.extension import Extension
from asdf_pydantic import AsdfPydanticConverter

from asdf_pydantic.examples.extensions import ExampleExtension
from asdf_pydantic.examples.shapes import AsdfRectangle
from asdf_pydantic.examples.tree import AsdfNode


class TestExtension(ExampleExtension):
tags = [ # type: ignore
TagDefinition(
AsdfRectangle._tag,
schema_uris="asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0",
)
]
def setup_module():
AsdfPydanticConverter.add_models(AsdfRectangle)

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

def setup_module():
asdf.get_config().add_extension(TestExtension())
tags = [*AsdfPydanticConverter().tags] # type: ignore
converters = [AsdfPydanticConverter()] # 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
# resources perhaps during AsdfPydanticConverter.add_models(). Further
# abstracting can be done later, perhaps defining a
# AsdfPydanticExtension.
asdf.get_config().add_resource_mapping(
{
"asdf://asdf-pydantic/shapes/schemas/rectangle-1.0.0": (
AsdfRectangle.schema_asdf().encode("utf-8")
)
}
)
asdf.get_config().add_extension(TestExtension())


def test_schema_exists_and_valid():
Expand Down Expand Up @@ -116,7 +120,7 @@ def test_validate_fail_on_bad_yaml_file():
# HACK: It is better that the ASDF's schema validation fails before
# the pydantic's. However, it seems ASDF deserialize first then do their
# validation.
with pytest.raises((asdf.exceptions.ValidationError, pydantic.ValidationError)):
with pytest.raises((asdf.ValidationError, pydantic.ValidationError)):
asdf.open(tempfile.name)


Expand Down

0 comments on commit 134bc77

Please sign in to comment.