Skip to content

feat(events): let State be a Pydantic v2 or v1 model - #461

Merged
JuroOravec merged 1 commit into
mainfrom
feat/pydantic-state
Oct 28, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
feat/pydantic-state

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

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 in events/state.md).

An Events State can now be a Pydantic v2 or v1 model, written by hand or generated from a plain class State: under a Pydantic model_base:

class Filters(Component):
    class State(BaseModel):
        query: str = ""
        page: int = 1
        _public = ("query",)

Pydantic checks the values at three points:

When a value does not fit The call fails with
a call restores State from its token stale_state (409), as for a dataclass State
the browser writes a field invalid_args (422), with Pydantic's message per field
a handler has changed State, before Citry signs it handler_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

  • Field readers. Every place that read State as a dataclass now calls one field reader. That covers the token, the check for whether a handler changed State, the first render, the render cache, bindings, topics and browser writes. Settings such as _storage are read through Pydantic's private attributes, where a typo such as _strorage is still caught.
  • The token. The token holds the model's JSON, by field name, without computed fields. Restoring reads that JSON back by field name (model_validate_json(by_name=True, by_alias=False), or parse_raw in v1). So dates, enums, sets, nested models and tuples round-trip even under strict=True, and an alias never fills another field.
  • Before signing. The check restores exactly what will be signed, adopts Pydantic's converted values (so state.page = "2" is signed as 2), and checks that storing and restoring it again gives the same values, so a value that would change on the next call fails now.
  • Browser writes. Writes are merged into the JSON form, checked the same way, and the whole checked model is adopted, so values a validator derives stay current.
  • Pydantic v1. A v1 nested model is dumped through the declared class, so a subclass instance cannot add undeclared fields to the page.

Refused when the component class is defined

At any depth of nested models:

  • a frozen model or field;
  • exclude or exclude_if;
  • serializers: model_serializer, field_serializer, PlainSerializer, WrapSerializer;
  • secret types such as SecretStr, which Pydantic writes masked;
  • a v1 alias without allow_population_by_field_name;
  • a setting annotated without a value, such as _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 data validation. Events data validation had the same bug before this PR, and this fixes it there too.

Changes from the design note

  • Test layout. The existing State suites are not parametrized in place. A new suite, 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.
  • The token's contents. The token stores the model's JSON rather than raw values, and restore reads it back as JSON. The note's tuple-only conversion did not cover strict dates, enums or nested tuples.
  • Timing. A call that changes a 20-field State takes about 15% longer than with a dataclass State (130 µs against 113 µs, debug native build). Restore keeps validating; the note's fallback, 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 with model_base="pydantic.BaseModel".
  • The citry and LSP suites pass.

Part of #172.

🤖 Generated with Claude Code

Base automatically changed from feat/model-base to main October 28, 2026 10:06
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
JuroOravec merged commit ccf55af into main Oct 28, 2026
20 of 31 checks passed
@JuroOravec
JuroOravec deleted the feat/pydantic-state branch October 28, 2026 10:36
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant