Support TypedDict form data in on_submit#6301
Conversation
Signed-off-by: Gautam Manchandani <gautammanch@Gautams-MacBook-Air.local>
Merging this PR will not alter performance
Comparing Footnotes
|
Greptile SummaryThis PR adds compile-time
Confidence Score: 4/5The change is additive and guarded by well-tested escape hatches; existing submit handlers are unaffected and the new TypedDict path is skipped whenever the form chain is opaque or the form carries an id. The validation logic and event-type compatibility helper are correct for all documented cases. Two code-clarity observations exist: contracts collected before an opaque event are silently discarded (intentional but undocumented at the return site), and only the first failing contract is surfaced per form. Neither represents wrong runtime behaviour, but they could confuse future maintainers or users debugging multi-handler forms. forms.py (_validate_on_submit_typed_dict_fields) — the contract-collection loop's early-return and single-error-raise patterns would benefit from inline comments. Important Files Changed
Reviews (3): Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." | Re-trigger Greptile |
|
Can you please add these tests as well and fix the issue? def test_on_submit_accepts_typed_dict_with_inherited_optional_fields():
"""Inherited optional TypedDict keys should remain optional."""
class BaseSignupData(TypedDict, total=False):
nickname: str
class SignupData(BaseSignupData):
email: str
class SignupState(rx.State):
@rx.event
def on_submit(self, form_data: SignupData):
pass
form = HTMLForm.create(
Input.create(name="email"),
on_submit=SignupState.on_submit,
)
assert isinstance(form.event_triggers["on_submit"], EventChain)def test_on_submit_accepts_controls_associated_via_form_attribute():
"""Controls associated via the HTML form attribute should not fail validation."""
class SignupData(TypedDict):
email: str
class SignupState(rx.State):
@rx.event
def on_submit(self, form_data: SignupData):
pass
form = HTMLForm.create(
id="signup",
on_submit=SignupState.on_submit,
)
Input.create(name="email", form="signup")
assert isinstance(form.event_triggers["on_submit"], EventChain) |
Use __required_keys__ for inherited TypedDict optional fields, skip validation for forms with id (HTML form attribute), replace stringly-typed control detection with _is_form_control marker, use concrete Mapping type in event spec, narrow exception handling, and add integration tests.
On 3.10, typing.TypedDict ignores typing_extensions.NotRequired when populating __required_keys__. Fall back to annotation inspection to subtract NotRequired fields on older Python versions.
…tamBytes/reflex into feat/support-typeddict-forms
Unit tests now pair happy paths with failing counterparts to prove validation is active. Integration test carries input/expected data per variant instead of fragile runtime app_source detection.
masenf
left a comment
There was a problem hiding this comment.
update from main.
this is a nice feature. i think the new iter_form_fields functionality could be used to replace _get_all_refs in the Form implementation which is the only place where that is really used. We could get rid of that relatively shaky method.
will also have to make sure this continues to work when the form's children are memoized components (have event handlers or other state associated with them). update the integration test to make sure at least one of the fields depends on state and thus will get auto-memoized.
|
@greptile-apps re-review this pr |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
…ict-forms # Conflicts: # pyi_hashes.json
these subpackages depend on new structures in reflex-base these dev package mins must be replaced before the final release
|
This is basically ready to go, but because of the in-flight min-dep issue in pyproject.toml, i'll be submitting a followup PR to relax the must-be-on-pypi requirement for deps that have a Then we'll have another CI update that fails to publish final releases if any pins are still at .dev versions |
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
fixes ENG-9259