2323 from pydantic_core import to_jsonable_python
2424except ImportError :
2525 # pydantic v1
26- from pydantic .json import pydantic_encoder as to_jsonable_python
26+ from pydantic .json import pydantic_encoder as to_jsonable_python # type: ignore
2727
2828import temporalio .workflow
2929from temporalio .converter import (
3636)
3737from temporalio .worker .workflow_sandbox ._restrictions import RestrictionContext
3838
39+ # Note that in addition to the implementation in this module, _RestrictedProxy
40+ # implements __get_pydantic_core_schema__ so that pydantic unwraps proxied types
41+ # when determining the schema.
42+
3943
4044class PydanticModelTypeConverter (JSONTypeConverter ):
45+ """Type converter for pydantic model instances."""
46+
4147 def to_typed_value (self , hint : Type , value : Any ) -> Any :
48+ """Convert dict value to pydantic model instance of the specified type"""
4249 if not inspect .isclass (hint ) or not issubclass (hint , pydantic .BaseModel ):
4350 return JSONTypeConverter .Unhandled
4451 model = hint
@@ -67,20 +74,29 @@ def to_typed_value(self, hint: Type, value: Any) -> Any:
6774
6875
6976class PydanticJSONEncoder (AdvancedJSONEncoder ):
77+ """JSON encoder for python objects containing pydantic model instances."""
78+
7079 def default (self , o : Any ) -> Any :
80+ """Convert object to jsonable python.
81+
82+ See :py:meth:`json.JSONEncoder.default`.
83+ """
7184 if isinstance (o , pydantic .BaseModel ):
7285 return to_jsonable_python (o )
7386 return super ().default (o )
7487
7588
7689class PydanticPayloadConverter (CompositePayloadConverter ):
77- """Pydantic payload converter.
90+ """Payload converter for payloads containing pydantic model instances .
7891
79- Payload converter that replaces the default JSON conversion with Pydantic
80- JSON conversion.
92+ JSON conversion is replaced with a converter that uses
93+ :py:class:`PydanticJSONEncoder` to convert the python object to JSON, and
94+ :py:class:`PydanticModelTypeConverter` to convert raw python values to
95+ pydantic model instances.
8196 """
8297
8398 def __init__ (self ) -> None :
99+ """Initialize object"""
84100 json_payload_converter = JSONPlainPayloadConverter (
85101 encoder = PydanticJSONEncoder ,
86102 custom_type_converters = [PydanticModelTypeConverter ()],
@@ -98,7 +114,7 @@ def __init__(self) -> None:
98114pydantic_data_converter = DataConverter (
99115 payload_converter_class = PydanticPayloadConverter
100116)
101- """Data converter for Pydantic models .
117+ """Data converter for payloads containing pydantic model instances .
102118
103- To use, pass this as the ``data_converter`` argument to :py:class:`temporalio.client.Client`
119+ To use, pass as the ``data_converter`` argument of :py:class:`temporalio.client.Client`
104120"""
0 commit comments