Skip to content

fix(proxy): carry dataclass class-level metadata onto synthesized proxy classes - #7014

Merged
masenf merged 5 commits into
mainfrom
claude/mutableproxy-dataclass-params-3wiemv
Sep 2, 2026
Merged

masenf merged 5 commits into
mainfrom
claude/mutableproxy-dataclass-params-3wiemv

Conversation

@masenf

@masenf masenf commented Sep 1, 2026 •

Copy link
Copy Markdown
Collaborator

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Description

MutableProxy.__new__ builds one wrapper class per wrapped dataclass type and copied __dataclass_fields__ onto it, which is what dataclasses.is_dataclass tests for. The rest of the metadata @dataclass writes was left behind, so the wrapper answered yes to is_dataclass(type(value)) and then raised AttributeError for the next thing a caller reads:

@dataclasses.dataclass(frozen=True)
class Frozen:
    x: int = 1

class S(rx.State):
    f: Frozen = Frozen()

value = state.f
dataclasses.is_dataclass(type(value))       # True
type(value).__dataclass_params__            # AttributeError, before this change
type(value).__match_args__                  # AttributeError, before this change
value.__dataclass_params__                  # worked: wrapt forwards it to the wrapped object

The instance lookups happened to work because wrapt forwards them to the wrapped object, which is why this only bit code going through the class — the standard idiom right after an is_dataclass check.

What is copied, and what is not

Every class attribute @dataclass writes (CPython 3.10–3.14 _process_class) was audited against what the synthesized proxy class exposes. They fall into three groups:

Attributes Handling
Plain data, read off the class __dataclass_fields__, __dataclass_params__, __match_args__ Copied — instance forwarding cannot answer for a class-level read
Generated methods __init__, __repr__, __eq__, __lt__/__le__/__gt__/__ge__, __hash__, frozen __setattr__/__delattr__, __replace__, __getstate__/__setstate__ Not copied — already resolve through the wrapped object; rebinding them to the proxy would sidestep its dirty tracking
Class layout and values __slots__, field defaults, ClassVars Not copied — a class attribute is found before __getattr__ runs, so copying these would shadow the wrapped instance's own values

Metadata the wrapped class was declared without is not invented either: @dataclass(match_args=False) leaves no __match_args__ on the wrapper.

Testing

  • New tests in tests/units/istate/test_proxy.py: metadata is carried (plain, frozen and slots=True models), absent metadata is not invented (match_args=False), no behavior or layout is copied, and a frozen dataclass stays frozen through its proxy. The metadata assertions fail with AttributeError before the change.
  • Full unit suite: 8013 passed. The two tests/units/utils/test_processes.py failures are pre-existing and environmental (no IPv6 in the test container) — they fail identically on a clean checkout of main, and are deselected in that count.
  • ruff check, ruff format, and pyright clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VFzJWvNNwomK7nhsEN2ELR

MutableProxy builds one wrapper class per wrapped dataclass type and copies
`__dataclass_fields__` onto it, which is what `dataclasses.is_dataclass`
tests for. The decorator's other class-level attribute,
`__dataclass_params__`, was left behind, so the wrapper answered yes to
`is_dataclass(type(value))` and then raised `AttributeError` for the very
next thing a caller reads -- the `frozen`/`eq`/`slots` flags. The instance
lookup happened to work (wrapt forwards it to the wrapped object), which is
why this only bit code that goes through the class.

Copy both attributes, skipping either one the wrapped class does not have.
@masenf
masenf requested a review from a team as a code owner September 1, 2026 07:25
@greptile-apps

greptile-apps Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR carries class-level dataclass metadata onto synthesized mutable-proxy classes while avoiding metadata that would shadow instance fields.

  • Adds a helper for selecting __dataclass_fields__, __dataclass_params__, and __match_args__.
  • Preserves absent metadata such as __match_args__ for standalone match_args=False dataclasses.
  • Adds coverage for frozen, slotted, pseudo-field, collision, and mutation behavior.
  • Documents the proxy metadata fix in the release notes.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
reflex/istate/proxy.py Synthesized dataclass proxy classes now receive selected class-level metadata without copying generated behavior or shadowing ordinary instance fields.
tests/units/istate/test_proxy.py Adds focused coverage for metadata propagation, absent metadata, field-name collisions, slots, pseudo-fields, and frozen mutation behavior.
news/+mutableproxy-dataclass-params.bugfix.md Accurately describes the newly available class-level dataclass metadata on mutable proxies.

Reviews (5): Last reviewed commit: "docs(proxy): cut the dataclass metadata ..." | Re-trigger 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.

