-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Do not duplicate metadata on model rebuild #11902
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
Conversation
CodSpeed Performance ReportMerging #11902 will not alter performanceComparing Summary
|
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 |
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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