Do not duplicate metadata on model rebuild#11902
Merged
Viicos merged 2 commits intov2.11-fixesfrom May 22, 2025
Merged
Conversation
CodSpeed Performance ReportMerging #11902 will not alter performanceComparing Summary
|
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Deploying pydantic-docs with
|
| Latest commit: |
cb10455
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://43275662.pydantic-docs.pages.dev |
| Branch Preview URL: | https://11870-2-11-fix.pydantic-docs.pages.dev |
Viicos
commented
May 22, 2025
Comment on lines
+591
to
+598
| def _copy(self) -> Self: | ||
| copied = copy(self) | ||
| for attr_name in ('metadata', '_attributes_set', '_qualifiers'): | ||
| # Apply "deep-copy" behavior on collections attributes: | ||
| value = getattr(copied, attr_name).copy() | ||
| setattr(copied, attr_name, value) | ||
|
|
||
| return copied |
Member
Author
There was a problem hiding this comment.
We can't define a custom __copy__(), because I couldn't find a way to delegate to copy.copy() and then apply the special case for metadata, _attributes_set and _qualifiers.
The following could still be done:
def __copy__(self) -> Self:
cls = type(self)
copied = cls()
for attr_name in cls.__slots__:
value = getattr(self, attr_name)
if attr_name in ('metadata', '_attributes_set', '_qualifiers'):
# Apply "deep-copy" behavior on collections attributes:
value = value.copy()
setattr(copied, attr_name, value)
return copiedBut this blows up on libraries (FastAPI/SQLModel) subclassing FieldInfo (not the first time this is causing issues..) as we don't know which extra attributes are defined on these classes.
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.
Change Summary
This is a stripped down version of #11898, as a backport to 2.11. In 2.11, a change in the model rebuild logic surfaced an issue with
FieldInfobeing wrongly mutated and copied properly, resulting in a regression.#11898 is a proper refactor, but can't be fully backported as it would be too risky as a patch release.
Fixes (for 2.11) #11870.
Third-party tests output.
Related issue number
Checklist