Skip to content

Commit

Permalink
add fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Dec 16, 2023
1 parent 7c9b890 commit c6ea7f9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/ome_types/model/_shape_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pydantic.version
from pydantic import Field, ValidationError, validator

# for circular import reasons...
from ome_types._autogenerated.ome_2016_06.ellipse import Ellipse
from ome_types._autogenerated.ome_2016_06.label import Label
from ome_types._autogenerated.ome_2016_06.line import Line
Expand Down Expand Up @@ -55,8 +56,8 @@ class ShapeUnion(OMEType, RootModel, UserSequence[ShapeType]): # type: ignore[m
default_factory=list,
json_schema_extra={
"type": "Elements",
"choices": tuple(
(("name", kind.title()), ("type", cls))
"choices": tuple( # type: ignore
(("name", kind.title()), ("type", cls.__name__))
for kind, cls in _KINDS.items()
),
},
Expand Down Expand Up @@ -121,7 +122,7 @@ class ShapeUnion(OMEType, UserSequence[ShapeType]): # type: ignore
metadata={ # type: ignore[call-arg]
"type": "Elements",
"choices": tuple(
(("name", kind.title()), ("type", cls))
(("name", kind.title()), ("type", cls.__name__))
for kind, cls in _KINDS.items()
),
},
Expand Down
9 changes: 6 additions & 3 deletions src/ome_types/model/_structured_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pydantic.version
from pydantic import Field, ValidationError, validator

# for circular import reasons...
from ome_types._autogenerated.ome_2016_06.annotation import Annotation
from ome_types._autogenerated.ome_2016_06.boolean_annotation import BooleanAnnotation
from ome_types._autogenerated.ome_2016_06.comment_annotation import CommentAnnotation
Expand Down Expand Up @@ -64,8 +65,9 @@ class StructuredAnnotationList(OMEType, RootModel, UserSequence[Annotation]): #
default_factory=list,
json_schema_extra={
"type": "Elements",
"choices": tuple(
(("name", cls.__name__), ("type", cls)) for cls in AnnotationTypes
"choices": tuple( # type: ignore
(("name", cls.__name__), ("type", cls.__name__))
for cls in AnnotationTypes
),
},
)
Expand Down Expand Up @@ -127,7 +129,8 @@ class StructuredAnnotationList(OMEType, UserSequence[Annotation]): # type: igno
metadata={ # type: ignore[call-arg]
"type": "Elements",
"choices": tuple(
(("name", cls.__name__), ("type", cls)) for cls in AnnotationTypes
(("name", cls.__name__), ("type", cls.__name__))
for cls in AnnotationTypes
),
},
)
Expand Down
20 changes: 19 additions & 1 deletion src/xsdata_pydantic_basemodel/pydantic_compat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from dataclasses import MISSING, field
from typing import TYPE_CHECKING, Any, Callable, Iterator, TypeVar

Expand Down Expand Up @@ -65,7 +66,24 @@ def _pydantic_field_to_dataclass_field(name: str, pydantic_field: FieldInfo) ->
# Then, in our source code, we declare choices as tuple[tuple[str, str], ...]
# which IS hashable.
if "choices" in metadata:
metadata["choices"] = [dict(choice) for choice in metadata["choices"]]
choices = []
for choice in metadata["choices"]:
choice = dict(choice)
# we also, unfortunately, need to convert the "type" field from a
# class name to a class object
if "type" in choice and isinstance(choice["type"], str):
try:
from ome_types import model

choice["type"] = getattr(model, choice["type"])
except AttributeError:
warnings.warn(
f"Could not find {choice['type']} in ome_types.model",
stacklevel=2,
)

choices.append(choice)
metadata["choices"] = choices

dataclass_field = field( # type: ignore
default=default, default_factory=default_factory, metadata=metadata
Expand Down
2 changes: 1 addition & 1 deletion tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ def test_canonicalize() -> None:
def test_export_schema() -> None:
schema = OME.model_json_schema()
assert isinstance(schema, dict)
assert isinstance(str, json.dumps(schema))
assert isinstance(json.dumps(schema), str)

0 comments on commit c6ea7f9

Please sign in to comment.