Skip to content

Commit 82de81a

Browse files
committed
Revert "Use structural typing for the interface"
This reverts commit b8ab990.
1 parent 1828e85 commit 82de81a

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

temporalio/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
from temporalio.converter import (
6767
DataConverter,
6868
SerializationContext,
69+
WithSerializationContext,
6970
WorkflowSerializationContext,
7071
)
7172
from temporalio.service import (
@@ -2736,7 +2737,7 @@ class AsyncActivityIDReference:
27362737
activity_id: str
27372738

27382739

2739-
class AsyncActivityHandle:
2740+
class AsyncActivityHandle(WithSerializationContext):
27402741
"""Handle representing an external activity for completion and heartbeat."""
27412742

27422743
def __init__(

temporalio/converter.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
2828
Mapping,
2929
NewType,
3030
Optional,
31-
Protocol,
3231
Sequence,
3332
Tuple,
3433
Type,
3534
TypeVar,
3635
Union,
3736
get_type_hints,
3837
overload,
39-
runtime_checkable,
4038
)
4139

4240
import google.protobuf.duration_pb2
@@ -139,13 +137,10 @@ class ActivitySerializationContext(BaseWorkflowSerializationContext):
139137
is_local: bool
140138

141139

142-
@runtime_checkable
143-
class WithSerializationContext(Protocol):
140+
# TODO: duck typing or nominal typing?
141+
class WithSerializationContext(ABC):
144142
"""Interface for classes that can use serialization context.
145143
146-
To use serialization context in your class, implementing this interface is sufficient; you do
147-
not need to inherit from this class.
148-
149144
The following classes may implement this interface:
150145
- :py:class:`PayloadConverter`
151146
- :py:class:`PayloadCodec`
@@ -337,7 +332,7 @@ def from_payload(
337332
raise NotImplementedError
338333

339334

340-
class CompositePayloadConverter(PayloadConverter):
335+
class CompositePayloadConverter(PayloadConverter, WithSerializationContext):
341336
"""Composite payload converter that delegates to a list of encoding payload converters.
342337
343338
Encoding/decoding are attempted on each payload converter successively until

tests/test_serialization_context.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
PayloadCodec,
3939
PayloadConverter,
4040
SerializationContext,
41+
WithSerializationContext,
4142
WorkflowSerializationContext,
4243
)
4344
from temporalio.exceptions import ApplicationError
@@ -65,7 +66,9 @@ class TraceData:
6566
items: list[TraceItem] = field(default_factory=list)
6667

6768

68-
class SerializationContextPayloadConverter(EncodingPayloadConverter):
69+
class SerializationContextPayloadConverter(
70+
EncodingPayloadConverter, WithSerializationContext
71+
):
6972
def __init__(self):
7073
self.context: Optional[SerializationContext] = None
7174

@@ -130,7 +133,9 @@ def from_payload(
130133
return value
131134

132135

133-
class SerializationContextCompositePayloadConverter(CompositePayloadConverter):
136+
class SerializationContextCompositePayloadConverter(
137+
CompositePayloadConverter, WithSerializationContext
138+
):
134139
def __init__(self):
135140
super().__init__(
136141
SerializationContextPayloadConverter(),
@@ -995,7 +1000,7 @@ async def run(self) -> Never:
9951000
test_traces: dict[str, list[TraceItem]] = defaultdict(list)
9961001

9971002

998-
class FailureConverterWithContext(DefaultFailureConverter):
1003+
class FailureConverterWithContext(DefaultFailureConverter, WithSerializationContext):
9991004
def __init__(self):
10001005
super().__init__(encode_common_attributes=False)
10011006
self.context: Optional[SerializationContext] = None
@@ -1126,7 +1131,7 @@ async def test_failure_converter_with_context(client: Client):
11261131
# Test payload codec
11271132

11281133

1129-
class PayloadCodecWithContext(PayloadCodec):
1134+
class PayloadCodecWithContext(PayloadCodec, WithSerializationContext):
11301135
def __init__(self):
11311136
self.context: Optional[SerializationContext] = None
11321137
self.encode_called_with_context = False
@@ -1427,7 +1432,7 @@ async def test_child_workflow_codec_with_context(client: Client):
14271432
# Payload codec: test decode context matches encode context
14281433

14291434

1430-
class PayloadEncryptionCodec(PayloadCodec):
1435+
class PayloadEncryptionCodec(PayloadCodec, WithSerializationContext):
14311436
"""
14321437
The outbound data for encoding must always be the string "outbound". "Encrypt" it by replacing
14331438
it with a key that is derived from the context available during encoding. On decryption, assert
@@ -1592,7 +1597,7 @@ async def test_decode_context_matches_encode_context(
15921597
# Test nexus payload codec
15931598

15941599

1595-
class AssertNexusLacksContextPayloadCodec(PayloadCodec):
1600+
class AssertNexusLacksContextPayloadCodec(PayloadCodec, WithSerializationContext):
15961601
def __init__(self):
15971602
self.context = None
15981603

@@ -1674,7 +1679,9 @@ class PydanticData(BaseModel):
16741679
trace: List[str] = []
16751680

16761681

1677-
class PydanticJSONConverterWithContext(PydanticJSONPlainPayloadConverter):
1682+
class PydanticJSONConverterWithContext(
1683+
PydanticJSONPlainPayloadConverter, WithSerializationContext
1684+
):
16781685
def __init__(self):
16791686
super().__init__()
16801687
self.context: Optional[SerializationContext] = None
@@ -1693,7 +1700,7 @@ def to_payload(self, value: Any) -> Optional[temporalio.api.common.v1.Payload]:
16931700
return super().to_payload(value)
16941701

16951702

1696-
class PydanticConverterWithContext(CompositePayloadConverter):
1703+
class PydanticConverterWithContext(CompositePayloadConverter, WithSerializationContext):
16971704
def __init__(self):
16981705
super().__init__(
16991706
*(
@@ -1751,7 +1758,9 @@ class UserMethodCalledError(Exception):
17511758
pass
17521759

17531760

1754-
class CustomEncodingPayloadConverter(JSONPlainPayloadConverter):
1761+
class CustomEncodingPayloadConverter(
1762+
JSONPlainPayloadConverter, WithSerializationContext
1763+
):
17551764
@property
17561765
def encoding(self) -> str:
17571766
return "custom-encoding-that-does-not-clash-with-default-converters"

0 commit comments

Comments
 (0)