The newer version of Pydantic encourages you to write validators using Annotated. For example: from pydantic import BaseModel, AfterValidator from typing import Annotated def validate_positive(value: int) -> int: assert value > 0, "must be > 0" PositiveInt = Annotated[int, AfterValidator(validate_positive)] class Order(BaseModel): id: int item_name: str quantity: PositiveInt and now you have a reusable type annotation for positive integers.
I've never much liked Annotated, just looks really messy and ugly. That looks neat though -- where in the docs did you find that? Pretty sure it's not in the Validators section.
Even better, Pydantic already provides types like that for convenience, which uses annotations. In this case, you can import PositiveInt like this: from pydantic import PositiveInt
@@Carberra I replied with the link but can't see my reply so perhaps youtube cut out the link. It's under concepts/validators, literally the first thing under the Validators heading.
@@Carberra Holy crap ! I thought it was fast, I didn't know it'd be that fast. I suppose the only other framework it can beat that is the gPRC and protobuf. v1 was written in Python, but v2 was written in Rust which is in the same league as C++, that probably explains about the superior performance.
I'm not sure how functionally different they are, but Marshmallow is certainly designed to used to define ORM-agnostic schemas for databases and convert to and from JSON data. I believe you can do the same with Pydantic, but I haven't used enough of Marshmallow to delve into the differences.
@@Carberra The folks from pydantic put a lot of effort into making the v1 -> v2 migration as painless as possible, so many old things still work but normally give a DeprecationWarning. But DeprecationWarning's in python are by default silenced in user code and only raised in library code. There also is the bump-pydantic project (same github orga as pydantic) to make migrating even easier.
The newer version of Pydantic encourages you to write validators using Annotated. For example:
from pydantic import BaseModel, AfterValidator
from typing import Annotated
def validate_positive(value: int) -> int:
assert value > 0, "must be > 0"
PositiveInt = Annotated[int, AfterValidator(validate_positive)]
class Order(BaseModel):
id: int
item_name: str
quantity: PositiveInt
and now you have a reusable type annotation for positive integers.
I've never much liked Annotated, just looks really messy and ugly. That looks neat though -- where in the docs did you find that? Pretty sure it's not in the Validators section.
Even better, Pydantic already provides types like that for convenience, which uses annotations. In this case, you can import PositiveInt like this:
from pydantic import PositiveInt
@@Carberra I replied with the link but can't see my reply so perhaps youtube cut out the link. It's under concepts/validators, literally the first thing under the Validators heading.
This is a great tip, thanks for sharing!
The new version of Pydantic is re-written in Rust, should be a lot faster than before.
I've seen benchmarks claiming it's about 7x as fast as V1, didn't know that was the reason though!
@@Carberra Holy crap ! I thought it was fast, I didn't know it'd be that fast. I suppose the only other framework it can beat that is the gPRC and protobuf.
v1 was written in Python, but v2 was written in Rust which is in the same league as C++, that probably explains about the superior performance.
i think if people have the time they should learn Rust!
@@irlshrek The question is "IF people have the time".
@@ButchCassidyAndSundanceKid Same League as C 😅
I like the library called apischema which lets you use dataclasses to achieve the same thing.
I was recently learning golang and coming back to python i missed struct but pydantic more than makes up for that
Great video👍
one more thing
What color scheme are you using, it looks awesome
Excellent video, thanks!
Please use caching when playing with external APIs, be polite ))
super useful! Subscribed
Thank you!
Does it include Michaelmas?
I love this. This is brilliant.
What's the difference between Pydantic and Marshmallow? Can they be used to accomplish the same things?
I'm not sure how functionally different they are, but Marshmallow is certainly designed to used to define ORM-agnostic schemas for databases and convert to and from JSON data. I believe you can do the same with Pydantic, but I haven't used enough of Marshmallow to delve into the differences.
3:33 It took you a month to release this video? Are they better when they age? ;)
Like a fine wine 🍷
Really nice!
TIL about `kebab-case` (until now just called it dashes or hyphens)
What theme is that?
Link to setup is in the description (:
Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!
You're very welcome!
My wife left me
What is bunting
Putting up national flags as decoration. (Had to look it up too.)
Isn't the whole point of it being spelled "pydantic" that you pronounce it "pie-dantic"? :D
It may well be, I actually don't know, never heard anyone else say it 😅
@@Carberra Well I'll be pedantic and pronounce it pie-dantic just for the irony lol
Harris Shirley Garcia Karen Perez Frank
Who else had to look up what bunting is?
This guy
How very unpatriotic of you! /j
Jones Lisa Williams Jose Lewis Gary
Brown Nancy Lewis Jason Miller Kenneth
I thought subclass config was deprecated
Huh, so it is. Why did they not remove that in 2.0? I just went with it cos it worked so I thought it hadn't changed.
@@Carberra The folks from pydantic put a lot of effort into making the v1 -> v2 migration as painless as possible, so many old things still work but normally give a DeprecationWarning. But DeprecationWarning's in python are by default silenced in user code and only raised in library code. There also is the bump-pydantic project (same github orga as pydantic) to make migrating even easier.
Explainer is better than code along.
Wilson Ruth Wilson Jeffrey Walker Timothy
Harris Scott Williams Donna Lopez Karen
Hall Jeffrey Jones Frank Rodriguez Susan
Lol. Use a non scripting language.