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: constraints not working in pydantic v2 (when a Union is used) #317

Closed
AIGeneratedUsername opened this issue Aug 1, 2023 · 2 comments · Fixed by #355
Closed

Bug: constraints not working in pydantic v2 (when a Union is used) #317

AIGeneratedUsername opened this issue Aug 1, 2023 · 2 comments · Fixed by #355
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@AIGeneratedUsername
Copy link

AIGeneratedUsername commented Aug 1, 2023

Preface

There was a confirmed bug #307 but it was not completely fixed.

Using Annotated was fixed but using Annotated[<type>, <...>] | None still fails.

Reproducible sample

from typing import Annotated
from annotated_types import Ge, Le
from pydantic import BaseModel

LongLat = tuple[Annotated[float, Ge(-180), Le(180)], Annotated[float, Ge(-90), Le(90)]]


class Location(BaseModel):
    long_lat: LongLat | None


from polyfactory.factories.pydantic_factory import ModelFactory


class LocationFactory(ModelFactory[Location]):
    __model__ = Location


# Run multiple times because we have a union of types and we must trigger a not-None option.
for _ in range(100):
    LocationFactory.build()

Error output

long_lat.0
  Input should be greater than or equal to -180 [type=greater_than_equal, input_value=-7760968059339.52, input_type=float]
    For further information visit https://errors.pydantic.dev/2.1/v/greater_than_equal
long_lat.1
  Input should be less than or equal to 90 [type=less_than_equal, input_value=7865.80622030079, input_type=float]
    For further information visit https://errors.pydantic.dev/2.1/v/less_than_equal

Versions

Name: polyfactory 
Version: 2.7.0

Funding

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
Fund with Polar
@Goldziher
Copy link
Contributor

Is this still happening?

@Goldziher Goldziher added bug Something isn't working help wanted Extra attention is needed good first issue Good for newcomers labels Aug 13, 2023
@AIGeneratedUsername
Copy link
Author

Is this still happening?

Yes, I just verified on the "main" branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
2 participants