litestar-org / polyfactory

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

Bug: Pydantic fields with constraints and multiple type hints cause `ParameterException` #490

Closed berkaycagir closed 7 months ago

berkaycagir commented 7 months ago

Description

One of our pydantic models has the following field definition among others:

property: StrictInt | StrictFloat = Field(le=1000000)

In polyfactory 2.13.0, a boilerplate factory for this model worked fine. However, with the upgrade to 2.14.0, we started seeing the exception in the title.

URL to code causing the issue

No response

MCVE

from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import (
    BaseModel,
    Field,
    StrictFloat,
    StrictInt,
)

class Config(BaseModel):
    property: StrictInt | StrictFloat = Field(le=1000000)

class ConfigFactory(ModelFactory[Config]):
    ...

if __name__ == "__main__":
    ConfigFactory.build()

Steps to reproduce

1. Install packages pydantic~=2.5.3 and polyfactory==2.14.0
2. Execute the file with python
3. See error

Screenshots

No response

Logs

Traceback (most recent call last):
  File "/Users/berkaycagir/test.py", line 19, in <module>
    ConfigFactory.build()
  File "/Users/berkaycagir/.pyenv/versions/test-env/lib/python3.12/site-packages/polyfactory/factories/pydantic_factory.py", line 385, in build
    processed_kwargs = cls.process_kwargs(**kwargs)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/berkaycagir/.pyenv/versions/test-env/lib/python3.12/site-packages/polyfactory/factories/base.py", line 955, in process_kwargs
    field_result = cls.get_field_value(
                   ^^^^^^^^^^^^^^^^^^^^
  File "/Users/berkaycagir/.pyenv/versions/test-env/lib/python3.12/site-packages/polyfactory/factories/base.py", line 694, in get_field_value
    return cls.get_constrained_field_value(annotation=unwrapped_annotation, field_meta=field_meta)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/berkaycagir/.pyenv/versions/test-env/lib/python3.12/site-packages/polyfactory/factories/pydantic_factory.py", line 368, in get_constrained_field_value
    return super().get_constrained_field_value(annotation, field_meta)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/berkaycagir/.pyenv/versions/test-env/lib/python3.12/site-packages/polyfactory/factories/base.py", line 660, in get_constrained_field_value
    raise ParameterException(msg)
polyfactory.exceptions.ParameterException: received constraints for unsupported type typing.Union[typing.Annotated[int, Strict(strict=True)], typing.Annotated[float, Strict(strict=True)]]

Release Version

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 7 months ago

@berkaycagir thanks for reporting this! I can reproduce this, and it most likely has to do with the changes introduced in #468 which slightly changed how unions are handled.