Skip to content

Commit

Permalink
add __has_field_info_default__ for minimal effect on perf
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Feb 24, 2021
1 parent 149b10c commit fb97e08
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pydantic/dataclasses.py
Expand Up @@ -19,6 +19,7 @@ class Dataclass:
__initialised__: bool
__post_init_original__: Optional[Callable[..., None]]
__processed__: Optional[ClassAttribute]
__has_field_info_default__: bool # whether or not a `pydantic.Field` is used as default value

def __init__(self, *args: Any, **kwargs: Any) -> None:
pass
Expand Down Expand Up @@ -104,9 +105,12 @@ def _pydantic_post_init(self: 'Dataclass', *initvars: Any) -> None:
if post_init_original is not None:
post_init_original(self, *initvars)

# We need to remove `FieldInfo` values since they are not valid as input
# It's ok since they are obviously the default values
input_data = {k: v for k, v in self.__dict__.items() if not isinstance(v, FieldInfo)}
if getattr(self, '__has_field_info_default__', False):
# We need to remove `FieldInfo` values since they are not valid as input
# It's ok to do that because they are obviously the default values!
input_data = {k: v for k, v in self.__dict__.items() if not isinstance(v, FieldInfo)}
else:
input_data = self.__dict__
d, _, validation_error = validate_model(self.__pydantic_model__, input_data, cls=self.__class__)
if validation_error:
raise validation_error
Expand Down Expand Up @@ -164,6 +168,7 @@ def _pydantic_post_init(self: 'Dataclass', *initvars: Any) -> None:

if isinstance(default, FieldInfo):
field_info = default
cls.__has_field_info_default__ = True
else:
field_info = Field(default=default, default_factory=default_factory, **field.metadata)

Expand Down

0 comments on commit fb97e08

Please sign in to comment.