No issues found across 3 files

Re-trigger cubic

@codspeed

codspeed Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 32 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing claude/mutableproxy-dataclass-params-3wiemv (3cc06ee) with main (812bb47)

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. ↩

@FarhanAliRaza FarhanAliRaza 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.

Tested in a Reflex app built on a worktree of this branch, and in the same app against main as a baseline. The app holds a plain dataclass state var, a nested dataclass, and a frozen=True, eq=True dataclass.

On main, an event handler that reads type(state.frozen).__dataclass_params__ after an is_dataclass test fails with AttributeError. On this branch it returns the correct flags, for nested proxies as well.

Mutation tracking, dataclasses.replace, asdict, and FrozenInstanceError on assignment to a frozen value all still work. tests/units/istate and tests/units/test_state.py pass. The new test fails when I revert the __new__ hunk.

Two requested changes are inline.

Comment thread reflex/istate/proxy.py Resolved
Comment thread reflex/istate/proxy.py Resolved
Audited every class attribute `@dataclass` writes (CPython 3.10-3.14
`_process_class`) against what a synthesized proxy class exposes. Three are
plain data read off the class, where the proxy's instance forwarding cannot
answer: `__dataclass_fields__`, `__dataclass_params__` and `__match_args__`.
The last was still missing, so `type(value).__match_args__` raised on a
proxied dataclass while `value.__match_args__` worked.

Everything else it writes stays off the proxy class, now stated in the
comment so the set is not widened by reflex: the generated methods
(`__init__`, `__repr__`, `__eq__`, the ordering set, the frozen
`__setattr__`/`__delattr__` pair, `__replace__`, `__getstate__`) already
resolve through the wrapped object and would sidestep dirty tracking if
rebound here, while field defaults, ClassVars and `__slots__` would shadow
the wrapped instance outright -- a class attribute is found before
`__getattr__` runs, so a copied default wins over the instance's own value.

Tests cover a slots dataclass (layout must not leak), a
`match_args=False` one (absent metadata must not be invented), that no
behavior is copied, and that a frozen dataclass stays frozen through its
proxy.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VFzJWvNNwomK7nhsEN2ELR
@masenf masenf changed the title fix(proxy): carry __dataclass_params__ onto synthesized proxy classes fix(proxy): carry dataclass class-level metadata onto synthesized proxy classes Sep 1, 2026

@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

Comment thread reflex/istate/proxy.py Resolved
A dataclass may declare a field named `__match_args__` or
`__dataclass_params__` -- neither is name-mangled, so both are legal field
names. Copying the class attribute for such a name shadowed the wrapped
instance: `proxy.__match_args__` and `dataclasses.asdict(proxy)` returned
the class value while the wrapped object held another, since a class
attribute is found before `__getattr__` forwards.

This is the hazard the comment already described for field defaults and
ClassVars, so the copy now applies the same rule to itself and skips any
name the wrapped dataclass declares as a field. The namespace construction
moves into `_dataclass_proxy_namespace`, where both skips -- a field-name
collision, and the `__match_args__` that `match_args=False` never wrote --
have room to be explained.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VFzJWvNNwomK7nhsEN2ELR

@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 2 files (changes from recent commits).

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

Re-trigger cubic

Comment thread reflex/istate/proxy.py Resolved
`__dataclass_fields__` carries ClassVar and InitVar entries alongside the
real fields, so keying the skip on it was too broad: a dataclass declaring
`__match_args__: ClassVar[...]` lost that copy, and one declaring
`__dataclass_fields__: ClassVar[...]` lost the fields copy outright, which
left `is_dataclass(type(proxy))` false and the wrapper no longer
impersonating a dataclass at all.

Neither pseudo-field can be shadowed by the copy: both keep their value on
the class, and the instance has no attribute of its own for a class
attribute to hide. `dataclasses.fields()` lists exactly the entries that
can -- the real instance fields -- so the skip keys on that instead.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VFzJWvNNwomK7nhsEN2ELR
…e needs

The comments had grown into an account of the audit behind them and of the
approaches that did not survive it. Both belong in the history, not next to
the code: what a reader needs here is why an instance field is skipped and
why `hasattr` guards the copy.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VFzJWvNNwomK7nhsEN2ELR
@masenf

masenf commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator Author

@FarhanAliRaza ready to go now 🚀

@masenf
masenf merged commit 0937e12 into main Sep 2, 2026
111 checks passed
@masenf
masenf deleted the claude/mutableproxy-dataclass-params-3wiemv branch September 2, 2026 16:43
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.

3 participants