Skip to content

Commit

Permalink
Improve initialization hooks example
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Jul 24, 2023
1 parent fcec0a4 commit 15f4a1f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions docs/usage/dataclasses.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,21 @@ class User:

@model_validator(mode='before')
def pre_root(cls, values: Dict[str, Any]) -> Dict[str, Any]:
print(values)
#> ArgsKwargs((), {'birth': {'year': 1995, 'month': 3, 'day': 2}})
print(f'First: {values}')
"""
First: ArgsKwargs((), {'birth': {'year': 1995, 'month': 3, 'day': 2}})
"""
return values

@model_validator(mode='after')
def post_root(self) -> 'User':
print(self)
#> User(birth=Birth(year=1995, month=3, day=2))
print(f'Third: {self}')
#> Third: User(birth=Birth(year=1995, month=3, day=2))
return self

def __post_init__(self):
print(self.birth)
#> Birth(year=1995, month=3, day=2)
print(f'Second: {self.birth}')
#> Second: Birth(year=1995, month=3, day=2)


user = User(**{'birth': {'year': 1995, 'month': 3, 'day': 2}})
Expand Down

0 comments on commit 15f4a1f

Please sign in to comment.