Skip to content

fix(models): pydantic subclasses of a PydanticModel inherit its fields with their defaults - #115

Merged
kalaspuff merged 1 commit into
mainfrom
issues-stack/1-pydantic-inherited-fields
Oct 31, 2026
Merged

kalaspuff merged 1 commit into
mainfrom
issues-stack/1-pydantic-inherited-fields

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What and why

A PydanticModel that subclasses another PydanticModel (an abstract base with shared fields) got the base's attribute paths as the defaults of every field it inherits. Pydantic reads getattr(cls, name) for each field while it builds a class, inherited ones included, and the class attribute of a dynamoxy field is its FieldPath. So required inherited fields became optional, create() raised ValidationError: ... expected int, got FieldPath for a field with a default, to_item() returned the paths as values, and reading an item without an inherited attribute raised ItemDecodeError. Redeclaring an inherited field (to give it another default) also warned that it "shadows an attribute in parent", which fails test suites that turn warnings into errors.

PydanticModel now uses a subclass of pydantic's metaclass that hides the field paths from class access while pydantic builds a class (in a context variable, so other threads and tasks are unaffected), and pydantic copies the base's fields as it does for plain models. Paths answer again from __pydantic_init_subclass__ on, so hooks of subclasses can use them.

Verified with an integration test on the in-memory backend and MiniStack (defaults and required state, to_item(), create(), increments through the subclass's and the base's paths, an item stored without the inherited attribute, redeclared fields, a filter) and a unit test that paths answer in subclass hooks and after a class definition fails. Both fail without the fix. make check passes, and the pydantic and instance test modules pass on Python 3.12 too.

Fixes #114

Checklist

  • Tests cover the change (in-memory backend and MiniStack)
  • Docs updated (pydantic guide, changelog)
  • make check passes locally (lint, types, clippy, consistency, tests; no Rust change)
  • Breaking change? No

Worth confirming

  • Decision: PydanticModel now has a metaclass of its own, a subclass of pydantic's ModelMetaclass (imported from pydantic._internal, where it has lived through pydantic 2.x). An application that combines PydanticModel with its own pydantic metaclass would now have to derive that metaclass from dynamoxy's; restoring the inherited fields after pydantic built the class would avoid that, but rebuilds every subclass's schema and cannot prevent the "shadows an attribute" warning.

fctl session


Note

Medium risk. It gives every PydanticModel a metaclass of its own and changes how the fields of pydantic subclasses are collected, so all pydantic model classes are built through new code, though only inheritance between PydanticModel classes changes behavior.

What it does. A PydanticModel that subclasses another PydanticModel used to get the base's attribute paths as the defaults of the fields it inherits, so required fields became optional, create() and to_item() misbehaved, and items without an inherited attribute could not be read. PydanticModel now uses a subclass of pydantic's metaclass that hides the field paths from class access, using a context variable, only while pydantic builds a class, so pydantic copies the inherited fields with their defaults and required state, and redeclaring a field no longer warns about shadowing. The change comes with integration tests on the memory backend and MiniStack, a unit test for hooks and failed class definitions, a guide paragraph and a changelog entry.

Own metaclass for PydanticModel. PydanticModel now has a metaclass derived from pydantic's private ModelMetaclass, so an application that combines it with its own pydantic metaclass has to derive that metaclass from dynamoxy's.

fctl review's first look at 29094cb · 5 files, +125 −3 · Review agent settings

…s with their defaults

Pydantic reads getattr(cls, name) for every field while it builds a
class, inherited ones included, and took the base's FieldPath (the class
attribute of each field) as the default of the inherited field. Required
inherited fields became optional, create() failed with "expected int,
got FieldPath", to_item() returned paths, and items without an inherited
attribute could not be read. Redeclaring an inherited field warned that
it "shadows an attribute in parent".

PydanticModel now uses a subclass of pydantic's metaclass that hides the
paths from class access while pydantic builds a class, so pydantic
copies the base's fields, as it does for plain models.

Fixes #114

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff added this pull request to stack #124 October 31, 2026 01:30
@kalaspuff
kalaspuff merged commit 861db75 into main Oct 31, 2026
22 checks passed
@kalaspuff
kalaspuff deleted the issues-stack/1-pydantic-inherited-fields branch October 31, 2026 01:34
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.

Pydantic models: fields inherited from a PydanticModel base get its attribute path as their default

1 participant