polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

Bug: Pydantic validation aliases are not handled by coverage

Open vrslev opened this issue 1 year ago • 1 comments

Description

ModelFactory.coverage() fails on models that have validation_alias set

MCVE

import typing

import pydantic
from polyfactory.factories.pydantic_factory import ModelFactory


class C(pydantic.BaseModel):
    a: typing.Annotated[str, pydantic.Field(validation_alias="b")]


class CFactory(ModelFactory[C]):
    pass


for _ in CFactory.coverage():
    pass

Logs

pydantic_core._pydantic_core.ValidationError: 1 validation error for C
b
  Field required [type=missing, input_value={'a': 'FdhLXuyqOfQSYmiqClUe'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.7/v/missing

Release Version

2.16.2

Platform

  • [ ] Linux
  • [X] Mac
  • [ ] Windows
  • [ ] Other (Please specify in the description above)

vrslev avatar Aug 14 '24 11:08 vrslev

Hi vrslev, this may be tricky to support as is given validation_alias may not be a str but an alias path as well. I will have a look to see if this can be done whilst being backwards compatible.

I would suggest setting populate_by_name on the pydantic model as a workaround in the meantime.

adhtruong avatar Oct 02 '24 07:10 adhtruong

Closing in favour of https://github.com/litestar-org/polyfactory/issues/713. This documents workaround and should handle more general case here

adhtruong avatar Jun 14 '25 12:06 adhtruong