Skip to content

Restore init=False dataclass fields when converting from JSON - #1920

Open
Kayvan-Zahiri wants to merge 1 commit into
temporalio:mainfrom
Kayvan-Zahiri:fix-dataclass-init-false-fields
Open

Kayvan-Zahiri wants to merge 1 commit into
temporalio:mainfrom
Kayvan-Zahiri:fix-dataclass-init-false-fields

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

A dataclass with a field(init=False) can be encoded but not decoded. The JSON encoder includes every field, and value_to_type then passes all of them to the constructor, which rejects the non-init ones:

@dataclass
class Order:
    id: str
    attempts: int = field(init=False, default=0)

# workflow/activity arg or result of type Order
TypeError: Order.__init__() got an unexpected keyword argument 'attempts'

I hit this on 1.34.0 with DataConverter.default round-tripping a plain dataclass like the one above.

The fix keeps init=False fields out of the constructor call. If the payload has a value for one, it's converted with the field's type hint like any other field and set after construction. object.__setattr__ is used so frozen dataclasses work too. Without that last step a field set after __init__ (like a counter) would silently reset to its default on decode.

Added test_json_dataclass_non_init_fields_round_trip in tests/test_converter.py, covering a field computed in __post_init__, a field changed after construction, and a frozen dataclass. It fails on main with the TypeError above and passes with the fix.

Validation:

  • pytest tests/test_converter.py: 41 passed
  • ruff check, ruff format --check, basedpyright and mypy --namespace-packages --check-untyped-defs on the changed files: clean. pyright is clean on _payload_converter.py. On tests/test_converter.py it only reports pytest as an unresolved import, which it also does on main in my env.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Kayvan-Zahiri
Kayvan-Zahiri requested a review from a team as a code owner October 2, 2026 19:58
@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.

1 similar comment
@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