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 adrianeboyd force-pushed the fix-set-enum-length branch from 3cac77f to 7cd5a7e Compare July 19, 2024 12:14
@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
github-merge-queue bot pushed a commit to mozilla/experimenter that referenced this pull request Sep 24, 2024
Bumps [polyfactory](https://github.com/litestar-org/polyfactory) from
2.16.2 to 2.17.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/litestar-org/polyfactory/releases">polyfactory's
releases</a>.</em></p>
<blockquote>
<h2>v2.17.0</h2>
<h1>Sponsors 🌟</h1>
<p>Thanks to these incredible business sponsors:</p>
<p><a href="https://scalar.com/">Scalar</a> (<a
href="https://github.com/scalar"><code>@​scalar</code></a>), <a
href="https://telemetrysports.com/">Telemetry Sports</a> (via <a
href="https://github.com/chris-telemetry"><code>@​chris-telemetry</code></a>),
<a href="https://www.stok.kr/">Stok</a> (<a
href="https://github.com/stok-team"><code>@​stok-team</code></a>)</p>
<p>A huge 'Thank you!' to all other sponsors across <a
href="https://polar.sh/litestar-org">Polar.sh</a>, <a
href="https://opencollective.com/litestar">OpenCollective</a> and <a
href="https://github.com/sponsors/litestar-org/">GitHub
Sponsors</a>!</p>
<h2>What's Changed</h2>
<ul>
<li>fix: constrained 0 length lists by <a
href="https://github.com/marcozzxx810"><code>@​marcozzxx810</code></a>
in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/570">litestar-org/polyfactory#570</a></li>
<li>build: update pre-commit dependencies by <a
href="https://github.com/adhtruong"><code>@​adhtruong</code></a> in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/571">litestar-org/polyfactory#571</a></li>
<li>fix: use provider map for any in coverage by <a
href="https://github.com/adhtruong"><code>@​adhtruong</code></a> in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/574">litestar-org/polyfactory#574</a></li>
<li>fix: tuple randomized length by <a
href="https://github.com/adhtruong"><code>@​adhtruong</code></a> in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/573">litestar-org/polyfactory#573</a></li>
<li>feat(<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/579">#579</a>):
enhance register_fixture return type annotation by <a
href="https://github.com/giulioindev"><code>@​giulioindev</code></a> in
<a
href="https://redirect.github.com/litestar-org/polyfactory/pull/581">litestar-org/polyfactory#581</a></li>
<li>docs: swap word with its antonym to match the context by <a
href="https://github.com/UncleGoogle"><code>@​UncleGoogle</code></a> in
<a
href="https://redirect.github.com/litestar-org/polyfactory/pull/575">litestar-org/polyfactory#575</a></li>
<li>fix: Adjust min/max items to valid lengths for Set[Enum] fields by
<a href="https://github.com/adrianeboyd"><code>@​adrianeboyd</code></a>
in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/567">litestar-org/polyfactory#567</a></li>
<li>fix: handle Optional type recursive models by <a
href="https://github.com/adhtruong"><code>@​adhtruong</code></a> in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/584">litestar-org/polyfactory#584</a></li>
<li>fix: handle recursive collections by <a
href="https://github.com/adhtruong"><code>@​adhtruong</code></a> in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/587">litestar-org/polyfactory#587</a></li>
<li>chore(release): bump release version by <a
href="https://github.com/adhtruong"><code>@​adhtruong</code></a> in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/588">litestar-org/polyfactory#588</a></li>
</ul>
<h2>New Contributors</h2>
<ul>
<li><a
href="https://github.com/marcozzxx810"><code>@​marcozzxx810</code></a>
made their first contribution in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/570">litestar-org/polyfactory#570</a></li>
<li><a
href="https://github.com/giulioindev"><code>@​giulioindev</code></a>
made their first contribution in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/581">litestar-org/polyfactory#581</a></li>
<li><a
href="https://github.com/UncleGoogle"><code>@​UncleGoogle</code></a>
made their first contribution in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/575">litestar-org/polyfactory#575</a></li>
<li><a
href="https://github.com/adrianeboyd"><code>@​adrianeboyd</code></a>
made their first contribution in <a
href="https://redirect.github.com/litestar-org/polyfactory/pull/567">litestar-org/polyfactory#567</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a
href="https://github.com/litestar-org/polyfactory/compare/v2.16.2...v2.17.0">https://github.com/litestar-org/polyfactory/compare/v2.16.2...v2.17.0</a></p>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/litestar-org/polyfactory/blob/main/docs/changelog.rst">polyfactory's
changelog</a>.</em></p>
<blockquote>
<h1>=========
Changelog</h1>
<p>All commits to this project will be documented in this file.</p>
<h2>[unreleased]</h2>
<p><code>67c5720
&lt;https://github.com/litestar-org/polyfactory/commit/67c57208de5ce993bdb2c7888864ac4e71964511&gt;</code>_
is the last commit in the changelog before unreleased changes.</p>
<p>Bug Fixes

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>(<code>24701eb
&lt;https://github.com/litestar-org/polyfactory/commit/24701eb593493f7c038709b1327dbf6dd8942e87&gt;</code><em>)
- Constrained 0 length lists (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/570">#570</a>)
by <code>@marcozzxx810
&lt;https://github.com/marcozzxx810&gt;</code></em> in
<code>[#570](litestar-org/polyfactory#570)
&lt;https://github.com/litestar-org/polyfactory/pull/570&gt;</code>_</li>
<li>(<code>6f5b78c
&lt;https://github.com/litestar-org/polyfactory/commit/6f5b78cc5ef3f0fb1037d17d06837af54aac586d&gt;</code><em>)
- Use provider map for any in coverage (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/574">#574</a>)
by <code>@adhtruong &lt;https://github.com/adhtruong&gt;</code></em> in
<code>[#574](litestar-org/polyfactory#574)
&lt;https://github.com/litestar-org/polyfactory/pull/574&gt;</code>_</li>
<li>(<code>4aaf656
&lt;https://github.com/litestar-org/polyfactory/commit/4aaf656baa5b612fa3b63c6047b653b9b785f60d&gt;</code><em>)
- Tuple randomized length (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/573">#573</a>)
by <code>@adhtruong &lt;https://github.com/adhtruong&gt;</code></em> in
<code>[#573](litestar-org/polyfactory#573)
&lt;https://github.com/litestar-org/polyfactory/pull/573&gt;</code>_</li>
<li>(<code>9a83ad6
&lt;https://github.com/litestar-org/polyfactory/commit/9a83ad6354d675b42eb514f9354e845490608ba6&gt;</code><em>)
- Adjust min/max items to valid lengths for Set[Enum] fields (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/567">#567</a>)
by <code>@adrianeboyd &lt;https://github.com/adrianeboyd&gt;</code></em>
in <code>[#567](litestar-org/polyfactory#567)
&lt;https://github.com/litestar-org/polyfactory/pull/567&gt;</code>_</li>
<li>(<code>135d7fe
&lt;https://github.com/litestar-org/polyfactory/commit/135d7fea8d939a1fa8c87838d2b705db8968dccd&gt;</code><em>)
- Handle Optional type recursive models (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/584">#584</a>)
by <code>@adhtruong &lt;https://github.com/adhtruong&gt;</code></em> in
<code>[#584](litestar-org/polyfactory#584)
&lt;https://github.com/litestar-org/polyfactory/pull/584&gt;</code>_</li>
<li>(<code>6440faa
&lt;https://github.com/litestar-org/polyfactory/commit/6440faa24abfc34c2d10e31797224f5aec2d43c0&gt;</code><em>)
- Handle recursive collections (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/587">#587</a>)
by <code>@adhtruong &lt;https://github.com/adhtruong&gt;</code></em> in
<code>[#587](litestar-org/polyfactory#587)
&lt;https://github.com/litestar-org/polyfactory/pull/587&gt;</code>_</li>
</ul>
<p>Documentation

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>(<code>841831d
&lt;https://github.com/litestar-org/polyfactory/commit/841831d12f59a0825e490e71fdbc32744c433565&gt;</code><em>)
- Swap word with its antonym to match the context (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/575">#575</a>)
by <code>@UncleGoogle &lt;https://github.com/UncleGoogle&gt;</code></em>
in <code>[#575](litestar-org/polyfactory#575)
&lt;https://github.com/litestar-org/polyfactory/pull/575&gt;</code>_</li>
</ul>
<p>Features

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>(<code>c0c2704
&lt;https://github.com/litestar-org/polyfactory/commit/c0c27049afc9dd60395d5dd087f47ee700e1a6a7&gt;</code><em>)
- Enhance register_fixture return type annotation (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/581">#581</a>)
by <code>@giulioindev &lt;https://github.com/giulioindev&gt;</code></em>
in <code>[#581](litestar-org/polyfactory#581)
&lt;https://github.com/litestar-org/polyfactory/pull/581&gt;</code>_</li>
</ul>
<p>Build

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>(<code>d75b1d2
&lt;https://github.com/litestar-org/polyfactory/commit/d75b1d2eee3c042b3bf1ea7b8c2b0b6a744df75d&gt;</code><em>)
- Update pre-commit dependencies (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/571">#571</a>)
by <code>@adhtruong &lt;https://github.com/adhtruong&gt;</code></em> in
<code>[#571](litestar-org/polyfactory#571)
&lt;https://github.com/litestar-org/polyfactory/pull/571&gt;</code>_</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/37a9894a52c1188a927c8c9cb5b4485b73d062c3"><code>37a9894</code></a>
chore(release): bump release version (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/588">#588</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/6440faa24abfc34c2d10e31797224f5aec2d43c0"><code>6440faa</code></a>
fix: handle recursive collections (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/587">#587</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/135d7fea8d939a1fa8c87838d2b705db8968dccd"><code>135d7fe</code></a>
fix: handle Optional type recursive models (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/584">#584</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/9a83ad6354d675b42eb514f9354e845490608ba6"><code>9a83ad6</code></a>
fix: Adjust min/max items to valid lengths for Set[Enum] fields (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/567">#567</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/841831d12f59a0825e490e71fdbc32744c433565"><code>841831d</code></a>
docs: swap word with its antonym to match the context (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/575">#575</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/c0c27049afc9dd60395d5dd087f47ee700e1a6a7"><code>c0c2704</code></a>
feat(<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/579">#579</a>):
enhance register_fixture return type annotation (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/581">#581</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/4aaf656baa5b612fa3b63c6047b653b9b785f60d"><code>4aaf656</code></a>
fix: tuple randomized length (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/573">#573</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/6f5b78cc5ef3f0fb1037d17d06837af54aac586d"><code>6f5b78c</code></a>
fix: use provider map for any in coverage (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/574">#574</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/d75b1d2eee3c042b3bf1ea7b8c2b0b6a744df75d"><code>d75b1d2</code></a>
build: update pre-commit dependencies (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/571">#571</a>)</li>
<li><a
href="https://github.com/litestar-org/polyfactory/commit/24701eb593493f7c038709b1327dbf6dd8942e87"><code>24701eb</code></a>
fix: constrained 0 length lists (<a
href="https://redirect.github.com/litestar-org/polyfactory/issues/570">#570</a>)</li>
<li>See full diff in <a
href="https://github.com/litestar-org/polyfactory/compare/v2.16.2...v2.17.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=polyfactory&package-manager=pip&previous-version=2.16.2&new-version=2.17.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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