Skip to content

Commit bb90aab

Browse files
authored
Don't fail on newtype string dict keys (#1059)
* Don't fail on newtype string dict keys * Change names
1 parent 7f228d8 commit bb90aab

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

temporalio/converter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,9 @@ def value_to_type(
15901590
elif key_type is type(None):
15911591
key = {"null": None}[key]
15921592

1593-
if not isinstance(key, key_type):
1593+
# Can't call isinstance if key_type is a newtype
1594+
is_newtype = getattr(key_type, "__supertype__", None)
1595+
if is_newtype or not isinstance(key, key_type):
15941596
key = value_to_type(key_type, key, custom_converters)
15951597
except Exception as err:
15961598
raise TypeError(

tests/test_converter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
_JSONTypeConverterUnhandled,
5454
decode_search_attributes,
5555
encode_search_attribute_values,
56+
value_to_type,
5657
)
5758
from temporalio.exceptions import ApplicationError, FailureError
5859

@@ -91,6 +92,14 @@ class DatetimeClass:
9192
datetime: datetime
9293

9394

95+
MyNewTypeStr = NewType("MyNewTypeStr", str)
96+
97+
98+
@dataclass
99+
class NewTypeMessage:
100+
data: dict[MyNewTypeStr, str]
101+
102+
94103
async def test_converter_default():
95104
async def assert_payload(
96105
input,
@@ -215,6 +224,22 @@ async def assert_payload(
215224
type_hint=DatetimeClass,
216225
)
217226

227+
# Newtype String
228+
await assert_payload(
229+
MyNewTypeStr("somestr"),
230+
"json/plain",
231+
'"somestr"',
232+
type_hint=MyNewTypeStr,
233+
)
234+
235+
# Newtype String key
236+
await assert_payload(
237+
NewTypeMessage({MyNewTypeStr("key"): "value"}),
238+
"json/plain",
239+
'{"data":{"key":"value"}}',
240+
type_hint=NewTypeMessage,
241+
)
242+
218243

219244
def test_binary_proto():
220245
# We have to test this separately because by default it never encodes

0 commit comments

Comments
 (0)