Fixed PrivateAttr defaults ignored on database-loaded instances#1818
Draft
xr843 wants to merge 2 commits intofastapi:mainfrom
Draft
Fixed PrivateAttr defaults ignored on database-loaded instances#1818xr843 wants to merge 2 commits intofastapi:mainfrom
xr843 wants to merge 2 commits intofastapi:mainfrom
Conversation
When SQLAlchemy reconstructs a model instance from a database query (bypassing __init__), it goes through __new__ which calls init_pydantic_private_attrs(). That function was setting __pydantic_private__ = None instead of initializing it with the private attribute defaults. This caused AttributeError when accessing PrivateAttr fields on database-loaded instances. The fix introspects the class's __private_attributes__ dict and calls get_default() on each entry, mirroring what Pydantic's own __init__ does. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
|
@xr843, please, review and test changes before opening PR for review |
- Used getattr() instead of direct attribute access to satisfy mypy's union-attr check on InstanceOrType. - Added regression tests verifying PrivateAttr with default and default_factory work correctly on database-loaded instances. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Author
|
Thank you for the feedback @YuriiMotov. I've addressed the issues:
Both tests confirm the fix resolves the Ready for re-review when you have a chance. |
Member
Please review the CI errors as well. |
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.
Summary
Fixes #149
__init__), it calls__new__which invokesinit_pydantic_private_attrs(). That function was setting__pydantic_private__ = Noneinstead of initializing it with the private attribute defaults.AttributeErrorwhen accessingPrivateAttrfields on database-loaded instances.__private_attributes__dict and callsget_default()on each entry to properly initialize__pydantic_private__, mirroring what Pydantic's ownBaseModel.__init__does.Test plan
PrivateAttr(default=...)fieldsAttributeError🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com