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 😅
Rodriguez Mary Thompson Brenda Thompson Kimberly
Allen Robert White Jessica Hernandez Timothy
I love this. This is brilliant.
TIL about `kebab-case` (until now just called it dashes or hyphens)
Clark Jason Young Christopher Brown Amy
White Nancy Lee Joseph Brown Kimberly
Hall Jeffrey Jones Frank Rodriguez Susan
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
Brown Nancy Lewis Jason Miller Kenneth
Davis Jose Gonzalez Donald Thomas Helen
Perez Jennifer Hall Daniel Allen Dorothy
I like the library called apischema which lets you use dataclasses to achieve the same thing.
Young Lisa Lewis Jason Robinson Thomas
Jones Lisa Williams Jose Lewis Gary
Garcia Margaret Allen Paul Martinez Michelle
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
Really nice!
Does it include Michaelmas?
What is bunting
Who else had to look up what bunting is?
This guy
How very unpatriotic of you! /j
Excellent video, thanks!
Please use caching when playing with external APIs, be polite ))
3:33 It took you a month to release this video? Are they better when they age? ;)
Like a fine wine 🍷
Explainer is better than code along.
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.
My wife left me
super useful! Subscribed
Thank you!
What theme is that?
Link to setup is in the description (:
Allen Timothy Miller Christopher Martinez George
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.
Jackson Melissa Lopez Jeffrey Miller Timothy
Williams Brian Miller Larry Thomas Amy
Harris Shirley Garcia Karen Perez Frank
Lee Amy White Brian Jones Patricia
Lee Scott Thompson Frank Walker Brenda
Wilson Ruth Wilson Jeffrey Walker Timothy
Harris Scott Williams Donna Lopez Karen
Lol. Use a non scripting language.
Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!
You're very welcome!