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: Set[Enum] hangs for too large collection length #565

Closed
1 of 4 tasks
adrianeboyd opened this issue Jul 12, 2024 · 2 comments · Fixed by #567
Closed
1 of 4 tasks

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

adrianeboyd opened this issue Jul 12, 2024 · 2 comments · Fixed by #567
Labels
bug Something isn't working

Comments

@adrianeboyd
Copy link
Contributor

adrianeboyd commented Jul 12, 2024

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

  • 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
@adrianeboyd adrianeboyd added the bug Something isn't working label Jul 12, 2024
@Alc-Alc
Copy link
Contributor

Alc-Alc commented Jul 12, 2024

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})

@adrianeboyd
Copy link
Contributor Author

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?

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