litestar-org/polyfactory

ParameterError should be thrown for invalid ranges in condecimal/confloat/conint when multiple_of is supplied

jtraub opened this issue · 0 comments

>>> from pydantic import BaseModel, condecimal
>>> from pydantic_factories import ModelFactory
>>> class A(BaseModel):
...     d: condecimal(le=Decimal(9), ge=Decimal(8.1), multiple_of=Decimal(4))
... 
>>> class AFactory(ModelFactory):
...     __model__ = A
... 
>>> AFactory.build()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/test_proj/venv/lib/python3.10/site-packages/pydantic_factories/factory.py", line 726, in build
    return cast("T", cls.__model__(**kwargs))  # pyright: ignore
  File "pydantic/main.py", line 342, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for A
d
  ensure this value is greater than or equal to 8.0999999999999996447286321199499070644378662109375 (type=value_error.number.not_ge; limit_value=8.0999999999999996447286321199499070644378662109375)

d field should be a decimal that is a multiple of 4 and lies within [8.1; 9] range. Obviously there are no such numbers so the lib should throw ParameterError instead of generating an invalid value and trying to pass to the model here.

If given range is really narrow it would lead to infinite cycle.

>>> class B(BaseModel):
...     d: condecimal(le=Decimal('999999999.9999999343812775'), ge=Decimal('999999999.990476'), multiple_of=Decimal('-0.556'))
...