Skip to content

feat: let CitrySettings.model_base choose the kind of class plain schemas become - #455

Merged
JuroOravec merged 2 commits into
mainfrom
feat/model-base
Oct 28, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
feat/model-base

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this does

You can now give Citry one base class, and it builds every plain schema declaration as a subclass of it:

class Model(BaseModel):
    model_config = ConfigDict(extra="forbid", strict=True)


app = Citry(model_base=Model)

Each plain class Kwargs: with annotated fields (and likewise TemplateData, JsData, CssData, and Events data classes) then becomes a Pydantic model that subclasses Model, so its model_config and validators apply to all of them, and Pydantic checks the values instead of Citry.

model_base What Citry generates
None (the default) dataclasses, as before
a Pydantic v2 or v1 model class models that subclass it
a dataclass dataclasses that subclass it and copy its options
typing.NamedTuple named tuples

An import string such as "pydantic.BaseModel" also works; Citry(...) imports it when it is built.

This is the first PR for #172. Pydantic State comes in the next one; until then, State follows a dataclass base and stays a dataclass under a Pydantic base.

Classes the setting leaves alone

  • Slots, classes decorated with @dataclass, and models written by hand.
  • A class that is, or inherits, a dataclass or a model. So a child component's class Kwargs(Parent.Kwargs): keeps its parent's kind.
  • Citry's own built-in components, and components from a library installed with register_library(). Their authors wrote and tested those schemas as dataclasses. An application's own subclass of a library component follows the setting when it declares its own plain Kwargs.

How a generated model differs

Pydantic refuses the authored class as a base, so a generated model or named tuple copies the authored class's members instead. Citry resolves each field's annotation in the module of the class that declared it, before it builds the model.

These fail when the component class is defined, and the message names what to change:

  • a copied method that calls super();
  • a field type Pydantic cannot check;
  • an underscore field or field(init=False) under Pydantic;
  • under typing.NamedTuple: a field without a default after one with a default, a default_factory, or a mutable default;
  • State under a frozen dataclass or typing.NamedTuple base;
  • Events data under typing.NamedTuple or a Pydantic v1 base, because Events validation reads only dataclasses and Pydantic v2 models.

CitrySettings(...) refuses a value of another kind, a named tuple class with fields, a dataclass with its own __init__, a Pydantic dataclass, and a Pydantic RootModel.

Changes from the design note

  • Error for clashing bases. The note asked that the error raised when a component's parents declare classes that cannot be combined name both kinds. That error only applies to settings classes such as Events, Lint and extension configs, which model_base never generates, so it is unchanged.
  • Names not yet defined. Under a Pydantic v2 base, a field type that cannot be found when the class is defined is resolved at the first render, so a class defined later in the module works. A name that is still undefined then, such as one imported only under if TYPE_CHECKING:, fails with Pydantic's "not fully defined" error. Under a v1 base, Pydantic needs Kwargs.update_forward_refs() for such names, as for a model written by hand.
  • super() advice. The note suggested super(ClassName, self) for a copied method. That cannot work, because Python ties any method that mentions super, even super(ClassName, self), to the class it was written in. The error and the docs now say to call the parent method directly instead.
  • Events data under Pydantic v1. The note allowed it, but Events validation reads only Pydantic v2 models, so it is refused when the component class is defined.
  • Built-in and library components. These keep their own schemas. The note did not cover them, and applying the setting broke Citry's own i18n component.

For the maintainer to confirm

  • Library components keep the schema kind their author wrote, whatever model_base the installing application sets.

Tests

  • test_model_base.py, 62 tests covering each kind for each schema, every error above, inheritance, library and built-in components, annotation resolution across modules, Const defaults behaving as in a model written by hand, and Events validation.
  • The existing suites pass unchanged.

Part of #172.

🤖 Generated with Claude Code

…emas become

A project can now give every plain schema one base. With
Citry(model_base=Model), a plain class Kwargs: (and TemplateData, JsData,
CssData, State and Events data classes) becomes a class of Model's kind:
a Pydantic v2 or v1 model that subclasses Model, so its model_config and
validators apply everywhere; a dataclass with Model's dataclass options;
or, for typing.NamedTuple, a named tuple. None keeps today's generated
slotted dataclasses. The value is a class or an import string, which
Citry(...) imports when it is built.

A generated model copies the authored class's members, since Pydantic
refuses the authored class as a base. Each field's annotation is read in
the module of the class that wrote it before the model is built. A copied
method that calls super(), a field Pydantic cannot validate, an
underscore field, field(init=False), or a mutable named-tuple default
fails at class setup with a message that names it. State stays a dataclass under a Pydantic base and
fails under a frozen dataclass or NamedTuple base; Events data cannot be
a named tuple or a Pydantic v1 model, which Events validation does not
read. Slots, @DataClass classes, hand-written models, Citry's own
components and components from an installed library are kept as
written, and a child schema keeps its parent's kind.

Part of #172.

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