polyfactory
polyfactory copied to clipboard
Bug: Set[Enum] hangs for too large collection length
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
- [X] 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.
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.
- Throw an exception
- Return all the elements of the enum in the resulting set (I can see this as a similar behavior to python sets
{1, 1, 1}does not error out, it is the same as{1})
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?