Skip to content

Commit bc949b8

Browse files
committed
Warn pydantic users who are not using contrib.pydantic
1 parent 775e4ee commit bc949b8

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

temporalio/converter.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import sys
1111
import traceback
1212
import uuid
13+
import warnings
1314
from abc import ABC, abstractmethod
1415
from dataclasses import dataclass
1516
from datetime import datetime
@@ -486,8 +487,11 @@ def from_payload(
486487
class AdvancedJSONEncoder(json.JSONEncoder):
487488
"""Advanced JSON encoder.
488489
489-
This encoder supports dataclasses, classes with dict() functions, and
490-
all iterables as lists.
490+
This encoder supports dataclasses and all iterables as lists.
491+
492+
It also uses Pydantic's "model_dump" or "dict" methods if available on the
493+
object, but with a warning that the dedicated pydantic data converter should
494+
be used.
491495
"""
492496

493497
def default(self, o: Any) -> Any:
@@ -498,10 +502,16 @@ def default(self, o: Any) -> Any:
498502
# Dataclass support
499503
if dataclasses.is_dataclass(o):
500504
return dataclasses.asdict(o)
501-
# Support for models with "dict" function like Pydantic
502-
dict_fn = getattr(o, "dict", None)
503-
if callable(dict_fn):
504-
return dict_fn()
505+
506+
# Deprecated support for Pydantic model instances
507+
for pydantic_attr in ["model_dump", "dict"]:
508+
to_dict = getattr(o, pydantic_attr, None)
509+
if callable(to_dict):
510+
warnings.warn(
511+
"It looks like you're using pydantic. Please use temporalio.contrib.pydantic.converter.pydantic_data_converter."
512+
)
513+
return to_dict()
514+
505515
# Support for non-list iterables like set
506516
if not isinstance(o, list) and isinstance(o, collections.abc.Iterable):
507517
return list(o)
@@ -1523,15 +1533,19 @@ def value_to_type(
15231533
# the start of this function. We retain the following for backwards
15241534
# compatibility with pydantic users who are not using contrib.pydantic, but
15251535
# this is deprecated.
1526-
parse_obj_attr = inspect.getattr_static(hint, "parse_obj", None)
1527-
if isinstance(parse_obj_attr, classmethod) or isinstance(
1528-
parse_obj_attr, staticmethod
1529-
):
1530-
if not isinstance(value, dict):
1531-
raise TypeError(
1532-
f"Cannot convert to {hint}, value is {type(value)} not dict"
1536+
for pydantic_attr in ["model_validate", "parse_obj"]:
1537+
pydantic_method = inspect.getattr_static(hint, pydantic_attr, None)
1538+
if isinstance(pydantic_method, classmethod) or isinstance(
1539+
pydantic_method, staticmethod
1540+
):
1541+
if not isinstance(value, dict):
1542+
raise TypeError(
1543+
f"Cannot convert to {hint}, value is {type(value)} not dict"
1544+
)
1545+
warnings.warn(
1546+
"It looks like you're using pydantic. Please use temporalio.contrib.pydantic.converter.pydantic_data_converter."
15331547
)
1534-
return getattr(hint, "parse_obj")(value)
1548+
return getattr(hint, pydantic_attr)(value)
15351549

15361550
# IntEnum
15371551
if inspect.isclass(hint) and issubclass(hint, IntEnum):

0 commit comments

Comments
 (0)