polyfactory
polyfactory copied to clipboard
Bug: Pydantic validation aliases are not handled by coverage
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)
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.
Closing in favour of https://github.com/litestar-org/polyfactory/issues/713. This documents workaround and should handle more general case here