Skip to content

Commit

Permalink
Do not call default_factory if we don't need to
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Sep 12, 2023
1 parent 1b1187f commit e39a559
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mashumaro/core/meta/code/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def metadatas(self) -> typing.Dict[str, typing.Mapping[str, typing.Any]]:
# https://github.com/python/mypy/issues/1362
}

@lru_cache(None)
def get_field_default(
self, name: str, call_factory: bool = False
) -> typing.Any:
Expand Down Expand Up @@ -903,7 +904,13 @@ def _add_pack_method_lines(self, method_name: str) -> None:
if force_value:
self.add_line(f"value = self.{fname}")
alias = aliases.get(fname)
default = self.get_field_default(fname, call_factory=True)
if omit_default:
# do not call default_factory if we don't need to
default = self.get_field_default(
fname, call_factory=True
)
else:
default = None
if fname in nullable_fields:
if (
packer == "value"
Expand Down

0 comments on commit e39a559

Please sign in to comment.