Skip to content

Commit 17f1907

Browse files
committed
Clean up from_payload
1 parent 7dd09a2 commit 17f1907

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

temporalio/contrib/pydantic/converter.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ def from_payload(self, payload: Payload, type_hint: Optional[Type] = None) -> An
4444
data = json.loads(payload.data.decode())
4545
if type_hint and typing.get_origin(type_hint) is list:
4646
assert isinstance(data, list), "Expected list"
47-
[type_hint] = typing.get_args(type_hint)
48-
assert type_hint is not None, "Expected type hint"
49-
assert issubclass(type_hint, BaseModel), "Expected BaseModel"
47+
[model] = typing.get_args(type_hint)
48+
assert model is not None, "Expected type hint"
49+
assert issubclass(model, BaseModel), "Expected BaseModel"
5050
if temporalio.workflow.unsafe.in_sandbox():
51-
type_hint = _unwrap_restricted_fields(type_hint)
52-
53-
return [self._from_dict(d, type_hint) for d in data]
51+
model = create_model(
52+
model.__name__,
53+
**{ # type: ignore
54+
name: (RestrictionContext.unwrap_if_proxied(f.annotation), f)
55+
for name, f in model.model_fields.items()
56+
},
57+
)
58+
59+
return [self._from_dict(d, model) for d in data]
5460
return self._from_dict(data, type_hint)
5561

5662
def _from_dict(self, data: dict, type_hint: Optional[Type]) -> Any:
@@ -81,13 +87,3 @@ def __init__(self) -> None:
8187
payload_converter_class=PydanticPayloadConverter
8288
)
8389
"""Data converter using Pydantic JSON conversion."""
84-
85-
86-
def _unwrap_restricted_fields(
87-
model: Type[BaseModel],
88-
) -> Type[BaseModel]:
89-
fields = {
90-
name: (RestrictionContext.unwrap_if_proxied(f.annotation), f)
91-
for name, f in model.model_fields.items()
92-
}
93-
return create_model(model.__name__, **fields) # type: ignore

0 commit comments

Comments
 (0)