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

feat: allow use of the default value based on configuration #472

Merged
merged 6 commits into from
Jan 13, 2024
Merged

Conversation

guacs
Copy link
Member

@guacs guacs commented Jan 4, 2024

Description

  • This PR allows users to configure whether to use the default value for a field or not based on the __use_defaults__ field on the class.
  • Users can also have more fine-grained (i.e. per field) control by overriding BaseFactory.should_use_default_value classmethod.

Close Issue(s)

docs/usage/configuration.rst Outdated Show resolved Hide resolved
Sometimes users may want to use the default value set on a field rather than create a random value. This allows users
to configure that behavior. Also, for more fine-grained control, the users can override the 'should_use_default_value'
method in order to change the behavior per field or however else they wish to do so.
By moving the check into `process_kwargs`, we can just safely avoid
any edge cases that may arise from how the defaults are defined. If
we should be using the default for a field, we can just not add it to
the kwargs that we finally end up passing to the constructor of the
model. This way, we ensure that the creation of the default value will
happen normally however the underlying library (dataclasses,
pydantic etc.) would handle it.
cofin and others added 2 commits January 13, 2024 11:41
Co-authored-by: Alc-Alc <45509143+Alc-Alc@users.noreply.github.com>
@cofin cofin enabled auto-merge (squash) January 13, 2024 17:42
Copy link

sonarcloud bot commented Jan 13, 2024

@cofin cofin merged commit b33e662 into main Jan 13, 2024
19 checks passed
@cofin cofin deleted the use-defaults branch January 13, 2024 17:46
Copy link

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

@guacs
Copy link
Member Author

guacs commented Jan 14, 2024

@cofin thank you! :)

@berkaycagir
Copy link

When can we expect a new release with this functionality? 🙂

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.

Use defaults values from BaseModel in ModelFactory
6 participants