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

Factory does not respect randomized collection lengths for optional collection types #403

Closed
1 of 4 tasks
emapple opened this issue Oct 11, 2023 · 2 comments · Fixed by #407
Closed
1 of 4 tasks
Labels
bug Something isn't working

Comments

@emapple
Copy link

emapple commented Oct 11, 2023

Description

When a model has an optional collection attribute, it does not adhere to the randomized collection arguments passed to it, instead using the default length of 1 no matter what.

On a related note -- in the MCVE, removing | None in the type hint will correctly build with a list of random length between the min and max. However, all instances of generated data (via factory.build() or factory.batch()) have the same length. Is there a way to have variable lengths, so each subsequent call has a random, uncorrelated length?

URL to code causing the issue

No response

MCVE

from polyfactory.factories.pydantic_factory import ModelFactory
from pydantic import BaseModel

class Model(BaseModel):
    var: list[str] | None


class Factory(ModelFactory):
    __model__ = Model
    __randomize_collection_length__ = True
    __allow_none_optionals__ = False
    __min_collection_length__ = 2
    __max_collection_length__ = 5

Factory.batch(10)

Output:

# all have length of 1, not respecting constraints
[Model(var=['HbolMJUevblAbkHClEQa']),
 Model(var=['PKriXrefSFPLBYtCRGSE']),
 Model(var=['KclpemfoHstknWHFiLit']),
 Model(var=['ZHCJROlbqnkXTqIuVxnm']),
 Model(var=['DnEUulZAecfGPvdtZBYS']),
 Model(var=['aLJBnHfwxXTnAjVTpHFh']),
 Model(var=['NCOBaqbWTRpdTXezdNNg']),
 Model(var=['yCnTwlCLkqZHwAglrEll']),
 Model(var=['dGlVSdmIlgWDyOPjfwwy']),
 Model(var=['LJZfurSmjxvomWxSCcZR'])]

Steps to reproduce

See MCVE

Screenshots

No response

Logs

No response

Release Version

2.9.0

Platform

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

Note

Check out all issues funded or available for funding here: https://polar.sh/litestar-org

  • 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
@emapple emapple added the bug Something isn't working label Oct 11, 2023
@guacs
Copy link
Member

guacs commented Oct 12, 2023

Thanks for this :)

This is essentially due to these constraints being encoded into the FieldMeta when they're created for each type. This coupling should be removed, and these length constraints should be handled separately within get_field_value.

@emapple
Copy link
Author

emapple commented Oct 17, 2023

Thanks for addressing this!

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
2 participants