-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ENG-9860 fix(vars): preserve custom Field attributes in state metaclass #6726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FarhanAliRaza
wants to merge
2
commits into
reflex-dev:main
Choose a base branch
from
FarhanAliRaza:farhan/eng-9860-state-metaclass-should-preserve-custom-attributes-on-field
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+96
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Custom attributes set on a `Field` are now preserved (deep-copied) when the state metaclass rebuilds fields, instead of being silently discarded. The reserved `annotation` attribute is never carried over so rebuilt fields are not misidentified as pydantic fields. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Tests for reflex_base.vars.base state metaclass field handling.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from reflex_base.utils.types import get_field_type | ||
| from reflex_base.vars.base import EvenMoreBasicBaseState, field | ||
|
|
||
| _MARKER_ATTR = "_marker" | ||
|
|
||
|
|
||
| def test_custom_field_attr_survives_annotated_rebuild(): | ||
| """A custom attribute on an annotated Field survives a rebuild.""" | ||
| f = field("x") | ||
| setattr(f, _MARKER_ATTR, "tag") | ||
|
|
||
| class MyState(EvenMoreBasicBaseState): | ||
| name: str = f # pyright: ignore[reportAssignmentType] | ||
|
|
||
| rebuilt = MyState.get_fields()["name"] | ||
| assert getattr(rebuilt, _MARKER_ATTR, None) == "tag" | ||
| assert rebuilt.annotated_type is str | ||
|
|
||
|
|
||
| def test_custom_field_attr_survives_unannotated_rebuild(): | ||
| """A custom attribute survives an inferred-type Field rebuild.""" | ||
| f = field(0) | ||
| setattr(f, _MARKER_ATTR, "tag") | ||
|
|
||
| class MyState(EvenMoreBasicBaseState): | ||
| count = f | ||
|
|
||
| rebuilt = MyState.get_fields()["count"] | ||
| assert getattr(rebuilt, _MARKER_ATTR, None) == "tag" | ||
| assert rebuilt.annotated_type is int | ||
|
|
||
|
|
||
| def test_custom_field_attr_survives_unannotated_factory_rebuild(): | ||
| """A custom attribute survives a default-factory Field rebuild.""" | ||
| f = field(default_factory=list) | ||
| setattr(f, _MARKER_ATTR, "tag") | ||
|
|
||
| class MyState(EvenMoreBasicBaseState): | ||
| items = f | ||
|
|
||
| rebuilt = MyState.get_fields()["items"] | ||
| assert getattr(rebuilt, _MARKER_ATTR, None) == "tag" | ||
| assert rebuilt.annotated_type is Any | ||
|
|
||
|
|
||
| def test_reserved_annotation_attr_not_copied(): | ||
| """A custom `annotation` attr must not make the rebuilt Field look pydantic. | ||
|
|
||
| get_field_type duck-types __fields__ entries on `.annotation`, so copying | ||
| it would shadow the real class annotation. | ||
| """ | ||
| f = field("x") | ||
| f.annotation = int # pyright: ignore[reportAttributeAccessIssue] | ||
|
|
||
| class MyState(EvenMoreBasicBaseState): | ||
| name: str = f # pyright: ignore[reportAssignmentType] | ||
|
|
||
| rebuilt = MyState.get_fields()["name"] | ||
| assert "annotation" not in rebuilt.__dict__ | ||
| assert get_field_type(MyState, "name") is str | ||
|
|
||
|
|
||
| def test_custom_mutable_attr_is_deepcopied(): | ||
| """Mutable custom attrs are deep-copied, not shared by reference.""" | ||
| f = field("x") | ||
| opts = {"a": [1]} | ||
| f._opts = opts # pyright: ignore[reportAttributeAccessIssue] | ||
|
|
||
| class MyState(EvenMoreBasicBaseState): | ||
| name: str = f # pyright: ignore[reportAssignmentType] | ||
|
|
||
| rebuilt = MyState.get_fields()["name"] | ||
| assert rebuilt._opts == {"a": [1]} # pyright: ignore[reportAttributeAccessIssue] | ||
| assert rebuilt._opts is not opts # pyright: ignore[reportAttributeAccessIssue] | ||
| opts["a"].append(2) | ||
| assert rebuilt._opts == {"a": [1]} # pyright: ignore[reportAttributeAccessIssue] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.