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: Annotations for nested types are ignored #434

Closed
1 of 4 tasks
kabell opened this issue Nov 7, 2023 · 1 comment · Fixed by #436
Closed
1 of 4 tasks

Bug: Annotations for nested types are ignored #434

kabell opened this issue Nov 7, 2023 · 1 comment · Fixed by #436
Labels
bug Something isn't working

Comments

@kabell
Copy link

kabell commented Nov 7, 2023

Description

When a pydantic model contains a simple annotated attribute, the restrictions from the annotation are used for generating the value.

Example:

Dimension = Annotated[int, Field(gt=1000, lt=2000)]

class Item(BaseModel):
    x: Dimension

class ItemFactory(ModelFactory):
    __model__ = Item

print(ItemFactory.build())
>>> x=1456

However, if I change the Item model to

class Item(BaseModel):
    dimensions: list[Dimension]

I get the following error saying that polyfactory generated the value 8569

>>>
pydantic_core._pydantic_core.ValidationError: 1 validation error for Item
dimensions.0
  Input should be less than 2000 [type=less_than, input_value=8569, input_type=int]
    For further information visit https://errors.pydantic.dev/2.4/v/less_than

URL to code causing the issue

No response

MCVE

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

Dimension = Annotated[int, Field(gt=1000, lt=2000)]

class Item(BaseModel):
    dimensions: list[Dimension]


class ItemFactory(ModelFactory):
    __model__ = Item


print(ItemFactory.build())

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

No response

Release Version

2.11.0

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 Litestar 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
@kabell kabell added the bug Something isn't working label Nov 7, 2023
@guacs
Copy link
Member

guacs commented Nov 11, 2023

@kabell thanks for reporting this :)

I am able to reproduce this but haven't gotten a chance to look into it in detail yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants