Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Data generated incorrectly from nested Annotated #507

Closed
1 of 4 tasks
XenoMonk opened this issue Feb 29, 2024 · 4 comments
Closed
1 of 4 tasks

Bug: Data generated incorrectly from nested Annotated #507

XenoMonk opened this issue Feb 29, 2024 · 4 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@XenoMonk
Copy link

XenoMonk commented Feb 29, 2024

Description

I have attempted to make this bug report once from another account: #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 #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

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

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
@XenoMonk XenoMonk added the bug Something isn't working label Feb 29, 2024
@guacs
Copy link
Member

guacs commented Mar 2, 2024

@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.

@guacs guacs closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2024
@guacs guacs added the duplicate This issue or pull request already exists label Mar 2, 2024
@XenoMonk
Copy link
Author

XenoMonk commented Mar 4, 2024

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
Copy link
Member

guacs commented Mar 4, 2024

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
Copy link
Author

XenoMonk commented Mar 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants