-
-
Notifications
You must be signed in to change notification settings - Fork 95
feat: Issue 678 use pydantic field examples #679
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
feat: Issue 678 use pydantic field examples #679
Conversation
…bably add build context, so this feature can be disabled
…the user's factory
Now everything is green except of the mypy. How should I fix it? I can either type-ignore it, or I can move the .examples to the base class. Or I can use some cast tricks. |
Thanks for work on this! Could a union type ie. |
I fixed not only mypy, but I also added some docs. |
Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/679 |
Looks good. Thanks for the work on this! |
Bumps [polyfactory](https://github.com/litestar-org/polyfactory) from 2.20.0 to 2.21.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.21.0</h2> <h1>Sponsors 🌟</h1> <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> <h3>Bug Fixes</h3> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/4ae69c81c61e70269c004794abf3abca31700e87">4ae69c8</a>) - Handle pydantic v1 URL and email correctly (<a href="https://github.com/litestar-org/polyfactory/issues/685">#685</a>) by <a href="https://github.com/Xdynix"><code>@Xdynix</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/685">#685</a></p> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/18d857966c2049f9705351f276370c7a4e38c0d5">18d8579</a>) - Make type alias detection work with typing_extensions 4.13 (<a href="https://github.com/litestar-org/polyfactory/issues/684">#684</a>) by <a href="https://github.com/airwoodix"><code>@airwoodix</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/684">#684</a></p> <p>### Features</p> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/9940230d3b1958f6f0b14e3157dcdd137af40cea">9940230</a>) - Document overriding SQLA column mapping (<a href="https://github.com/litestar-org/polyfactory/issues/671">#671</a>) by <a href="https://github.com/adhtruong"><code>@adhtruong</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/671">#671</a></p> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/cc789b55298e6e160f8d14877c7ea5859d72ee4d">cc789b5</a>) - Support new type and annotated mapping (<a href="https://github.com/litestar-org/polyfactory/issues/680">#680</a>) by <a href="https://github.com/adhtruong"><code>@adhtruong</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/680">#680</a></p> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/36395d94a3a325c3f7db51fc71f0ef9c2edb3f3d">36395d9</a>) - Issue 678 use pydantic field examples (<a href="https://github.com/litestar-org/polyfactory/issues/679">#679</a>) by <a href="https://github.com/stardust85"><code>@stardust85</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/679">#679</a></p> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/02fed7258c5b522891a0b8cdbd0491d21bc9bd95">02fed72</a>) - Add release command (<a href="https://github.com/litestar-org/polyfactory/issues/687">#687</a>) by <a href="https://github.com/adhtruong"><code>@adhtruong</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/687">#687</a></p> <h3>Miscellaneous Tasks</h3> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/e0c6941d3fdf89038484bfe62b80485b897ca439">e0c6941</a>) - Remove sonar (<a href="https://github.com/litestar-org/polyfactory/issues/672">#672</a>) by <a href="https://github.com/adhtruong"><code>@adhtruong</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/672">#672</a></p> <h3>Refactor</h3> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/00aa862cf96174b4fb845e95afeb0e440f1679ee">00aa862</a>) - Deprecate FieldMeta.random (<a href="https://github.com/litestar-org/polyfactory/issues/663">#663</a>) by <a href="https://github.com/adhtruong"><code>@adhtruong</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/663">#663</a></p> <p>(<a href="https://github.com/litestar-org/polyfactory/commit/ade1115b20bb61da698cb2e1561b38a8de20fca3">ade1115</a>) - Migrate to typos (<a href="https://github.com/litestar-org/polyfactory/issues/675">#675</a>) by <a href="https://github.com/adhtruong"><code>@adhtruong</code></a> in <a href="https://github.com/litestar-org/polyfactory/pull/675">#675</a></p> <h2>New Contributors</h2> <ul> <li><a href="https://github.com/Xdynix"><code>@Xdynix</code></a> made their first contribution in <a href="https://github.com/litestar-org/polyfactory/pull/685">litestar-org/polyfactory#685</a></li> <li><a href="https://github.com/airwoodix"><code>@airwoodix</code></a> made their first contribution in <a href="https://github.com/litestar-org/polyfactory/pull/684">litestar-org/polyfactory#684</a></li> <li><a href="https://github.com/stardust85"><code>@stardust85</code></a> made their first contribution in <a href="https://github.com/litestar-org/polyfactory/pull/679">litestar-org/polyfactory#679</a></li> </ul> <p><strong>Full Changelog</strong>: <a href="https://github.com/litestar-org/polyfactory/compare/v2.20.0...v2.21.0">https://github.com/litestar-org/polyfactory/compare/v2.20.0...v2.21.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> <h2><code>Release [v2.21.0] - 2025-04-18 <https://github.com/litestar-org/polyfactory/releases/tag/v2.21.0></code>_</h2> <ul> <li><code>See All commits in v2.21.0 <https://github.com/litestar-org/polyfactory/commits/v2.21.0></code>_</li> </ul> <p><code>0facd26 <https://github.com/litestar-org/polyfactory/commit/0facd26ec2e8915e1769e90ffb55ccccd60aadc5></code>_ ... <code>02fed72 <https://github.com/litestar-org/polyfactory/commit/02fed7258c5b522891a0b8cdbd0491d21bc9bd95></code>_ | <code>See diff for 2.21.0 <https://github.com/litestar-org/polyfactory/compare/0facd26ec2e8915e1769e90ffb55ccccd60aadc5...02fed7258c5b522891a0b8cdbd0491d21bc9bd95></code>_</p> <p>Bug Fixes ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p> <ul> <li>(<code>4ae69c8 <https://github.com/litestar-org/polyfactory/commit/4ae69c81c61e70269c004794abf3abca31700e87></code><em>) - Handle pydantic v1 URL and email correctly (<a href="https://github.com/litestar-org/polyfactory/issues/685">#685</a>) by <code>@Xdynix <https://github.com/Xdynix></code></em> in <code>[#685](litestar-org/polyfactory#685) <https://github.com/litestar-org/polyfactory/pull/685></code>_</li> <li>(<code>18d8579 <https://github.com/litestar-org/polyfactory/commit/18d857966c2049f9705351f276370c7a4e38c0d5></code><em>) - Make type alias detection work with typing_extensions 4.13 (<a href="https://github.com/litestar-org/polyfactory/issues/684">#684</a>) by <code>@airwoodix <https://github.com/airwoodix></code></em> in <code>[#684](litestar-org/polyfactory#684) <https://github.com/litestar-org/polyfactory/pull/684></code>_</li> </ul> <p>Features ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p> <ul> <li>(<code>9940230 <https://github.com/litestar-org/polyfactory/commit/9940230d3b1958f6f0b14e3157dcdd137af40cea></code><em>) - Document overriding SQLA column mapping (<a href="https://github.com/litestar-org/polyfactory/issues/671">#671</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#671](litestar-org/polyfactory#671) <https://github.com/litestar-org/polyfactory/pull/671></code>_</li> <li>(<code>cc789b5 <https://github.com/litestar-org/polyfactory/commit/cc789b55298e6e160f8d14877c7ea5859d72ee4d></code><em>) - Support new type and annotated mapping (<a href="https://github.com/litestar-org/polyfactory/issues/680">#680</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#680](litestar-org/polyfactory#680) <https://github.com/litestar-org/polyfactory/pull/680></code>_</li> <li>(<code>36395d9 <https://github.com/litestar-org/polyfactory/commit/36395d94a3a325c3f7db51fc71f0ef9c2edb3f3d></code><em>) - Issue 678 use pydantic field examples (<a href="https://github.com/litestar-org/polyfactory/issues/679">#679</a>) by <code>@stardust85 <https://github.com/stardust85></code></em> in <code>[#679](litestar-org/polyfactory#679) <https://github.com/litestar-org/polyfactory/pull/679></code>_</li> <li>(<code>02fed72 <https://github.com/litestar-org/polyfactory/commit/02fed7258c5b522891a0b8cdbd0491d21bc9bd95></code><em>) - Add release command (<a href="https://github.com/litestar-org/polyfactory/issues/687">#687</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#687](litestar-org/polyfactory#687) <https://github.com/litestar-org/polyfactory/pull/687></code>_</li> </ul> <p>Miscellaneous Tasks ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p> <ul> <li>(<code>e0c6941 <https://github.com/litestar-org/polyfactory/commit/e0c6941d3fdf89038484bfe62b80485b897ca439></code><em>) - Remove sonar (<a href="https://github.com/litestar-org/polyfactory/issues/672">#672</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#672](litestar-org/polyfactory#672) <https://github.com/litestar-org/polyfactory/pull/672></code>_</li> </ul> <p>Refactor ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p> <ul> <li>(<code>00aa862 <https://github.com/litestar-org/polyfactory/commit/00aa862cf96174b4fb845e95afeb0e440f1679ee></code><em>) - Deprecate FieldMeta.random (<a href="https://github.com/litestar-org/polyfactory/issues/663">#663</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#663](litestar-org/polyfactory#663) <https://github.com/litestar-org/polyfactory/pull/663></code>_</li> <li>(<code>ade1115 <https://github.com/litestar-org/polyfactory/commit/ade1115b20bb61da698cb2e1561b38a8de20fca3></code><em>) - Migrate to typos (<a href="https://github.com/litestar-org/polyfactory/issues/675">#675</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#675](litestar-org/polyfactory#675) <https://github.com/litestar-org/polyfactory/pull/675></code>_</li> </ul> <p>Ci ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</p> <ul> <li>(<code>c42bf6d <https://github.com/litestar-org/polyfactory/commit/c42bf6dd4aaa2b3625b7dc7d2873bbd5b0a7a70d></code><em>) - Add toml formatter (<a href="https://github.com/litestar-org/polyfactory/issues/682">#682</a>) by <code>@adhtruong <https://github.com/adhtruong></code></em> in <code>[#682](litestar-org/polyfactory#682) <https://github.com/litestar-org/polyfactory/pull/682></code>_</li> </ul> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/litestar-org/polyfactory/commit/203310bfa4a8054efb59c1bda34715d4196a7954"><code>203310b</code></a> chore(release): bump to v2.21.0 (<a href="https://github.com/litestar-org/polyfactory/issues/692">#692</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/02fed7258c5b522891a0b8cdbd0491d21bc9bd95"><code>02fed72</code></a> feat: add release command (<a href="https://github.com/litestar-org/polyfactory/issues/687">#687</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/36395d94a3a325c3f7db51fc71f0ef9c2edb3f3d"><code>36395d9</code></a> feat: Issue 678 use pydantic field examples (<a href="https://github.com/litestar-org/polyfactory/issues/679">#679</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/18d857966c2049f9705351f276370c7a4e38c0d5"><code>18d8579</code></a> fix: make type alias detection work with typing_extensions 4.13 (<a href="https://github.com/litestar-org/polyfactory/issues/684">#684</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/4ae69c81c61e70269c004794abf3abca31700e87"><code>4ae69c8</code></a> fix: handle pydantic v1 URL and email correctly (<a href="https://github.com/litestar-org/polyfactory/issues/685">#685</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/c42bf6dd4aaa2b3625b7dc7d2873bbd5b0a7a70d"><code>c42bf6d</code></a> ci: add toml formatter (<a href="https://github.com/litestar-org/polyfactory/issues/682">#682</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/ade1115b20bb61da698cb2e1561b38a8de20fca3"><code>ade1115</code></a> refactor: migrate to typos (<a href="https://github.com/litestar-org/polyfactory/issues/675">#675</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/cc789b55298e6e160f8d14877c7ea5859d72ee4d"><code>cc789b5</code></a> feat: support new type and annotated mapping (<a href="https://github.com/litestar-org/polyfactory/issues/680">#680</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/e0c6941d3fdf89038484bfe62b80485b897ca439"><code>e0c6941</code></a> chore: remove sonar (<a href="https://github.com/litestar-org/polyfactory/issues/672">#672</a>)</li> <li><a href="https://github.com/litestar-org/polyfactory/commit/00aa862cf96174b4fb845e95afeb0e440f1679ee"><code>00aa862</code></a> refactor: deprecate FieldMeta.random (<a href="https://github.com/litestar-org/polyfactory/issues/663">#663</a>)</li> <li>Additional commits viewable in <a href="https://github.com/litestar-org/polyfactory/compare/v2.20.0...v2.21.0">compare view</a></li> </ul> </details> <br /> [](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] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Description
trying to implement the enhancement I asked for. If you define
__allow_examples__
dunder attribute as True in your factory, it will use a random value from the pydantic class's .examples attribute (see https://docs.pydantic.dev/latest/concepts/fields/#customizing-json-schema). The default value is False, so it is backwards compatible.This feature is supported only in Pydantic V2, but for Pydantic V1 it will just not try to access this attribute (it is not present there), so it is backwards compatible.
Closes
Fixes #678