litestar-org/polyfactory

Bug: ParameterError is thrown for valid ranges in conint

jtraub opened this issue · 1 comments

Continued my experiments.

This code

class Test(BaseModel):
    a: conint(gt=10, lt=12)  # type: ignore[valid-type]


class TestFactory(ModelFactory):
    __model__ = Test


result = TestFactory.build()

produces

Traceback (most recent call last):
  File "/tmp/test_proj/test.py", line 24, in <module>
    result = TestFactory.build()
  File "/tmp/test_proj/venv/lib/python3.10/site-packages/pydantic_factories/factory.py", line 716, in build
    kwargs[field_name] = cls.get_field_value(model_field, field_parameters=kwargs.get(field_name, {}))
  File "/tmp/test_proj/venv/lib/python3.10/site-packages/pydantic_factories/factory.py", line 603, in get_field_value
    return cls._handle_constrained_field(model_field=model_field)
  File "/tmp/test_proj/venv/lib/python3.10/site-packages/pydantic_factories/factory.py", line 263, in _handle_constrained_field
    return handle_constrained_int(field=cast("ConstrainedInt", outer_type))
  File "/tmp/test_proj/venv/lib/python3.10/site-packages/pydantic_factories/constraints/integer.py", line 18, in handle_constrained_int
    minimum, maximum = get_constrained_number_range(
  File "/tmp/test_proj/venv/lib/python3.10/site-packages/pydantic_factories/value_generators/constrained_number.py", line 59, in get_constrained_number_range
    raise ParameterError("maximum value must be greater than minimum value")
pydantic_factories.exceptions.ParameterError: maximum value must be greater than minimum value

Instead I expected it to produce 11 which matches the constraints 10 < a < 12 for integer a.

It seems that conint(ge=10, le=10) will throw the same error while it should not because 10 is a valid value.

Yep, definitely a bug

>>> from pydantic import BaseModel, conint
>>> class TestModel(BaseModel):
...     a: conint(gt=10, lt=12)
...     b: conint(ge=10, le=10)
... 
>>> TestModel(a=11, b=10)
TestModel(a=11, b=10)
>>>