Skip to content
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

Add config.defer_build to optionally make model building lazy #6823

Merged
merged 4 commits into from Jul 25, 2023

Conversation

samuelcolvin
Copy link
Member

@samuelcolvin samuelcolvin commented Jul 24, 2023

Change Summary

This proposes a new config flag defer_model_build, when true this means the core schema, schema validator and schema serializer are not constructed for a model when it's defined, instead they're only built when model_rebuild() is called, or when the model is first called.

Two reasons this can be useful:

Related issue number

fix #6742

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 - somewhat, I'm going to rewrite the docs for ConfigDict anyway, so I'll add more logic then
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @adriangb

@samuelcolvin
Copy link
Member Author

please review.

@adriangb
Copy link
Member

Having this as a config flag sounds reasonable to me. For the reasons discussed in #6768 I don’t think it’s good as a default, but for large apps it seems like a reasonable trade-off.

Shouldn’t we make TypeAdapter lazy as well?

@samuelcolvin
Copy link
Member Author

Let's rename to defer_build and apply to TypeAdapter too.

@cloudflare-pages
Copy link

cloudflare-pages bot commented Jul 24, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: b4fdc2f
Status: ✅  Deploy successful!
Preview URL: https://dd11051d.pydantic-docs2.pages.dev
Branch Preview URL: https://defer-model-build.pydantic-docs2.pages.dev

View logs

@@ -23,7 +23,7 @@ def __init__(
code: PydanticErrorCodes,
attempt_rebuild: Callable[[], SchemaValidator | None] | None = None,
) -> None:
"""Attempt rebuild."""
# debug(attempt_rebuild, code)
Copy link
Member

Choose a reason for hiding this comment

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

I think you should remove it

@samuelcolvin samuelcolvin changed the title Add config.defer_model_build to optionally make model building lazy Add config.defer_build to optionally make model building lazy Jul 25, 2023
@samuelcolvin samuelcolvin enabled auto-merge (squash) July 25, 2023 11:34
@samuelcolvin samuelcolvin merged commit cff5385 into main Jul 25, 2023
46 checks passed
@samuelcolvin samuelcolvin deleted the defer_model_build branch July 25, 2023 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect ForwardRef class lookup in inherited models
3 participants