You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pydantic models: fields inherited from a PydanticModel base get its attribute path as their default #114
Priority: medium. A PydanticModel that subclasses another PydanticModel (an abstract base with shared fields) gets the base class's attribute paths as the defaults of every field it inherits:
classCounted(dynamoxy.PydanticModel, abstract=True):
views: int=0title: strclassPost(Counted, table="posts"):
post_id: Annotated[str, dynamoxy.PartitionKey()]
Post.model_fields["views"].default# <attribute Counted.views>, not 0Post(post_id="p1") # accepted without the required title
await Post.create(client, post_id="p1", title="x") raises ValidationError: Post is invalid: views: expected int, got FieldPath, although views has a default.
Reading an item that lacks an inherited attribute raises ItemDecodeError (views: Input should be a valid integer), so adding a field with a default to the base class makes the stored items unreadable.
Post(post_id="p1").to_item() returns the paths as values ({"views": <attribute Counted.views>, "title": <attribute Counted.title>, ...}).
Native models are not affected, and neither are pydantic models that subclass a plain pydantic.BaseModel, as the pydantic guide shows.
Where:python/dynamoxy/_pydantic.py. PydanticModel.__pydantic_init_subclass__ sets each field's FieldPath as a class attribute. When pydantic builds a subclass, it reads getattr(cls, name) for every annotated field, inherited ones included, gets the base class's path (whose __get__ returns the path on class access) and takes it as the field's default.
Fix: keep the parent's FieldInfo for inherited fields, for example by keeping the paths out of the lookup pydantic does while it builds the subclass, or by restoring the inherited defaults and required state from the base's model_fields. Test a PydanticModel subclass of a PydanticModel with required and defaulted fields on both backends.
Activity
fix(models): pydantic subclasses of a PydanticModel inherit its field…