Skip to content

Add StrEnum conversion support #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 3, 2022
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ The default data converter supports converting multiple types including:
* Iterables including ones JSON dump may not support by default, e.g. `set`
* Any class with a `dict()` method and a static `parse_obj()` method, e.g.
[Pydantic models](https://pydantic-docs.helpmanual.io/usage/models)
* [IntEnum](https://docs.python.org/3/library/enum.html) based enumerates
* [IntEnum, StrEnum](https://docs.python.org/3/library/enum.html) based enumerates

For converting from JSON, the workflow/activity type hint is taken into account to convert to the proper type. Care has
been taken to support all common typings including `Optional`, `Union`, all forms of iterables and mappings, `NewType`,
Expand Down
14 changes: 14 additions & 0 deletions temporalio/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dataclasses
import inspect
import json
import sys
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
Expand All @@ -33,6 +34,10 @@
import temporalio.api.common.v1
import temporalio.common

# StrEnum is available in 3.11+
if sys.version_info >= (3, 11):
from enum import StrEnum


class PayloadConverter(ABC):
"""Base payload converter to/from multiple payloads/values."""
Expand Down Expand Up @@ -874,6 +879,15 @@ def value_to_type(hint: Type, value: Any) -> Any:
)
return hint(value)

# StrEnum, available in 3.11+
if sys.version_info >= (3, 11):
if inspect.isclass(hint) and issubclass(hint, StrEnum):
if not isinstance(value, str):
raise TypeError(
f"Cannot convert to enum {hint}, value not a string, value is {type(value)}"
)
return hint(value)

# Iterable. We intentionally put this last as it catches several others.
if inspect.isclass(origin) and issubclass(origin, collections.abc.Iterable):
if not isinstance(value, collections.abc.Iterable):
Expand Down
23 changes: 21 additions & 2 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import temporalio.converter
from temporalio.api.common.v1 import Payload as AnotherNameForPayload

# StrEnum is available in 3.11+
if sys.version_info >= (3, 11):
from enum import StrEnum


class NonSerializableClass:
pass
Expand All @@ -46,6 +50,12 @@ class SerializableEnum(IntEnum):
FOO = 1


if sys.version_info >= (3, 11):

class SerializableStrEnum(StrEnum):
FOO = "foo"


@dataclass
class MyDataClass:
foo: str
Expand Down Expand Up @@ -107,8 +117,8 @@ async def assert_payload(
await assert_payload(NonSerializableClass(), None, None)
assert "not JSON serializable" in str(excinfo.value)

# Bad enum type. We do not allow non-int enums due to ambiguity in
# rebuilding and other confusion.
# Bad enum type. We do not allow non-int or non-str enums due to ambiguity
# in rebuilding and other confusion.
with pytest.raises(TypeError) as excinfo:
await assert_payload(NonSerializableEnum.FOO, None, None)
assert "not JSON serializable" in str(excinfo.value)
Expand Down Expand Up @@ -295,6 +305,15 @@ def fail(hint: Type, value: Any) -> None:
ok(SerializableEnum, SerializableEnum.FOO)
ok(List[SerializableEnum], [SerializableEnum.FOO, SerializableEnum.FOO])

# StrEnum is available in 3.11+
if sys.version_info >= (3, 11):
# StrEnum
ok(SerializableStrEnum, SerializableStrEnum.FOO)
ok(
List[SerializableStrEnum],
[SerializableStrEnum.FOO, SerializableStrEnum.FOO],
)

# 3.10+ checks
if sys.version_info >= (3, 10):
ok(list[int], [1, 2])
Expand Down