-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
492 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,9 @@ venv.bak/ | |
# Rope project settings | ||
.ropeproject | ||
|
||
# ruff | ||
.ruff_cache/ | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Optional | ||
|
||
from pydantic.json_schema import GenerateJsonSchema | ||
|
||
DEFAULT_ASDF_SCHEMA_REF_TEMPLATE = "#/definitions/{model}" | ||
DESIRED_ASDF_SCHEMA_KEY_ORDER = ( | ||
"$schema", | ||
"id", | ||
"title", | ||
"type", | ||
"properties", | ||
"allOf", | ||
"anyOf", | ||
"required", | ||
"definitions", | ||
) | ||
|
||
|
||
class GenerateAsdfSchema(GenerateJsonSchema): | ||
"""Generates ASDF-compatible schema from Pydantic's default JSON schema generator. | ||
```{caution} Experimental | ||
This schema generator is not complete. It currently creates JSON 2020-12 | ||
schema (despite `$schema` says it's `asdf-schema-1.0.0`) which are not | ||
compatible with ASDF. | ||
``` | ||
""" | ||
|
||
# HACK: When we can support tree models, then not all schema should have tag | ||
schema_dialect = "http://stsci.edu/schemas/asdf/asdf-schema-1.0.0" | ||
|
||
def __init__( | ||
self, | ||
by_alias: bool = True, | ||
ref_template: str = DEFAULT_ASDF_SCHEMA_REF_TEMPLATE, | ||
tag_uri: Optional[str] = None, | ||
): | ||
super().__init__(by_alias=by_alias, ref_template=ref_template) | ||
self.tag_uri = tag_uri | ||
|
||
def generate(self, schema, mode="validation"): | ||
json_schema = super().generate(schema, mode) # noqa: F841 | ||
|
||
if self.tag_uri: | ||
json_schema["$schema"] = self.schema_dialect | ||
json_schema["id"] = f"{self.tag_uri}/schema" | ||
|
||
# TODO: Convert jsonschema 2020-12 to ASDF schema | ||
if "$defs" in json_schema: | ||
json_schema["definitions"] = json_schema.pop("$defs") | ||
|
||
# Order keys | ||
json_schema = { | ||
**{ | ||
key: json_schema[key] | ||
for key in DESIRED_ASDF_SCHEMA_KEY_ORDER | ||
if key in json_schema | ||
}, | ||
**json_schema, # Rest of the keys not in order list | ||
} | ||
|
||
return json_schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
from __future__ import annotations | ||
|
||
from asdf_pydantic.examples.tree import AsdfNode, Node | ||
from asdf_pydantic.examples.tree import AsdfTreeNode, Node | ||
|
||
|
||
def test_sanity(): | ||
AsdfNode().asdf_yaml_tree() == {"child": None} | ||
AsdfTreeNode().asdf_yaml_tree() == {"child": None} | ||
|
||
|
||
def test_should_not_convert_given_child_is_AsdfNode(): | ||
AsdfNode(child=AsdfNode()).asdf_yaml_tree() == {"child": AsdfNode()} | ||
AsdfTreeNode(child=AsdfTreeNode()).asdf_yaml_tree() == {"child": AsdfTreeNode()} | ||
|
||
|
||
def test_should_convert_given_child_is_Node(): | ||
AsdfNode(child=Node()).asdf_yaml_tree() == {"child": {"child": None}} | ||
AsdfTreeNode(child=Node()).asdf_yaml_tree() == {"child": {"child": None}} | ||
|
||
|
||
def test_given_mix_child_is_mix_of_AsdfNode_and_Node(): | ||
assert AsdfNode(child=AsdfNode(child=Node())).asdf_yaml_tree() == { | ||
"child": AsdfNode(child=Node()) | ||
assert AsdfTreeNode(child=AsdfTreeNode(child=Node())).asdf_yaml_tree() == { | ||
"child": AsdfTreeNode(child=Node()) | ||
} | ||
|
||
assert AsdfNode(child=Node(child=AsdfNode())).asdf_yaml_tree() == { | ||
"child": {"child": AsdfNode()} | ||
assert AsdfTreeNode(child=Node(child=AsdfTreeNode())).asdf_yaml_tree() == { | ||
"child": {"child": {"child": None}} | ||
} |
Empty file.
Oops, something went wrong.