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
13 changes: 10 additions & 3 deletions temporalio/converter/_payload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ def to_transfer_type(self, value: ValueT) -> TransferTypeT:
raise NotImplementedError

@abstractmethod
def from_transfer_type(self, value: TransferTypeT) -> ValueT:
def from_transfer_type(
self, value: TransferTypeT, type_hint: type[ValueT]
) -> ValueT:
"""Convert a transfer type value to its user-facing value.

``type_hint`` is the requested user-facing type, including concrete
generic arguments.

.. warning::
This API is experimental and subject to change.
"""
Expand Down Expand Up @@ -638,8 +643,10 @@ def from_payloads(
payloads, typing.cast("list[type]", inner_type_hints)
)
return [
converter.from_transfer_type(value) if converter is not None else value
for value, converter in zip(values, converters)
converter.from_transfer_type(value, type_hint)
if converter is not None
else value
for value, converter, type_hint in zip(values, converters, type_hints)
]

def with_context(self, context: SerializationContext) -> Self:
Expand Down
71 changes: 71 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import sys
import traceback
import typing
from collections import deque
from collections.abc import Iterable, Mapping, MutableMapping, Sequence
from dataclasses import dataclass
Expand All @@ -14,8 +15,11 @@
from typing import (
Any,
Dict, # type:ignore[reportDeprecated]
Generic,
Literal,
NewType,
TypeVar,
cast,
get_args,
get_type_hints,
)
Expand Down Expand Up @@ -278,6 +282,7 @@ def to_transfer_type(
def from_transfer_type(
self,
value: temporalio.api.common.v1.WorkflowExecution,
type_hint: type[TemporalTransferTypeValue],
) -> TemporalTransferTypeValue:
return TemporalTransferTypeValue(value=value.workflow_id)

Expand Down Expand Up @@ -305,6 +310,7 @@ def to_transfer_type(
def from_transfer_type(
self,
value: temporalio.api.common.v1.WorkflowExecution,
type_hint: type[TemporalTransferTypeValueWithoutHint],
) -> TemporalTransferTypeValueWithoutHint:
return TemporalTransferTypeValueWithoutHint(value=value.workflow_id)

Expand All @@ -315,6 +321,47 @@ class TemporalTransferTypeValueWithoutHint:
value: str


T = TypeVar("T")


@dataclass
class TemporalTransferTypeGenericValue(Generic[T]):
value: T


class TemporalTransferTypeGenericValueConverter(
TransferTypeConverter[
TemporalTransferTypeGenericValue[T],
temporalio.api.common.v1.WorkflowExecution,
]
):
transfer_type = temporalio.api.common.v1.WorkflowExecution

def to_transfer_type(
self, value: TemporalTransferTypeGenericValue[T]
) -> temporalio.api.common.v1.WorkflowExecution:
return temporalio.api.common.v1.WorkflowExecution(
workflow_id=str(value.value),
run_id="run-id",
)

def from_transfer_type(
self,
value: temporalio.api.common.v1.WorkflowExecution,
type_hint: type[TemporalTransferTypeGenericValue[T]],
) -> TemporalTransferTypeGenericValue[T]:
converted_value: str | int = value.workflow_id
if typing.get_args(type_hint)[0] is int:
converted_value = int(converted_value)
return TemporalTransferTypeGenericValue(value=cast(T, converted_value))


# Register after both classes are defined so the generic type can be resolved.
transfer_type_convertible(TemporalTransferTypeGenericValueConverter)(
TemporalTransferTypeGenericValue
)


class CustomDefaultPayloadConverter(DefaultPayloadConverter):
pass

Expand Down Expand Up @@ -358,6 +405,30 @@ def test_temporal_transfer_type_payload_converter_without_transfer_type_hint():
)


@pytest.mark.parametrize(
("value", "type_hint"),
[
(
TemporalTransferTypeGenericValue("workflow-id"),
TemporalTransferTypeGenericValue[str],
),
(
TemporalTransferTypeGenericValue(123),
TemporalTransferTypeGenericValue[int],
),
],
)
def test_temporal_transfer_type_payload_converter_with_generic_value(
value: TemporalTransferTypeGenericValue[T],
type_hint: type[TemporalTransferTypeGenericValue[T]],
):
converter = DataConverter.default.payload_converter

payload = converter.to_payload(value)

assert converter.from_payload(payload, type_hint) == value


def test_transfer_type_convertible_rejects_existing_converter():
with pytest.raises(TypeError, match="already has a transfer type converter"):
transfer_type_convertible(TemporalTransferTypeValueConverter)(
Expand Down
Loading