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
4 changes: 1 addition & 3 deletions temporalio/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1764,9 +1764,7 @@ def value_to_type(
elif key_type is type(None):
key = {"null": None}[key]

# Can't call isinstance if key_type is a newtype
is_newtype = getattr(key_type, "__supertype__", None)
if is_newtype or not isinstance(key, key_type):
if not isinstance(key_type, type) or not isinstance(key, key_type):
key = value_to_type(key_type, key, custom_converters)
except Exception as err:
raise TypeError(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,29 @@ async def test_json_type_converter():
assert [addr, addr] == (
await custom_conv.decode([list_payload], [List[ipaddress.IPv4Address]])
)[0]


def test_value_to_type_literal_key():
# The type for the dictionary's *key*:
KeyHint = Literal[
"Key1",
"Key2",
]

# The type for the dictionary's *value* (the inner dict):
InnerKeyHint = Literal[
"Inner1",
"Inner2",
]
InnerValueHint = str | int | float | None
ValueHint = dict[InnerKeyHint, InnerValueHint]

# The full type hint for the mapping:
hint_with_bug = dict[KeyHint, ValueHint]

# A value that uses one of the literal keys:
value_to_convert = {"Key1": {"Inner1": 123.45, "Inner2": 10}}
custom_converters: Sequence[JSONTypeConverter] = []

# Function executes without error
value_to_type(hint_with_bug, value_to_convert, custom_converters)