litestar-org/polyfactory

Bug: When setting size=0, 1 instance is created and not put in a list

Danielius5 opened this issue · 1 comments

Hello, I am not sure if this is intended behavior or undefined behavior, but using Fixture with size=random.randint(0, 200) has caused us a bit of trouble.

Instead of giving back an empty list (what we assumed), when size=0, Fixture still returns 1 instance of the model and does not put it in the list. I created a sample example code of what we have:

from pydantic import BaseModel
from pydantic_factories import ModelFactory
from pydantic_factories.fields import Fixture
from pydantic_factories.plugins.pytest_plugin import register_fixture


class TestSubModel(BaseModel):
    ...

class TestModel(BaseModel):
    submodels: list[TestSubModel]

@register_fixture
class TestSubModelFactory(ModelFactory[TestSubModel]):
    __model__ = TestSubModel

@register_fixture
class TestModelFactory(ModelFactory[TestModel]):
    __model__ = TestModel

    submodels = Fixture(TestSubModelFactory, size=1) # change to 0 to get test failing


def test_factory(test_model_factory: TestModelFactory):
    TestModel.parse_obj(test_model_factory.build().dict())

when changed 1 to 0 you should get:

>   ???
E   pydantic.error_wrappers.ValidationError: 1 validation error for TestModel
E   submodels
E     value is not a valid list (type=type_error.list)

To my understanding, allowing to return an empty list on size=0 would be very useful as it would allow to check the edge case of no submodels. Maybe we are doing something wrong?

Fund with Polar

Hi. Yes, this seems to be a bug. I'll include a fix for this in 2.0