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

Schema generation improvements #7054

Closed
adriangb opened this issue Aug 9, 2023 · 2 comments
Closed

Schema generation improvements #7054

adriangb opened this issue Aug 9, 2023 · 2 comments
Assignees

Comments

@adriangb
Copy link
Member

adriangb commented Aug 9, 2023

Tracking issue for #6951 and related work.

Pydantic v2 embraced the use of Annotated which has proved to be a really good decision. However in the process of getting this to work, we've learned a lot and made a lot of mistakes. One of those mistakes is the sheer complexity of GenerateSchema.

In particular since we put constraints (like min_length) directly on the type's schema the type needs to know about it's constraints. In other words, if you have Annotated[str, <some stuff>, Field(min_length=1)] then str needs to know about Field when it generates it's schema. That means you can't just iterate over the annotations left to right or something like that, hence we created __prepare_pydantic_annotations__. All of this has some pretty unfortunate consequences:

Ideally, we want to:

  • Simplify GenerateSchema and refactor it into an easily understandable class that we can then make public so that users can override it.
  • Improve startup time by simplifying GenerateSchema and introducing some caching.
  • Get rid of __prepare_pydantic_annotations__
  • Allow constraints to be re-ordered

To get there I think we'll want to:

Other related issues:

Selected Assignee: @samuelcolvin

@pydantic-hooky pydantic-hooky bot added the unconfirmed Bug not yet confirmed as valid/applicable label Aug 9, 2023
@adriangb
Copy link
Member Author

adriangb commented Aug 9, 2023

cc @samuelcolvin @dmontagu

@samuelcolvin samuelcolvin removed the unconfirmed Bug not yet confirmed as valid/applicable label Aug 22, 2023
@adriangb
Copy link
Member Author

I still think we need to do some cleanup of GenerateSchema beyond what we've already done (see merged PRs linked to from the fist post in this issue), but that may need larger refactors in pydantic-core that might require waiting for v3.

For now we've improved performance vastly with some more minimal refactoring (#7565, #7536, #7535, #7529, #7528, #7527, #7524, #7523 and #7522) so I'm going to close this issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants