Repository navigation
feat: let CitrySettings.model_base choose the kind of class plain schemas become - #455
Merged
Merged
Conversation
…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]>
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
You can now give Citry one base class, and it builds every plain schema declaration as a subclass of it:
Each plain
class Kwargs:with annotated fields (and likewiseTemplateData,JsData,CssData, and Eventsdataclasses) then becomes a Pydantic model that subclassesModel, so itsmodel_configand validators apply to all of them, and Pydantic checks the values instead of Citry.model_baseNone(the default)typing.NamedTupleAn import string such as
"pydantic.BaseModel"also works;Citry(...)imports it when it is built.This is the first PR for #172. Pydantic
Statecomes in the next one; until then,Statefollows 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.class Kwargs(Parent.Kwargs):keeps its parent's kind.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 plainKwargs.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:
super();field(init=False)under Pydantic;typing.NamedTuple: a field without a default after one with a default, adefault_factory, or a mutable default;Stateunder a frozen dataclass ortyping.NamedTuplebase;dataundertyping.NamedTupleor 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 PydanticRootModel.Changes from the design note
Events,Lintand extension configs, whichmodel_basenever generates, so it is unchanged.if TYPE_CHECKING:, fails with Pydantic's "not fully defined" error. Under a v1 base, Pydantic needsKwargs.update_forward_refs()for such names, as for a model written by hand.super()advice. The note suggestedsuper(ClassName, self)for a copied method. That cannot work, because Python ties any method that mentionssuper, evensuper(ClassName, self), to the class it was written in. The error and the docs now say to call the parent method directly instead.dataunder Pydantic v1. The note allowed it, but Events validation reads only Pydantic v2 models, so it is refused when the component class is defined.For the maintainer to confirm
model_basethe 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,Constdefaults behaving as in a model written by hand, and Events validation.Part of #172.
🤖 Generated with Claude Code