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.
@Carberra6 ай бұрын
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.
@VictorOliveira-lg2qg6 ай бұрын
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
@DuncanBooth6 ай бұрын
@@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.
@ndamu-soft3 ай бұрын
This is a great tip, thanks for sharing!
@ButchCassidyAndSundanceKid6 ай бұрын
The new version of Pydantic is re-written in Rust, should be a lot faster than before.
@Carberra6 ай бұрын
I've seen benchmarks claiming it's about 7x as fast as V1, didn't know that was the reason though!
@ButchCassidyAndSundanceKid6 ай бұрын
@@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.
@irlshrek6 ай бұрын
i think if people have the time they should learn Rust!
@ButchCassidyAndSundanceKid6 ай бұрын
@@irlshrek The question is "IF people have the time".
@gabriellevesque21856 ай бұрын
@@ButchCassidyAndSundanceKid Same League as C 😅
@JohnMitchellCalif5 ай бұрын
super useful! Subscribed
@Carberra5 ай бұрын
Thank you!
@santiagohal67476 ай бұрын
I love this. This is brilliant.
@ayehavgunne6 ай бұрын
I like the library called apischema which lets you use dataclasses to achieve the same thing.
@HerozTech6 ай бұрын
I was recently learning golang and coming back to python i missed struct but pydantic more than makes up for that Great video👍
@mjackstewart5 ай бұрын
Does it include Michaelmas?
@davidmurphy5636 ай бұрын
3:33 It took you a month to release this video? Are they better when they age? ;)
@Carberra6 ай бұрын
Like a fine wine 🍷
@HerozTech6 ай бұрын
one more thing What color scheme are you using, it looks awesome
@konstantinub6 ай бұрын
What's the difference between Pydantic and Marshmallow? Can they be used to accomplish the same things?
@Carberra6 ай бұрын
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.
@illiasukonnik99666 ай бұрын
Excellent video, thanks! Please use caching when playing with external APIs, be polite ))
@pythonwithjames6 ай бұрын
Really nice!
@basb36036 ай бұрын
What theme is that?
@Carberra6 ай бұрын
Link to setup is in the description (:
@aeggeska13 ай бұрын
What is bunting
@nibblrrr71242 ай бұрын
Putting up national flags as decoration. (Had to look it up too.)
@greob6 ай бұрын
Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!
@Carberra6 ай бұрын
You're very welcome!
@ruslanoid6 ай бұрын
TIL about `kebab-case` (until now just called it dashes or hyphens)
@gownerjones6 ай бұрын
Isn't the whole point of it being spelled "pydantic" that you pronounce it "pie-dantic"? :D
@Carberra6 ай бұрын
It may well be, I actually don't know, never heard anyone else say it 😅
@gownerjones6 ай бұрын
@@Carberra Well I'll be pedantic and pronounce it pie-dantic just for the irony lol
@christianlinux82836 ай бұрын
I thought subclass config was deprecated
@Carberra6 ай бұрын
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.
@LSHDackel5 ай бұрын
@@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.