Skip to content

Do not duplicate metadata on model rebuild#11902

Merged
Viicos merged 2 commits intov2.11-fixesfrom
11870-2.11-fix
May 22, 2025
Merged

Do not duplicate metadata on model rebuild#11902
Viicos merged 2 commits intov2.11-fixesfrom
11870-2.11-fix

Conversation

@Viicos
Copy link
Copy Markdown
Member

@Viicos Viicos commented May 22, 2025

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 FieldInfo being 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

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@github-actions github-actions Bot added the relnotes-fix Used for bugfixes. label May 22, 2025
@Viicos Viicos added the third-party-tests Add this label on a PR to trigger 3rd party tests label May 22, 2025
@Viicos Viicos closed this May 22, 2025
@Viicos Viicos reopened this May 22, 2025
@Viicos Viicos removed the third-party-tests Add this label on a PR to trigger 3rd party tests label May 22, 2025
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 22, 2025

CodSpeed Performance Report

Merging #11902 will not alter performance

Comparing 11870-2.11-fix (cb10455) with main (1f967d7)

Summary

✅ 46 untouched benchmarks

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 22, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
Project Total  

This report was generated by python-coverage-comment-action

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 22, 2025

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

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

View logs

Comment thread pydantic/fields.py
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
Copy link
Copy Markdown
Member Author

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 copied

But 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.

@Viicos Viicos merged commit 1b63218 into v2.11-fixes May 22, 2025
51 checks passed
@Viicos Viicos deleted the 11870-2.11-fix branch May 22, 2025 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotes-fix Used for bugfixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant