Skip to content

Commit b0aae9b

Browse files
authored
Fixed serialization of typing.Literal (#867)
Fixes #852
1 parent 80b2ad1 commit b0aae9b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

temporalio/converter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
ClassVar,
2424
Dict,
2525
List,
26+
Literal,
2627
Mapping,
2728
NewType,
2829
Optional,
@@ -39,7 +40,7 @@
3940
import google.protobuf.json_format
4041
import google.protobuf.message
4142
import google.protobuf.symbol_database
42-
from typing_extensions import Literal
43+
import typing_extensions
4344

4445
import temporalio.api.common.v1
4546
import temporalio.api.enums.v1
@@ -1424,7 +1425,7 @@ def value_to_type(
14241425
type_args: Tuple = getattr(hint, "__args__", ())
14251426

14261427
# Literal
1427-
if origin is Literal:
1428+
if origin is Literal or origin is typing_extensions.Literal:
14281429
if value not in type_args:
14291430
raise TypeError(f"Value {value} not in literal values {type_args}")
14301431
return value

tests/test_converter.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Dict,
1717
Iterable,
1818
List,
19+
Literal,
1920
Mapping,
2021
MutableMapping,
2122
NewType,
@@ -31,12 +32,12 @@
3132

3233
import pydantic
3334
import pytest
34-
from typing_extensions import Literal, TypedDict
35+
import typing_extensions
36+
from typing_extensions import TypedDict
3537

3638
import temporalio.api.common.v1
3739
import temporalio.common
3840
from temporalio.api.common.v1 import Payload, Payloads
39-
from temporalio.api.common.v1 import Payload as AnotherNameForPayload
4041
from temporalio.api.failure.v1 import Failure
4142
from temporalio.common import RawValue
4243
from temporalio.converter import (
@@ -311,6 +312,9 @@ def fail(hint: Any, value: Any) -> None:
311312
ok(Literal["foo"], "foo")
312313
ok(Literal["foo", False], False)
313314
fail(Literal["foo", "bar"], "baz")
315+
ok(typing_extensions.Literal["foo"], "foo")
316+
ok(typing_extensions.Literal["foo", False], False)
317+
fail(typing_extensions.Literal["foo", "bar"], "baz")
314318

315319
# Dataclass
316320
ok(MyDataClass, MyDataClass("foo", 5, SerializableEnum.FOO))

0 commit comments

Comments
 (0)