Repository navigation
fix(models): pydantic subclasses of a PydanticModel inherit its fields with their defaults - #115
Merged
Merged
Conversation
…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]>
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 and why
A
PydanticModelthat subclasses anotherPydanticModel(an abstract base with shared fields) got the base's attribute paths as the defaults of every field it inherits. Pydantic readsgetattr(cls, name)for each field while it builds a class, inherited ones included, and the class attribute of a dynamoxy field is itsFieldPath. So required inherited fields became optional,create()raisedValidationError: ... expected int, got FieldPathfor a field with a default,to_item()returned the paths as values, and reading an item without an inherited attribute raisedItemDecodeError. 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.PydanticModelnow 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 checkpasses, and the pydantic and instance test modules pass on Python 3.12 too.Fixes #114
Checklist
make checkpasses locally (lint, types, clippy, consistency, tests; no Rust change)Worth confirming
PydanticModelnow has a metaclass of its own, a subclass of pydantic'sModelMetaclass(imported frompydantic._internal, where it has lived through pydantic 2.x). An application that combinesPydanticModelwith 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
PydanticModela 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 betweenPydanticModelclasses changes behavior.What it does. A
PydanticModelthat subclasses anotherPydanticModelused to get the base's attribute paths as the defaults of the fields it inherits, so required fields became optional,create()andto_item()misbehaved, and items without an inherited attribute could not be read.PydanticModelnow 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.
PydanticModelnow has a metaclass derived from pydantic's privateModelMetaclass, 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