Skip to content

fix: raise a clear error for dataclasses.field(...) state var defaults - #7308

Merged
FarhanAliRaza merged 13 commits into
reflex-dev:mainfrom
flcrom:fix-7017-dataclasses-field-default
Nov 23, 2026
Merged

FarhanAliRaza merged 13 commits into
reflex-dev:mainfrom
flcrom:fix-7017-dataclasses-field-default

Conversation

@flcrom

@flcrom flcrom commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes #7017

A state var default of dataclasses.field(...) kept the dataclasses.Field object itself as the default value. Building the initial state then deep-copied that object and failed on its metadata mappingproxy, with no hint of which var caused it:

TypeError: cannot pickle 'mappingproxy' object

Now BaseStateMeta raises a StateValueError when the class is defined. The message names the var and points to the supported form:

StateValueError: State.item uses dataclasses.field(...), which state classes do not support. Use rx.field(...) instead.

Reflex keeps one way to declare a field (rx.field) instead of supporting a second one.

Tests: test_dataclasses_field_default_raises in tests/units/reflex_base/vars/test_base.py (frontend and backend names, with and without annotation) and test_state_var_dataclasses_field_default_raises in tests/units/test_state.py. Both fail on main and pass with the fix.

Review in cubic

AI help: used for parts of this change.

@flcrom
flcrom requested a review from a team as a code owner September 24, 2026 10:17

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Re-trigger cubic

@codspeed

codspeed Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 18 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing flcrom:fix-7017-dataclasses-field-default (4347b78) with main (fc761bd)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches state class declarations across Reflex.

The implementation has no new confirmed defects, but the existing requirement to compare complete error messages must be satisfied before merging.

Summary

The PR now rejects dataclasses.field(...) when a state class is defined, rather than converting it to rx.field(...).

  • State classes reject dataclasses.field(...) with a clear replacement hint.

Reviews (11) · Last reviewed commit: "Reject dataclasses.field(...) in state c..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Resolve conflicts with the BaseStateMeta field-handling refactor: port the dataclasses.field(...) unpacking into _unannotated_fields/_annotated_fields, drop the obsolete new_backend_vars change (machinery removed upstream), and update the tests to __fields__ assertions.

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your contribution. one outstanding issue regarding mutable defaults in unannotated fields

Comment thread tests/units/test_state.py Resolved

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread tests/units/test_state.py Resolved
@flcrom

flcrom commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Could a maintainer rerun the failed build jobs? Setup timed out downloading Plotly 7.1.0 from PyPI after three retries, before the minimum-dependency check ran. The build gate failed because of that setup failure.

https://github.com/reflex-dev/reflex/actions/runs/36835213735/job/110282575310

@masenf masenf added the bug Something isn't working label Oct 6, 2026
@FarhanAliRaza FarhanAliRaza added the on deck PRs lined up to review / merge next label Nov 23, 2026
# Conflicts:
#	packages/reflex-base/src/reflex_base/vars/base.py
Convert each dataclasses.Field in the class namespace to rx.field(...) before
fields are collected, so every existing path (annotated, unannotated, backend,
inherited overrides) treats it like rx.field. Drop the separate type inference
and the require_serializable flag for unannotated fields: an unannotated
dataclasses.field now types its var exactly like rx.field does.
Comment thread tests/units/test_state.py Resolved
A dataclasses.field(...) state var default crashed at compile time with
"cannot pickle 'mappingproxy' object". Raise a StateValueError when the
class is defined instead, naming the var and pointing to rx.field(...),
rather than supporting a second way to declare fields.
@FarhanAliRaza FarhanAliRaza changed the title fix: support dataclasses.field(...) as a state var default fix: raise a clear error for dataclasses.field(...) state var defaults Nov 23, 2026
@FarhanAliRaza
FarhanAliRaza merged commit 59e1b2f into reflex-dev:main Nov 23, 2026
171 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

State var default of dataclasses.field(...) fails with TypeError: cannot pickle 'mappingproxy' object

3 participants