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: RecursionError With constrained 0 length lists #566

Closed
3 of 4 tasks
tom300z opened this issue Jul 16, 2024 · 1 comment · Fixed by #570
Closed
3 of 4 tasks

Bug: RecursionError With constrained 0 length lists #566

tom300z opened this issue Jul 16, 2024 · 1 comment · Fixed by #570
Labels
bug Something isn't working

Comments

@tom300z
Copy link

tom300z commented Jul 16, 2024

Description

When constraining a list to be empty (max_length=0):

from pydantic import BaseModel, Field
from polyfactory.factories.pydantic_factory import ModelFactory
class TestModel(BaseModel):
    empty_list_field: list = Field(default=[], max_length=0)

class TestModelFactory(ModelFactory):
    __model__ = TestModel

TestModelFactory.build()

a recursion error occurs.

The problem seems to be in

length = factory.__random__.randint(min_items, max_items) or 1

where a default length of 1 is used if the randomly picked length (always 0) is falsy. @Goldziher do you remember why this default exists?

URL to code causing the issue

No response

MCVE

from pydantic import BaseModel, Field
from polyfactory.factories.pydantic_factory import ModelFactory
class TestModel(BaseModel):
    empty_list_field: list = Field(default=[], max_length=0)

class TestModelFactory(ModelFactory):
    __model__ = TestModel

TestModelFactory.build()

Steps to reproduce

1. Install polyfactory & pydantic (v2)
2. Run example code

Screenshots

No response

Logs

No response

Release Version

v2.16.2

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

guacs commented Jul 20, 2024

@tom300z thanks for reporting this! I checked, and I couldn't really see a good reason to have it default to 1. Some of the tests check that the length of the collection from handle_constrained_collection is greater than 0, but I think that's incorrect. It should be checking that the length is greater than or equal to 0. If I make that change, the tests are all passing as well. So I think it should be fine to not default to 1 even if the random length we get is 0.

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