litestar-org / polyfactory

Simple and powerful factories for mock data generation
https://polyfactory.litestar.dev/
MIT License
988 stars 78 forks source link

Bug: Validation Error in Pydantic model with optional list #559

Closed RLC92 closed 1 month ago

RLC92 commented 2 months ago

Description

Even the simplest model with an optional list appears to not work. Instead of generating a valid coverage including None and a random string, a validation error is raised. Worth noting that it seems to be making a list of list of str, rather than a list of string.

URL to code causing the issue

https://github.com/RLC92/polyfactory_error_submission

MCVE

from pydantic import BaseModel
from typing import Optional, List, Union # Note: Optional is included as I tried it as well, realize that it is also just a union.
from polyfactory.factories.pydantic_factory import ModelFactory

class ExampleModel(BaseModel):
    values: Union[List[str], None]

class ExampleModelFactory(ModelFactory):
    __model__ = ExampleModel

if __name__ == "__main__":
    print(list(ExampleModelFactory.coverage()))

Steps to reproduce

1. Install dependencies (I'm using 1.10.17 and polyfactory 2.16.0)
2. Run the example.
3. Get a validation failure in coverage:

pydantic.error_wrappers.ValidationError: 1 validation error for ExampleModel
values -> 0
  str type expected (type=type_error.str)

Screenshots

No response

Logs

No response

Release Version

2.16.0

Platform


[!NOTE]
While we are open for sponsoring on GitHub Sponsors and OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.

Fund with Polar

RLC92 commented 2 months ago

Also sorry if the post was a little dorked, I tried to edit it to make it right but I don't report on Github very frequently, hopefully this'll improve with practice.