Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog/fixed/tiptoeing-integer-ferret.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Decode `dict` keys typed as a `NewType` of `int`, `float`, `bool`, or an int enum with the default JSON converter, instead of failing with a `TypeError`.
18 changes: 11 additions & 7 deletions temporalio/converter/_payload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,18 +939,22 @@ def value_to_type(
# original type.
try:
if isinstance(key, str):
if key_type is int or key_type is float:
key = key_type(key)
# A NewType key is serialized like its supertype
base_key_type = key_type
while hasattr(base_key_type, "__supertype__"):
base_key_type = base_key_type.__supertype__
if base_key_type is int or base_key_type is float:
key = base_key_type(key)
elif (
inspect.isclass(key_type)
and issubclass(key_type, Enum)
and issubclass(key_type, int)
inspect.isclass(base_key_type)
and issubclass(base_key_type, Enum)
and issubclass(base_key_type, int)
):
# JSON serializes int enum keys as their number
key = int(key)
elif key_type is bool:
elif base_key_type is bool:
key = {"true": True, "false": False}[key]
elif key_type is type(None):
elif base_key_type is type(None):
key = {"null": None}[key]

if not isinstance(key_type, type) or not isinstance(key, key_type):
Expand Down
1 change: 1 addition & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ def fail(hint: Any, value: Any) -> None:
ok(dict[int, str], {1: "1"})
ok(dict[float, str], {1.0: "1"})
ok(dict[bool, str], {True: "1"})
ok(dict[NewIntType, str], {NewIntType(1): "1"})

# On a 3.10+ dict type, None isn't returned from a key. This is potentially a bug
ok(dict[None, str], {"null": "1"})
Expand Down