Open
Description
Description
I am using Polyfactory with Pydantic, and it appears that if you create a field with both ge
and multiple_of
set, but not le
or lt
, then Polyfactory will generate values that don't satisfy all the constraints. For example, if you set ge
to 10 and multiple_of
to 3, then Polyfactory appears to always generate a value of 3, which fails to satisfy the >= 10
constraint.
It looks like the bug is in polyfactory.value_generators.constrained_numbers
's generate_constrained_number()
function. In that function, if both minimum
and multiple_of
are set to non-None values, but maximum
is None, then it'll take the earliest exit by immediately returning multiple_of
, even if multiple_of
may not satisfy the minimum
constraint.
URL to code causing the issue
No response
MCVE
from pydantic import BaseModel, Field
from polyfactory.factories.pydantic_factory import ModelFactory
class Foo(BaseModel):
bar: int = Field(ge=10, multiple_of=3)
class FooFactory(ModelFactory[Foo]):
...
FooFactory.build()
Steps to reproduce
1. Install both pydantic and polyfactory
2. Save the code snippet above to a file (e.g. main.py)
3. Execute the file (e.g. `python3 main.py`)
Screenshots
No response
Logs
Traceback (most recent call last):
File "/Users/richardx/tmp/polyfactory-multiple-test/main.py", line 13, in <module>
FooFactory.build()
File "/Users/richardx/tmp/polyfactory-multiple-test/venv/lib/python3.11/site-packages/polyfactory/factories/pydantic_factory.py", line 401, in build
return cls.__model__(**processed_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/richardx/tmp/polyfactory-multiple-test/venv/lib/python3.11/site-packages/pydantic/main.py", line 171, in __init__
self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
bar
Input should be greater than or equal to 10 [type=greater_than_equal, input_value=3, input_type=int]
For further information visit https://errors.pydantic.dev/2.6/v/greater_than_equal
Release Version
4.14.1
Platform
- Linux
- Mac
- Windows
- Other (Please specify in the description above)