Repository navigation
feat(events): let State be a Pydantic v2 or v1 model - #461
Merged
Merged
Conversation
An Events State can now be a Pydantic model, written by hand (class State(BaseModel):) or generated from a plain class State: under a Pydantic model_base. Every place that read State as a dataclass now goes through one field reader, and State settings such as _storage are read from Pydantic's private attributes, where a typo such as _strorage is also caught. Pydantic checks the values when a call restores State (a value that no longer fits answers stale_state, as for a dataclass), when the browser writes a field (invalid_args with Pydantic's message per field), and once after a handler changes State, before Citry signs it, so a wrong assignment or an in-place change that breaks the model fails that call with handler_error and a log naming the component, handler and field. A call that only reads State is not checked again. The token holds the model's JSON by field name and restore reads it back as JSON by field name, so dates, enums, sets, nested models and tuples round-trip even under strict=True, and an alias never fills another field. The check before signing restores what will be signed, adopts Pydantic's converted values, and requires a second round trip to match. A frozen model or field, an excluded field, a serializer, and secret types, at any depth, are refused at class setup. A validator that raises an unexpected exception answers that one call instead of escaping the dispatcher, for State writes and Events data. Part of #172. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
force-pushed
the
feat/pydantic-state
branch
from
October 28, 2026 10:19
f7f9a99 to
215708a
Compare
JuroOravec
added a commit
that referenced
this pull request
Oct 28, 2026
…the v1 subclass fix The #461 record lists the Pydantic v1 nested subclass as fixed during the review, as the lane that built #461 reported it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 28, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What this does
Builds on #455 (
model_base), now merged; rebased onto main. A library component's State keeps the kind its author wrote, as its other schemas do (test and a sentence inevents/state.md).An Events
Statecan now be a Pydantic v2 or v1 model, written by hand or generated from a plainclass State:under a Pydanticmodel_base:Pydantic checks the values at three points:
stale_state(409), as for a dataclass Stateinvalid_args(422), with Pydantic's message per fieldhandler_error(500) in that call, with a log naming the component and handler (and the field, when Pydantic reports one)A call that only reads State is not checked again.
How State is stored
_storageare read through Pydantic's private attributes, where a typo such as_strorageis still caught.model_validate_json(by_name=True, by_alias=False), orparse_rawin v1). So dates, enums, sets, nested models and tuples round-trip even understrict=True, and an alias never fills another field.state.page = "2"is signed as2), and checks that storing and restoring it again gives the same values, so a value that would change on the next call fails now.Refused when the component class is defined
At any depth of nested models:
excludeorexclude_if;model_serializer,field_serializer,PlainSerializer,WrapSerializer;SecretStr, which Pydantic writes masked;allow_population_by_field_name;_public: tuple.Validator bugs
A validator that raises something other than a validation error now fails only that call and is logged: 500 on restore, 422 on a browser write or on Events
datavalidation. Eventsdatavalidation had the same bug before this PR, and this fixes it there too.Changes from the design note
test_pydantic_state.py, runs the shared State behaviors against four kinds (generated dataclass, hand-written v2, hand-written v1, generated v2) and covers every model-specific case the note lists.model_construct, was not needed.Tests
test_pydantic_state.py: 65 tests.test_vue_state_refresh_e2e.py: the State refresh browser test also runs withmodel_base="pydantic.BaseModel".Part of #172.
🤖 Generated with Claude Code