Ensure __pydantic_private__ is set in model_construct() with user-defined model_post_init()#12816
Merged
Viicos merged 4 commits intopydantic:mainfrom Mar 2, 2026
Merged
Conversation
__pydantic_private__ is set in model_construct with user-defined model_post_init__pydantic_private__ is set in model_construct() with user-defined model_post_init()
…r-defined model_post_init() When a model defines model_post_init() and uses PrivateAttr fields, calling model_construct() would fail because __pydantic_private__ was not initialized before model_post_init() was called. This ensures __pydantic_private__ is always set with default values before model_post_init() runs in model_construct(). Fixes pydantic#12813
a365e02 to
18922fc
Compare
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Viicos
approved these changes
Mar 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12813
Problem
When a model defines a custom
model_post_initbut has no private attributes,model_construct()fails to set__pydantic_private__, causingpickle.dumps()to crash withAttributeError.This happens because:
model_post_initwithinit_private_attributesmodel_constructonly sets__pydantic_private__in theelifbranch (when__pydantic_post_init__is falsy)model_post_init,__pydantic_post_init__is truthy, so theelifbranch is skippedmodel_post_initdoes not set__pydantic_private__→ attribute never gets setFix
After calling
model_post_initinmodel_construct, check if__pydantic_private__was set. If not, set it toNone.Test
Added
test_model_construct_pickle_with_model_post_initthat reproduces the exact scenario from the issue.