-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Could __post_init_original__ run before pydantic validation? #557
Comments
I'd be happy with the change if it didn't break any existing tests. |
@samuelcolvin I opened pull request #560 with the changes. All tests seem to be passing fine. Could you review it? |
If I many I want to reopen this issue. Shouldn't there be a way to disable this functionality with a global config variable? Now you kinda have to validate the payload inside the post_init again or is my workflow wrong?
Yes the fix is easy here just create a Date object and do But what if I have a deep nested structure with a lot of composition do I need to create each object separately before creating my parent class? |
Moved that discussion to another issue, see #566. |
* Use __pydantic_serializer__ for serializing to JSON * Make to_json(by_alias=False) work * Use presence of __pydantic_serializer__ instead of anything else to go down model serialization path
Question
import sys; print(sys.version)
:import pydantic; print(pydantic.VERSION)
:Currently pydantic runs a dataclass' original
__post_init__
after its validation. However that limits valid use cases in which a required field is calculated inside the__post_init__
. This is exemplified in python's dataclass documentation here.Python's example fails pydantic's validation:
This behaviour is enforced here:
https://github.com/samuelcolvin/pydantic/blob/426325820738328ce4c023d43fbddad07d62f4a0/pydantic/dataclasses.py#L27-L32
The text was updated successfully, but these errors were encountered: