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

Pydantic V2 blog #4218

Merged
merged 25 commits into from Jul 10, 2022
Merged

Pydantic V2 blog #4218

merged 25 commits into from Jul 10, 2022

Conversation

samuelcolvin
Copy link
Member

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.

@samuelcolvin samuelcolvin changed the base branch from 1.9.X-fixes to master July 6, 2022 17:27
@samuelcolvin samuelcolvin changed the base branch from master to 1.9.X-fixes July 6, 2022 17:28
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
@samuelcolvin
Copy link
Member Author

Thanks so much @PrettyWood, these all look like sensible points, I'll fix them tomorrow.

Copy link
Member

@PrettyWood PrettyWood left a 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!

docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
Copy link

@Rabscuttler Rabscuttler left a 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.

docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
@samuelcolvin
Copy link
Member Author

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.

Copy link
Member

@tiangolo tiangolo left a 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.

docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
Copy link
Contributor

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Exciting times!

docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
Copy link
Contributor

@tomhamiltonstubber tomhamiltonstubber left a comment

Choose a reason for hiding this comment

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

This looks fantastic 💪 🚀

docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
!!! 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.
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member Author

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.

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Amazing! 🚀🎉

docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Show resolved Hide resolved
docs/blog/pydantic-v2.md Outdated Show resolved Hide resolved
@samuelcolvin samuelcolvin marked this pull request as ready for review July 10, 2022 10:46
@samuelcolvin
Copy link
Member Author

This is live 🚀

@samuelcolvin samuelcolvin merged commit 80ec177 into 1.9.X-fixes Jul 10, 2022
@samuelcolvin samuelcolvin deleted the pydantic-v2-blog branch July 10, 2022 10:46
"""
previously `update_forward_refs()`, update forward references
"""
def model_copy(self, ...) -> Self:
Copy link
Member

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

Copy link
Member Author

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,
Copy link
Member

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)]`
Copy link
Member

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`),
Copy link
Member

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:
Copy link
Member

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

@samuelcolvin
Copy link
Member Author

Thanks so much, will fix tonight.

samuelcolvin added a commit that referenced this pull request Aug 8, 2022
* 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>
samuelcolvin added a commit that referenced this pull request Aug 12, 2022
* 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>
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

Successfully merging this pull request may close these issues.

None yet

9 participants