Skip to content

Commit

Permalink
lower complexity of _process_class
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Feb 24, 2021
1 parent fb97e08 commit 9032fd1
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions pydantic/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ def is_builtin_dataclass(_cls: Type[Any]) -> bool:
return not hasattr(_cls, '__processed__') and dataclasses.is_dataclass(_cls)


def _generate_pydantic_post_init(
post_init_original: Optional[Callable[..., None]], post_init_post_parse: Optional[Callable[..., None]]
) -> Callable[..., None]:
def _pydantic_post_init(self: 'Dataclass', *initvars: Any) -> None:
if post_init_original is not None:
post_init_original(self, *initvars)

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
object.__setattr__(self, '__dict__', d)
object.__setattr__(self, '__initialised__', True)
if post_init_post_parse is not None:
post_init_post_parse(self, *initvars)

return _pydantic_post_init


def _process_class(
_cls: Type[Any],
init: bool,
Expand All @@ -101,23 +125,7 @@ def _process_class(

post_init_post_parse = getattr(_cls, '__post_init_post_parse__', None)

def _pydantic_post_init(self: 'Dataclass', *initvars: Any) -> None:
if post_init_original is not None:
post_init_original(self, *initvars)

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
object.__setattr__(self, '__dict__', d)
object.__setattr__(self, '__initialised__', True)
if post_init_post_parse is not None:
post_init_post_parse(self, *initvars)
_pydantic_post_init = _generate_pydantic_post_init(post_init_original, post_init_post_parse)

# If the class is already a dataclass, __post_init__ will not be called automatically
# so no validation will be added.
Expand Down

0 comments on commit 9032fd1

Please sign in to comment.