File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -1590,7 +1590,9 @@ def value_to_type(
1590
1590
elif key_type is type (None ):
1591
1591
key = {"null" : None }[key ]
1592
1592
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 ):
1594
1596
key = value_to_type (key_type , key , custom_converters )
1595
1597
except Exception as err :
1596
1598
raise TypeError (
Original file line number Diff line number Diff line change 53
53
_JSONTypeConverterUnhandled ,
54
54
decode_search_attributes ,
55
55
encode_search_attribute_values ,
56
+ value_to_type ,
56
57
)
57
58
from temporalio .exceptions import ApplicationError , FailureError
58
59
@@ -91,6 +92,14 @@ class DatetimeClass:
91
92
datetime : datetime
92
93
93
94
95
+ MyNewTypeStr = NewType ("MyNewTypeStr" , str )
96
+
97
+
98
+ @dataclass
99
+ class NewTypeMessage :
100
+ data : dict [MyNewTypeStr , str ]
101
+
102
+
94
103
async def test_converter_default ():
95
104
async def assert_payload (
96
105
input ,
@@ -215,6 +224,22 @@ async def assert_payload(
215
224
type_hint = DatetimeClass ,
216
225
)
217
226
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
+
218
243
219
244
def test_binary_proto ():
220
245
# We have to test this separately because by default it never encodes
You can’t perform that action at this time.
0 commit comments