Skip to content

Fixed serialization of typing.Literal #867

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 2 commits into from
May 14, 2025
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
5 changes: 3 additions & 2 deletions temporalio/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ClassVar,
Dict,
List,
Literal,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that typing.Literal is present in 3.8+ so it is safe for use in all versions we currently support (it wasn't present in the oldest supported Python version when we first developed this)

Mapping,
NewType,
Optional,
Expand All @@ -39,7 +40,7 @@
import google.protobuf.json_format
import google.protobuf.message
import google.protobuf.symbol_database
from typing_extensions import Literal
import typing_extensions

import temporalio.api.common.v1
import temporalio.api.enums.v1
Expand Down Expand Up @@ -1424,7 +1425,7 @@ def value_to_type(
type_args: Tuple = getattr(hint, "__args__", ())

# Literal
if origin is Literal:
if origin is Literal or origin is typing_extensions.Literal:
if value not in type_args:
raise TypeError(f"Value {value} not in literal values {type_args}")
return value
Expand Down
8 changes: 6 additions & 2 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
Dict,
Iterable,
List,
Literal,
Mapping,
MutableMapping,
NewType,
Expand All @@ -31,12 +32,12 @@

import pydantic
import pytest
from typing_extensions import Literal, TypedDict
import typing_extensions
from typing_extensions import TypedDict

import temporalio.api.common.v1
import temporalio.common
from temporalio.api.common.v1 import Payload, Payloads
from temporalio.api.common.v1 import Payload as AnotherNameForPayload
from temporalio.api.failure.v1 import Failure
from temporalio.common import RawValue
from temporalio.converter import (
Expand Down Expand Up @@ -311,6 +312,9 @@ def fail(hint: Any, value: Any) -> None:
ok(Literal["foo"], "foo")
ok(Literal["foo", False], False)
fail(Literal["foo", "bar"], "baz")
ok(typing_extensions.Literal["foo"], "foo")
ok(typing_extensions.Literal["foo", False], False)
fail(typing_extensions.Literal["foo", "bar"], "baz")

# Dataclass
ok(MyDataClass, MyDataClass("foo", 5, SerializableEnum.FOO))
Expand Down
Loading