Skip to content

Commit

Permalink
refactor: move should_use_default check into process_kwargs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guacs committed Jan 13, 2024
1 parent c5eebe9 commit 0e01d5d
Showing 1 changed file with 1 addition and 6 deletions.
7 changes: 1 addition & 6 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,11 +636,6 @@ def get_field_value( # noqa: C901, PLR0911, PLR0912
if field_build_parameters is None and cls.should_set_none_value(field_meta=field_meta):
return None

if cls.should_use_default_value(field_meta):
if callable(field_meta.default):
return field_meta.default()
return field_meta.default

unwrapped_annotation = unwrap_annotation(field_meta.annotation, random=cls.__random__)

if is_literal(annotation=unwrapped_annotation) and (literal_args := get_args(unwrapped_annotation)):
Expand Down Expand Up @@ -871,7 +866,7 @@ def process_kwargs(cls, **kwargs: Any) -> dict[str, Any]:

for field_meta in cls.get_model_fields():
field_build_parameters = cls.extract_field_build_parameters(field_meta=field_meta, build_args=kwargs)
if cls.should_set_field_value(field_meta, **kwargs):
if cls.should_set_field_value(field_meta, **kwargs) and not cls.should_use_default_value(field_meta):
if hasattr(cls, field_meta.name) and not hasattr(BaseFactory, field_meta.name):
field_value = getattr(cls, field_meta.name)
if isinstance(field_value, Ignore):
Expand Down

0 comments on commit 0e01d5d

Please sign in to comment.