diff --git a/changelog/fixed/tiptoeing-integer-ferret.md b/changelog/fixed/tiptoeing-integer-ferret.md new file mode 100644 index 000000000..d729d36d8 --- /dev/null +++ b/changelog/fixed/tiptoeing-integer-ferret.md @@ -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`. diff --git a/temporalio/converter/_payload_converter.py b/temporalio/converter/_payload_converter.py index b1df6f5ce..17f7371d7 100644 --- a/temporalio/converter/_payload_converter.py +++ b/temporalio/converter/_payload_converter.py @@ -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): diff --git a/tests/test_converter.py b/tests/test_converter.py index b47cadbdd..fbe4e6410 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -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"})