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

fix: Adjust min/max items to valid lengths for Set[Enum] fields #567

Merged
merged 8 commits into from
Sep 13, 2024

Conversation

adrianeboyd
Copy link
Contributor

Description

For Set[Enum] fields with a limited maximum length, adjust min_items and max_items to be within the range of valid lengths.

Closes

Fixes #565

@adrianeboyd adrianeboyd requested a review from guacs as a code owner July 19, 2024 11:53
@adrianeboyd
Copy link
Contributor Author

The question from #565 about whether this should raise an error instead or warn in addition remains open.

@adrianeboyd adrianeboyd changed the title fix: Adjust min/max item to valid lengths for Set[Enum] fields fix: Adjust min/max items to valid lengths for Set[Enum] fields Jul 19, 2024
@adrianeboyd adrianeboyd marked this pull request as draft July 19, 2024 12:12
For `Set[Enum]` fields with a limited maximum length, adjust `min_items`
and `max_items` to be within the range of valid lengths.
@adrianeboyd
Copy link
Contributor Author

I am not really sure whether this is sufficient for handling all possible ways of defining fields with enums? Or whether isinstance(..., EnumMeta) is the best way to check?

@adrianeboyd adrianeboyd marked this pull request as ready for review July 19, 2024 12:25
@guacs
Copy link
Member

guacs commented Jul 20, 2024

What if we just don't handle this? Instead, we could just raise an exception telling the user the minimum is too large. It might be due to the default set by polyfactory, but this way we can tell the user to change it themselves. I don't really like the idea of us just changing a value without them knowing. I don't think it would be a breaking change either since it currently results in an infinite loop anyway.

tests/test_complex_types.py Outdated Show resolved Hide resolved
@adrianeboyd
Copy link
Contributor Author

Thanks for the review! I think I addressed all the individual suggestions, although handling Literal seemed clunkier than I expected, so maybe there's a better way to do that?

In terms of raising an error or not, I'm not particularly familiar with polyfactory so it might just be my understanding, but it seems difficult to configure the min/max length on a per-item basis. How would you (easily) set the intended lengths for a model like this?

class WithSetEnum(BaseModel):
    letters: Set[Enum("Letters", ["a", "b", "c", "d", "e", "f"])]
    colors: Set[Enum("Colors", ["red", "green"])]

@guacs
Copy link
Member

guacs commented Jul 23, 2024

Thanks for the review! I think I addressed all the individual suggestions, although handling Literal seemed clunkier than I expected, so maybe there's a better way to do that?

In terms of raising an error or not, I'm not particularly familiar with polyfactory so it might just be my understanding, but it seems difficult to configure the min/max length on a per-item basis. How would you (easily) set the intended lengths for a model like this?

class WithSetEnum(BaseModel):
    letters: Set[Enum("Letters", ["a", "b", "c", "d", "e", "f"])]
    colors: Set[Enum("Colors", ["red", "green"])]

Yeah this is a bit tricky since you'd have to set the min val as 0 and max val as 2. I'm not sure what the best way to go about this would be. You could also have a classmethod for generating the values for that specific field and manually call handle_constrained_collection.

Copy link
Member

@guacs guacs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @adrianeboyd, sorry for the super late reply! I think if we can fix those things regarding the Literal, then this is good to go :)

guacs and others added 4 commits August 25, 2024 07:48
- `min_items` should be at most the number of possible values
- `max_items` for `Literal` should be the number of possible values
- `max_items` for both `Enum` and `Literal` shouldn't clobber a provided
  `max_items` that's lower than the number of possible values
- run the sanity check for `min_items <= max_items` before any values
  are modified
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/567

Copy link
Member

@guacs guacs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrianeboyd so sorry for keeping you waiting! Life got in the way and I've been super busy. All the changes look good to me :) Thanks so much!

@guacs guacs enabled auto-merge (squash) September 13, 2024 14:32
@guacs guacs merged commit 9a83ad6 into litestar-org:main Sep 13, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Set[Enum] hangs for too large collection length
2 participants