Skip to content

Commit

Permalink
feat: smaller serialization for catalog items (#59)
Browse files Browse the repository at this point in the history
* smaller serialization for catalog items

* numpy2
  • Loading branch information
tlambert03 authored Jun 20, 2024
1 parent de1fbe2 commit 4e0120f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ test_thirdparty = [
"napari>=0.4.19",
"plotly",
"pydantic",
"pydantic-extra-types",
"pydantic-extra-types>=2",
"pygfx",
"pytest-qt",
"rich",
"viscm",
"vispy",
"vispy>=0.14",
"pyqtgraph",
]
dev = [
Expand Down
9 changes: 8 additions & 1 deletion src/cmap/_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,14 @@ def __get_pydantic_core_schema__(
from pydantic_core import core_schema

schema = handler(Any)
ser = core_schema.plain_serializer_function_ser_schema(lambda x: x.as_dict())

def _serialize(obj: Colormap) -> Any:
if obj.info is not None and obj.info.qualified_name:
# this is a catalog item
return obj.info.qualified_name
return obj.as_dict()

ser = core_schema.plain_serializer_function_ser_schema(_serialize)
return core_schema.no_info_after_validator_function(
cls._validate, schema, serialization=ser
)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_model_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

try:
import pydantic_extra_types.color as pydantic_color
except ImportError:
except (ImportError, NotImplementedError):
import pydantic.color as pydantic_color


Expand Down Expand Up @@ -60,6 +60,11 @@ class Config:
else:
assert MyModel.parse_raw(serialized) == obj

# with category colormaps, we only use the qualified name
obj2 = MyModel(color="red", colormap=Colormap("viridis"))
serialized2 = obj2.json()
assert '"colormap":"bids:viridis"' in serialized2


def test_psygnal_serialization() -> None:
# support for _json_encode_ is built into psygnal, ... don't need json_encoders
Expand Down

0 comments on commit 4e0120f

Please sign in to comment.