Skip to content

Decode NewType int dict keys in the JSON converter - #2138

Open
devteamaegis wants to merge 1 commit into
temporalio:mainfrom
devteamaegis:fix/newtype-int-dict-keys
Open

devteamaegis wants to merge 1 commit into
temporalio:mainfrom
devteamaegis:fix/newtype-int-dict-keys

Conversation

@devteamaegis

Copy link
Copy Markdown

What was changed

value_to_type now unwraps a NewType dict key type to its supertype before turning the JSON string key back into an int, float, bool, None, or int enum.

UserId = NewType("UserId", int)
converter = JSONPlainPayloadConverter()
payload = converter.to_payload({UserId(1): "a"})  # '{"1":"a"}'
converter.from_payload(payload, dict[UserId, str])
# before: TypeError: Failed converting key '1' to type UserId in mapping dict[UserId, str]
# after:  {1: 'a'}

Why

JSON writes every dict key as a string. The key conversion compares key_type directly against int, float, bool, NoneType, and int enums, so a NewType over one of those never matches. The key stays "1", and the final value_to_type(UserId, "1") resolves the supertype to int and rejects the string. #1059 fixed NewType of str keys (#1032), and #1900 added int enum keys; a NewType over an int-like type was still missed. The default converter can encode this value but cannot decode it, so a workflow or activity with such an argument fails at decode time.

Testing

  • Added ok(dict[NewIntType, str], {NewIntType(1): "1"}) to test_json_type_hints. It fails without the source change and passes with it.
  • pytest tests/test_converter.py: 42 passed. I ran it against the source tree with the published 1.36.1 bridge binary rather than a local Rust build, since the change is pure Python.
  • Also checked by hand that NewType keys over float, bool, an IntEnum, and a NewType of a NewType round-trip.
  • ruff check --select I, ruff format --check, pydocstyle, pyright, and basedpyright are clean on the touched files. mypy reports no errors in them.

This was investigated and written with an AI coding agent (Claude) working on my behalf.

@devteamaegis
devteamaegis requested a review from a team as a code owner November 17, 2026 20:52
@CLAassistant

CLAassistant commented Nov 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants