litestar-org / polyfactory

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

Bug: Data generated incorrectly from nested Annotated #507

Closed XenoMonk closed 6 months ago

XenoMonk commented 6 months ago

Description

I have attempted to make this bug report once from another account: https://github.com/litestar-org/polyfactory/issues/506 But it is not visible to any other users (or when logged out)

I am attempting to use nested annotations with pydantic FieldInfo on each level.

I have seen https://github.com/litestar-org/polyfactory/issues/434 and for a moment i thought simply updating polyfactory would solve my problem, but alas.

From what i can see these nested annotations are working correctly in pydantic and seems to be the expectation for more complex annotations, such that FieldInfo is applied to the correct "level" of the annotation.

The a and b attributes produce two different errors that seem related.

URL to code causing the issue

No response

MCVE

from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel, Field
from typing import Annotated

def ofloatlist(min_length=None, max_length=None, gt=None, ge=None, lt=None, le=None, **kwargs) -> type[None | list[None | float]]:
    annotation = None | Annotated[list[None | Annotated[float, Field(gt=gt, ge=ge, lt=lt, le=le)]], Field(min_length=min_length, max_length=max_length)]
    return Annotated[annotation, Field(**kwargs)]  # type: ignore

bleh = Annotated[None | list[Annotated[None | Annotated[float, Field(ge=0, le=5)], Field(min_length=6)]], Field(title="meh")]

class MyModel(BaseModel):
    a: ofloatlist(min_length=6, ge=2, le=5, title="Value") = None
    b: bleh = None

class MyModelFactory(ModelFactory):
    __model__ = MyModel
    __random_seed__ = 3

print(MyModelFactory.build())

Steps to reproduce

Run the MCVE

Screenshots

No response

Logs

pydantic_core._pydantic_core.ValidationError: 3 validation errors for MyModel
a.0
  Input should be less than or equal to 5 [type=less_than_equal, input_value=67018326269.9516, input_type=float]
    For further information visit https://errors.pydantic.dev/2.6/v/less_than_equal
a.3
  Input should be less than or equal to 5 [type=less_than_equal, input_value=4563270605912.7, input_type=float]
    For further information visit https://errors.pydantic.dev/2.6/v/less_than_equal
b.0
  Input should be less than or equal to 5 [type=less_than_equal, input_value=60.518708028656015, input_type=float]
    For further information visit https://errors.pydantic.dev/2.6/v/less_than_equal

Release Version

Python: 3.10 Pydantic: 2.6.1 Polyfactory: 2.14.1

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

guacs commented 6 months ago

@XenoMonk thanks for pointing this out!

This is a known bug (#493). #499 has a fix for this and if I run your MCVE on that branch, there are no validation errors. For now, you'll have to downgrade to 2.14.0 since the bug was introduced in 2.14.1.

XenoMonk commented 6 months ago

I agree that the MCVE is solved by downgrading to 2.14.0. But i am still getting similar errors in my production code. I am trying to make a new MCVE showing these in 2.14.0.

guacs commented 6 months ago

I agree that the MCVE is solved by downgrading to 2.14.0. But i am still getting similar errors in my production code. I am trying to make a new MCVE showing these in 2.14.0.

@XenoMonk the fix has been released as part of 2.15.0. It'd be great if you could test it out with that and see if you're still facing any issues.

XenoMonk commented 6 months ago

2.15.0 Seems to solve all the issues i have encountered. Thank you for your quick replies.