Support TypedDict form data in on_submit#6301
Support TypedDict form data in on_submit#6301GautamBytes wants to merge 2 commits intoreflex-dev:mainfrom
Conversation
Signed-off-by: Gautam Manchandani <gautammanch@Gautams-MacBook-Air.local>
Greptile SummaryThis PR adds
Key changes:
Confidence Score: 5/5Safe to merge — all remaining findings are non-blocking style and defensive-coding suggestions with no impact on correctness or existing behaviour The implementation is well-structured and thoroughly tested. The four findings are all P2: a redundant dead-code import block, a narrow exception catch that could be broadened for defensive safety, a deliberate (though undocumented) global relaxation of Mapping→TypedDict compatibility, and a minor double traversal of form refs. None of these affect runtime correctness or existing users. The runtime payload shape is explicitly unchanged, backward compatibility is preserved, and the new validation only fires at construction time with a clear error message. packages/reflex-base/src/reflex_base/event/init.py — redundant TYPE_CHECKING block at the bottom and the global scope of the Mapping relaxation are worth a second look before merge
|
| Filename | Overview |
|---|---|
| packages/reflex-components-core/src/reflex_components_core/el/elements/forms.py | Core change: adds TypedDict field validation at form construction time, new mapping submit event overload, and several helper functions for static field discovery — well-structured but has a subtle gap where get_type_hints only catches NameError |
| packages/reflex-base/src/reflex_base/event/init.py | Adds _is_mapping_style_event_arg_compatible_with_typed_dict to relax type checks globally for Mapping→TypedDict compatibility; moves BASE_STATE TypeVar to top but leaves a now-redundant if TYPE_CHECKING import block at the bottom |
| tests/units/components/forms/test_form.py | Comprehensive new test suite covering acceptance, optional fields, extra fields, bound-arg resolution, missing-field errors, and dynamic-name bypass — good coverage of the happy and error paths |
| pyi_hashes.json | Hash update for the changed forms.py stub — routine maintenance change |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Form.create"] --> B{on_submit in props?}
B -- No --> C[Assign prevent_default]
B -- Yes --> D[Build form component]
C --> D
D --> E[_validate_on_submit_typed_dict_fields]
E --> F{on_submit is EventChain?}
F -- No --> Z[Skip validation]
F -- Yes --> G{Any non-EventSpec events?}
G -- Yes --> Z
G -- No --> H[_resolve_on_submit_typed_dict_contract]
H --> I{form_data arg found?}
I -- No --> Z
I -- Yes --> J[get_type_hints on handler func]
J --> K{annotation is TypedDict?}
K -- No --> Z
K -- Yes --> L[_get_static_form_field_keys]
L --> M[Collect id-backed refs]
M --> N[Scan children for static name props]
N --> O{Any dynamic Var identifiers?}
O -- Yes --> Z
O -- No --> P{Missing required keys?}
P -- No --> Z[Validation passes]
P -- Yes --> Q[Raise EventHandlerValueError]
Reviews (1): Last reviewed commit: "Support TypedDict form submit data" | Re-trigger Greptile
All Submissions:
Type of change
New Feature Submission:
Changes To Core Features:
Description
This PR adds
TypedDictsupport foron_submitform data handlers while keeping existingdict[str, Any]anddict[str, str]handlers working as before.What changed:
on_submithandlers annotated with a concreteTypedDictTypedDictkeys against statically knowable form fields at form construction timenamefields and existing id-backed form refs in the validation setThis improves:
closes #6264