8000 Do not cache parametrized models when in the process of parametrizing another model by Viicos · Pull Request #10704 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Do not cache parametrized models when in the process of parametrizing another model #10704

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

Merged
merged 4 commits into from
Jan 7, 2025

Conversation

Viicos
Copy link
Member
@Viicos Viicos commented Oct 24, 2024

Change Summary

Fixes #10279, partially fixes #9887.

We avoid caching parametrized models when in the process of parametrizing another model, as the result of BaseModel.__class_getitem__ can return PydanticRecursiveRef instances in this case. In the model core schema generation logic, we mutate FieldInfo instances if the annotation wasn't evaluated yet, meaning we could set the annotation to some typing construct containing a PydanticRecursiveRef, e.g. with:

class Model1[T](BaseModel):
    # When evaluated, this annotation will be a `PydanticRecursiveRef`
    model2: 'Model2[T]'

class Model2[S](BaseModel):
    model1: Model1[S]

Because these FieldInfo instances come from the model_fields attribute of the model class, having the caching logic here can result in a parametrized model class with a FieldInfo containing PydanticRecursiveRef instances when we later parametrize a model (e.g. Model1[str]).

Note that is hopefully not a long term solution, as this doesn't fix what is an even simpler repro: #9969.

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 Oct 24, 2024
Copy link
cloudflare-workers-and-pages bot commented Oct 24, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6194229
Status: ✅  Deploy successful!
Preview URL: https://24ecaaa2.pydantic-docs.pages.dev
Branch Preview URL: https://10279-2.pydantic-docs.pages.dev

View logs

Copy link
codspeed-hq bot commented Oct 24, 2024

CodSpeed Performance Report

Merging #10704 will not alter performance

Comparing 10279-2 (6194229) with main (aa85c3a)

Summary

✅ 46 untouched benchmarks

@sydney-runkle
Copy link
Contributor

@Viicos, this should be unblocked now :)

@Viicos Viicos added the third-party-tests Add this label on a PR to trigger 3rd party tests label Jan 6, 2025
@Viicos Viicos marked this pull request as ready for review January 6, 2025 18:55
Comment on lines -761 to -762
assert module.Foo.model_fields['bar'].annotation == typing.Optional[module.Bar[str]]
assert module.Foo.model_fields['bar2'].annotation == typing.Union[int, module.Bar[float]]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions are not valid anymore, as the Bar[str] class on the annotation is no longer the same that the one we create by calling module.Bar[str] here (as it isn't cached anymore).

Copy link
Contributor
github-actions bot commented Jan 6, 2025

Coverage report

Click to see where and how coverage changed

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

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

Copy link
Contributor
@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Happy to see this move across the line 👍

@sydney-runkle sydney-runkle merged commit 0b81063 into main Jan 7, 2025
147 checks passed
@sydney-runkle sydney-runkle deleted the 10279-2 branch January 7, 2025 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes. third-party-tests Add this label on a PR to trigger 3rd party tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeyError happening during schema cleaning with generic models KeyError when trying to instantiate BaseModels that depend on each other
2 participants
0