litestar-org / polyfactory

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

Bug: Set[Enum] hangs for too large collection length #565

Open adrianeboyd opened 1 month ago

adrianeboyd commented 1 month ago

Description

A model using Set[Enum] hangs when the collection length exceeds the size of the enum.

The example below is intended to be reproducible, I think you'd run into it more typically in practice with a randomized collection length and a (default) max collection length that's larger than the size of the set.

URL to code causing the issue

No response

MCVE

from enum import Enum
from typing import Set

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

class WithSetEnum(BaseModel):
    things: Set[Enum("Thing", ["a", "b", "c"])]

class WithSetEnumFactory(ModelFactory):
    __model__ = WithSetEnum
    __randomize_collection_length__ = True
    __min_collection_length__ = 4

WithSetEnumFactory.build()

Steps to reproduce

No response

Screenshots

No response

Logs

No response

Release Version

2.16.2

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

Alc-Alc commented 1 month ago

As you have identified, this only happens when a set is used with enums and if the expected length of this set is higher than the enum members (it will never find find elements to satisfy this, so it keeps looping). What can be the expected behavior here?

If __min_collection_length__ > len(finite_members) and if "container is a type that guarantees uniqueness and the possible elements themselves are finite", I can think of two ways.

adrianeboyd commented 1 month ago

With the combination of the default max collection length (that you can't differentiate from a user-specified value?) and the fact that it seems difficult to set max length values for individual fields, I would think all enum values (+ potentially a warning, but the library doesn't seem to use warnings much) is better than an exception?