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
Pydantic V2 blog #4218
Pydantic V2 blog #4218
Conversation
Thanks so much @PrettyWood, these all look like sensible points, I'll fix them tomorrow. |
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.
I can't say how happy I am to see v2 coming. The work you've done is remarkable! And I love the well thought changes. Thank you so much!
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.
Looking good! Lots of exciting changes.
Overall a bit long and wordy, and some paragraphs have overly long sentences in them.
So if you're editing it wouldn't hurt to try and cut or shorten stuff generally.
I like http://www.hemingwayapp.com/ to nudge towards short and clear sentences.
7c13dcb
to
c35c1df
Compare
Thanks all for your reviews. I've extended the blog/proposal significantly, partly in response to your feedback. @PrettyWood would be great if you could have a quick look at the new "Implementation Details" section. For me this is mostly ready, but I'm waiting for @tiangolo @adriangb and @tomhamiltonstubber's reviews. |
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.
This all looks great! 🎉
Just a couple of suggestions, nitpicks, and questions.
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
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.
Exciting times!
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.
This looks fantastic 💪 🚀
Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
!!! note | ||
We (actually mostly Sebastián :wink:) will have to make some changes to FastAPI to fully leverage `context` | ||
as we'd need some kind of dependency injection to build context before validation so models can still be passed as | ||
arguments to views. I'm sure he'll be game. |
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.
I'm game! Haha 😅
I think it's gonna be challenging but I'm confident is solvable/doable.
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.
Thanks, it's not like anything breaks here if you make no changes - just extra functionality if you do...
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 might need to add another method to models like
class MyModel(BaseModel):
@classmethod
async def model_prepare_context(cls, db_conn: Depends(get_db)) -> dict[str, Any:
"""allows custom logic to prepare context"""
I guess model_prepare_context
doesn't even need to on BaseModel
, you just document in FastAPI that if it's defined, it'll be called with dependency resolution to prepare context?
Up to you, but I'm very happy to add something like this to pydantic if it helps FastAPI.
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.
Amazing! 🚀🎉
This is live 🚀 |
""" | ||
previously `update_forward_refs()`, update forward references | ||
""" | ||
def model_copy(self, ...) -> Self: |
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.
Glad it's live. You forgot to remove this
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.
Good catch, will do so later.
and a validation error is raised | ||
2. The reason I've been so keen to get pydantic-core to compile and run with wasm is that I want all examples | ||
in the docs of pydantic V2 to be editable and runnable in the browser | ||
3. Full (pun intended) support for `TypedDict`, including `full=False` - e.g. omitted keys, |
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.
You mean total=False
I reckon
2. The reason I've been so keen to get pydantic-core to compile and run with wasm is that I want all examples | ||
in the docs of pydantic V2 to be editable and runnable in the browser | ||
3. Full (pun intended) support for `TypedDict`, including `full=False` - e.g. omitted keys, | ||
providing validation schema to a `TypeDict` field/item will use `Annotated`, e.g. `Annotated[str, Field(strict=True)]` |
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.
TypedDict
and more comprehensive details of errors to be provided to end users, | ||
[pydantic#784](https://github.com/samuelcolvin/pydantic/issues/784) | ||
6. `on_error` logic in a schema which allows either a default value to be used in the event of an error, | ||
or that value to be omitted (in the case of a `full=False` `TypeDict`), |
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.
Same
8. Powerful "priority" system for optionally merging or overriding config in sub-models for nested schemas | ||
9. Pydantic will support [annotated-types](https://github.com/annotated-types/annotated-types), | ||
so you can do stuff like `Annotated[set[int], Len(0, 10)]` or `Name = Annotated[str, Len(1, 1024)]` | ||
10. A single decorator for general usage - we should add a `valdiate` decorator which can be used: |
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.
validate
|
||
The following features will remain (mostly) changed: | ||
|
||
* Generics |
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.
I think this should change and would be a very nice point in the "other improvements" section. Now that we stop using cython, we should be able to have
class M(Basemodel, Generic [T])...
instead of the GenericModel
Thanks so much, will fix tonight. |
* first draft of pydantic V2 blog * more blog * blog rendering and formatting * more section * completing conversion table * prompt build * reviewing blog post * more reviewing and extending * recommendations from @Rabscuttler and @PrettyWood * add implementation details and more suggestions * comment about breaking changes * convert namespae to table, more removals * Apply suggestions from code review by @tiangolo Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com> * feedback from @tiangolo's review * changes from @adriangb's review * Apply suggestions from code review Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> * convert namespace info to psuedo-code * rename property, remove schema_json() * adding validation context * remove 'model_schema_json', take 2 * more tweaks while reviewing * comment about pypy and tagged unions * add thanks :prey:, prepare for release * suggestions from @PrettyWood * suggestions from @PrettyWood, model_dump_json comment Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com> Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com>
* generate history from changes, uprev * Pydantic V2 blog (#4218) * first draft of pydantic V2 blog * more blog * blog rendering and formatting * more section * completing conversion table * prompt build * reviewing blog post * more reviewing and extending * recommendations from @Rabscuttler and @PrettyWood * add implementation details and more suggestions * comment about breaking changes * convert namespae to table, more removals * Apply suggestions from code review by @tiangolo Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com> * feedback from @tiangolo's review * changes from @adriangb's review * Apply suggestions from code review Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> * convert namespace info to psuedo-code * rename property, remove schema_json() * adding validation context * remove 'model_schema_json', take 2 * more tweaks while reviewing * comment about pypy and tagged unions * add thanks :prey:, prepare for release * suggestions from @PrettyWood * suggestions from @PrettyWood, model_dump_json comment Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com> Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> * comments mostly from @PrettyWood (#4226) * comments mostly from @PrettyWood * add updated comment * fix pre-commit * allow for shallow copies (#4093) * allow for shallow copies * Add changes file * tweak change * update for comments * rename attr * use single quotes * bump ci * add warning if not a string, switch to string literals * fix linting, prompt ci * fix ci * extend and fix tests * change default to "shallow" Co-authored-by: Samuel Colvin <s@muelcolvin.com> * uprev and prepare for release * linting Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com> Co-authored-by: Zac Hatfield-Dodds <zac.hatfield.dodds@gmail.com> Co-authored-by: Tim Paine <t.paine154@gmail.com>
Blog post I've been promising people for ages about pydantic V2.
This is based off the most recent release so it can be deployed without breaking docs.
Once released this will be merged into the
1.9.X-fixes
branch and eventually cherry-picked into the default branch for posterity.