Repository navigation
fix(proxy): carry dataclass class-level metadata onto synthesized proxy classes - #7014
Conversation
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.
Greptile SummaryThis PR carries class-level dataclass metadata onto synthesized mutable-proxy classes while avoiding metadata that would shadow instance fields.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| 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
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
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.
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
There was a problem hiding this comment.
All reported issues were addressed across 3 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
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
There was a problem hiding this comment.
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
`__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
|
@FarhanAliRaza ready to go now 🚀 |
All Submissions:
Type of change
Changes To Core Features:
Description
MutableProxy.__new__builds one wrapper class per wrapped dataclass type and copied__dataclass_fields__onto it, which is whatdataclasses.is_dataclasstests for. The rest of the metadata@dataclasswrites was left behind, so the wrapper answered yes tois_dataclass(type(value))and then raisedAttributeErrorfor the next thing a caller reads: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_dataclasscheck.What is copied, and what is not
Every class attribute
@dataclasswrites (CPython 3.10–3.14_process_class) was audited against what the synthesized proxy class exposes. They fall into three groups:__dataclass_fields__,__dataclass_params__,__match_args____init__,__repr__,__eq__,__lt__/__le__/__gt__/__ge__,__hash__, frozen__setattr__/__delattr__,__replace__,__getstate__/__setstate____slots__, field defaults, ClassVars__getattr__runs, so copying these would shadow the wrapped instance's own valuesMetadata the wrapped class was declared without is not invented either:
@dataclass(match_args=False)leaves no__match_args__on the wrapper.Testing
tests/units/istate/test_proxy.py: metadata is carried (plain, frozen andslots=Truemodels), 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 withAttributeErrorbefore the change.tests/units/utils/test_processes.pyfailures are pre-existing and environmental (no IPv6 in the test container) — they fail identically on a clean checkout ofmain, and are deselected in that count.ruff check,ruff format, andpyrightclean.🤖 Generated with Claude Code
https://claude.ai/code/session_01VFzJWvNNwomK7nhsEN2ELR