Skip to content

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

Closed

Description

@fctl-sh

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:

class Counted(dynamoxy.PydanticModel, abstract=True):
    views: int = 0
    title: str


class Post(Counted, table="posts"):
    post_id: Annotated[str, dynamoxy.PartitionKey()]


Post.model_fields["views"].default  # <attribute Counted.views>, not 0
Post(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.

Found while fixing #79 (fctl session).

Activity

  1. added
    bug
    on Oct 30, 2026
  2. added a commit that references this issue on Oct 31, 2026
    861db75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions