Skip to content

Pydantic models: use_enum_values, forward references, RootModel fields and default factories with data fail #78

Closed

Description

@fctl-sh

Priority: medium. Four ways that pydantic models which pydantic itself accepts fail in dynamoxy:

  • model_config = ConfigDict(use_enum_values=True): create() raises AttributeError: 'str' object has no attribute 'value' (codecs.py, the enum codec calls value.value on what pydantic already turned into the plain value).
  • Forward references: shipping: Address | None with Address declared below the model. create_tables(Order) and Order.create(client, ...) raise TypeError: cannot store ... ForwardRef('Address | None'): _pydantic.py reads model_fields before pydantic has rebuilt the model, and nothing calls model_rebuild(). The schema.py module docstring says models may refer to classes defined later.
  • Nested RootModel fields are written as {"root": [...]}, and reading them raises ItemDecodeError ("Input should be a valid list").
  • A default_factory that takes the validated data (pydantic 2.10+) raises a raw TypeError when an item without that attribute is read (schema.py, FieldSpec.initial() calls the factory without arguments).

Fix: handle each in _pydantic.py/codecs.py: accept plain values when use_enum_values is set, call model_rebuild() (or resolve annotations) before building the schema, encode and decode RootModel by its root, and call data-taking factories through pydantic.

Found in a review of the library for bugs (fctl session); the fixes that fit in its ten pull requests are #61 to #65, #67 to #70 and #72.

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