Skip to content

Commit c17b088

Browse files
committed
Implement WithSerializationContext on CompositePayloadConverter
1 parent 1167da6 commit c17b088

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

temporalio/converter.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def from_payload(
301301
raise NotImplementedError
302302

303303

304-
class CompositePayloadConverter(PayloadConverter):
304+
class CompositePayloadConverter(PayloadConverter, WithSerializationContext):
305305
"""Composite payload converter that delegates to a list of encoding payload converters.
306306
307307
Encoding/decoding are attempted on each payload converter successively until
@@ -321,6 +321,9 @@ def __init__(self, *converters: EncodingPayloadConverter) -> None:
321321
"""
322322
# Insertion order preserved here since Python 3.7
323323
self.converters = {c.encoding.encode(): c for c in converters}
324+
self._any_converters_with_context = any(
325+
isinstance(c, WithSerializationContext) for c in converters
326+
)
324327

325328
def to_payloads(
326329
self, values: Sequence[Any]
@@ -384,6 +387,29 @@ def from_payloads(
384387
) from err
385388
return values
386389

390+
def with_context(self, context: Optional[SerializationContext]) -> Self:
391+
"""Return a copy of this converter with context-aware child converters.
392+
393+
Returns a new instance with the same set of converters, using with_context() on the child
394+
converters for those that support it.
395+
396+
Args:
397+
context: The serialization context to use, or None for no context.
398+
399+
Returns:
400+
A new CompositePayloadConverter with context-aware converters, or self if no converters
401+
support context.
402+
"""
403+
if not self._any_converters_with_context:
404+
return self
405+
new_converters = []
406+
for converter in self.converters.values():
407+
if isinstance(converter, WithSerializationContext):
408+
new_converters.append(converter.with_context(context))
409+
else:
410+
new_converters.append(converter)
411+
return type(self)(*new_converters)
412+
387413

388414
class DefaultPayloadConverter(CompositePayloadConverter):
389415
"""Default payload converter compatible with other Temporal SDKs.

0 commit comments

Comments
 (0)