Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ to include examples, links to docs, or any other relevant information.

### Fixed

- Restore pickling of Pydantic data converters, preserving the type adapter cache
size limit while excluding cached adapters.
- Current workflow and activity payload converter accessors now return the configured converter
without SDK-internal transfer type conversion.

Expand Down
15 changes: 15 additions & 0 deletions temporalio/contrib/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class PydanticJSONPlainPayloadConverter(EncodingPayloadConverter):
types from the datetime module, sets, UUID, etc, and custom types composed
of any of these.

Pickling preserves the cache size limit but excludes cached type adapters.

See https://docs.pydantic.dev/latest/api/standard_library_types/
"""

Expand All @@ -77,6 +79,19 @@ def __init__(
TypeAdapter
)

def __getstate__(self) -> dict[str, Any]:
"""Pickle without the type adapter cache, whose wrapper is not picklable."""
state = self.__dict__.copy()
state["_type_adapter"] = self._type_adapter.cache_info().maxsize
return state

def __setstate__(self, state: dict[str, Any]) -> None:
"""Restore state with an empty type adapter cache."""
self.__dict__.update(state)
self._type_adapter = functools.lru_cache(maxsize=state["_type_adapter"])(
TypeAdapter
)

@property
def encoding(self) -> str:
"""See base class."""
Expand Down
37 changes: 37 additions & 0 deletions tests/contrib/pydantic/test_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import datetime
import os
import pathlib
import pickle
import typing
import uuid

import pydantic
import pytest
from pydantic import BaseModel

import temporalio.contrib.pydantic
from temporalio.client import Client
from temporalio.contrib.pydantic import (
PydanticJSONPlainPayloadConverter,
Expand Down Expand Up @@ -55,6 +57,41 @@
)


@pytest.mark.parametrize("populate_cache", [False, True])
def test_pydantic_json_converter_pickle(populate_cache: bool):
class Model(BaseModel):
value: int
default: int = 0

converter = PydanticJSONPlainPayloadConverter(
temporalio.contrib.pydantic.ToJsonOptions(exclude_unset=True),
max_cached_type_adapters=2,
)
value = Model(value=1)
payload = converter.to_payload(value)
assert payload is not None
if populate_cache:
assert converter.from_payload(payload, Model) == value
cache_info = converter._type_adapter.cache_info()

restored = pickle.loads(pickle.dumps(converter))

assert converter._type_adapter.cache_info() == cache_info
assert restored._type_adapter.cache_info().maxsize == 2
assert restored._type_adapter.cache_info().currsize == 0
assert restored.to_payload(value) == payload
assert payload.data == b'{"value":1}'
assert restored.from_payload(payload, Model) == value
assert restored.from_payload(payload, Model) == value
assert restored._type_adapter.cache_info().hits == 1


async def test_pydantic_data_converter_pickle():
restored = pickle.loads(pickle.dumps(pydantic_data_converter))
value = {"a": 1}
assert await restored.decode(await restored.encode([value]), [dict]) == [value]


@pytest.mark.parametrize(
(
"converter_kwargs",
Expand Down
Loading