Skip to content

Commit 4e0120f

Browse files
authored
feat: smaller serialization for catalog items (#59)
* smaller serialization for catalog items * numpy2
1 parent de1fbe2 commit 4e0120f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ test_thirdparty = [
5454
"napari>=0.4.19",
5555
"plotly",
5656
"pydantic",
57-
"pydantic-extra-types",
57+
"pydantic-extra-types>=2",
5858
"pygfx",
5959
"pytest-qt",
6060
"rich",
6161
"viscm",
62-
"vispy",
62+
"vispy>=0.14",
6363
"pyqtgraph",
6464
]
6565
dev = [

src/cmap/_colormap.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,14 @@ def __get_pydantic_core_schema__(
663663
from pydantic_core import core_schema
664664

665665
schema = handler(Any)
666-
ser = core_schema.plain_serializer_function_ser_schema(lambda x: x.as_dict())
666+
667+
def _serialize(obj: Colormap) -> Any:
668+
if obj.info is not None and obj.info.qualified_name:
669+
# this is a catalog item
670+
return obj.info.qualified_name
671+
return obj.as_dict()
672+
673+
ser = core_schema.plain_serializer_function_ser_schema(_serialize)
667674
return core_schema.no_info_after_validator_function(
668675
cls._validate, schema, serialization=ser
669676
)

tests/test_model_fields.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
try:
1919
import pydantic_extra_types.color as pydantic_color
20-
except ImportError:
20+
except (ImportError, NotImplementedError):
2121
import pydantic.color as pydantic_color
2222

2323

@@ -60,6 +60,11 @@ class Config:
6060
else:
6161
assert MyModel.parse_raw(serialized) == obj
6262

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

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

0 commit comments

Comments
 (0)