Skip to content

Bug: ModelFactory.coverage doesn't respect constraints on Decimal #769

@bullfest

Description

@bullfest

Description

Using MyFactory.build() works fine, but when using MyFactory.coverage() then the annotated pydantic.Field constraint is ignored.

MCVE

from typing import Annotated
from decimal import Decimal
import pydantic
from polyfactory.factories.pydantic_factory import ModelFactory
class Model(pydantic.BaseModel):
    d:  Annotated[Decimal, pydantic.Field(max_digits=18, decimal_places=4)] | None

class MyFactory(ModelFactory[Model]):
    __check_model__ = True

list(MyFactory.coverage())

Logs

pydantic_core._pydantic_core.ValidationError: 1 validation error for Model

  Decimal input should have no more than 18 digits in total [type=decimal_max_digits, input_value=Decimal('-6874602807633095298158368109.83'), input_type=Decimal]
    For further information visit https://errors.pydantic.dev/2.12/v/decimal_max_digits

Release Version

polyfactory==2.22.3
pydantic==2.12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions