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
9 changes: 8 additions & 1 deletion temporalio/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
if sys.version_info >= (3, 11):
from enum import StrEnum

if sys.version_info >= (3, 10):
from types import UnionType


class PayloadConverter(ABC):
"""Base payload converter to/from multiple payloads/values."""
Expand Down Expand Up @@ -1155,8 +1158,12 @@ def value_to_type(hint: Type, value: Any) -> Any:
raise TypeError(f"Value {value} not in literal values {type_args}")
return value

is_union = origin is Union
if sys.version_info >= (3, 10):
is_union = is_union or isinstance(origin, UnionType)

# Union
if origin is Union:
if is_union:
# Try each one. Note, Optional is just a union w/ none.
for arg in type_args:
try:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ def fail(hint: Type, value: Any) -> None:
ok(Union[int, str], "foo")
ok(Union[MyDataClass, NestedDataClass], MyDataClass("foo", 5, SerializableEnum.FOO))
ok(Union[MyDataClass, NestedDataClass], NestedDataClass("foo"))
if sys.version_info >= (3, 10):
ok(int | None, None)
ok(int | None, 5)
fail(int | None, "1")
ok(MyDataClass | NestedDataClass, MyDataClass("foo", 5, SerializableEnum.FOO))
ok(MyDataClass | NestedDataClass, NestedDataClass("foo"))

# NewType
ok(NewIntType, 5)
Expand Down