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.
@Carberra3 ай бұрын
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-lg2qg3 ай бұрын
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
@DuncanBooth3 ай бұрын
@@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-soft14 күн бұрын
This is a great tip, thanks for sharing!
@ButchCassidyAndSundanceKid3 ай бұрын
The new version of Pydantic is re-written in Rust, should be a lot faster than before.
@Carberra3 ай бұрын
I've seen benchmarks claiming it's about 7x as fast as V1, didn't know that was the reason though!
@ButchCassidyAndSundanceKid3 ай бұрын
@@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.
@irlshrek3 ай бұрын
i think if people have the time they should learn Rust!
@ButchCassidyAndSundanceKid3 ай бұрын
@@irlshrek The question is "IF people have the time".
@gabriellevesque21853 ай бұрын
@@ButchCassidyAndSundanceKid Same League as C 😅
@WatomaSjdob-b6o21 күн бұрын
Rodriguez Mary Thompson Brenda Thompson Kimberly
@DinaDurbin-e2v18 күн бұрын
Allen Robert White Jessica Hernandez Timothy
@JerrellMerica-c8zАй бұрын
Clark Jason Young Christopher Brown Amy
@ThomasMartin-m9xАй бұрын
White Nancy Lee Joseph Brown Kimberly
@ruslanoid3 ай бұрын
TIL about `kebab-case` (until now just called it dashes or hyphens)
@HerozTech3 ай бұрын
one more thing What color scheme are you using, it looks awesome
@JoeyojHolmsopАй бұрын
Hall Jeffrey Jones Frank Rodriguez Susan
@roseesewinter5111Ай бұрын
Allen Timothy Miller Christopher Martinez George
@gownerjones3 ай бұрын
Isn't the whole point of it being spelled "pydantic" that you pronounce it "pie-dantic"? :D
@Carberra3 ай бұрын
It may well be, I actually don't know, never heard anyone else say it 😅
@gownerjones3 ай бұрын
@@Carberra Well I'll be pedantic and pronounce it pie-dantic just for the irony lol
@santiagohal67473 ай бұрын
I love this. This is brilliant.
@CooperArmstrong-y4mАй бұрын
Brown Nancy Lewis Jason Miller Kenneth
@HelenLangworthy-j2t24 күн бұрын
Garcia Margaret Allen Paul Martinez Michelle
@danielsbosworth47418 күн бұрын
Davis Jose Gonzalez Donald Thomas Helen
@DouglasParish-w5mАй бұрын
Perez Jennifer Hall Daniel Allen Dorothy
@NicholasMichaud-u7e27 күн бұрын
Young Lisa Lewis Jason Robinson Thomas
@mjackstewart2 ай бұрын
Does it include Michaelmas?
@ayehavgunne3 ай бұрын
I like the library called apischema which lets you use dataclasses to achieve the same thing.
@HerozTech3 ай бұрын
I was recently learning golang and coming back to python i missed struct but pydantic more than makes up for that Great video👍
@MoniRinaАй бұрын
Jones Lisa Williams Jose Lewis Gary
@pythonwithjames3 ай бұрын
Really nice!
@ChrisAdaline3 ай бұрын
Who else had to look up what bunting is?
@murphygreen84843 ай бұрын
This guy
@Carberra3 ай бұрын
How very unpatriotic of you! /j
@Mywifeleftme32 ай бұрын
My wife left me
@aeggeska123 күн бұрын
What is bunting
@davidmurphy5633 ай бұрын
3:33 It took you a month to release this video? Are they better when they age? ;)
@Carberra3 ай бұрын
Like a fine wine 🍷
@illiasukonnik99663 ай бұрын
Excellent video, thanks! Please use caching when playing with external APIs, be polite ))
@damonguzman2 ай бұрын
Explainer is better than code along.
@konstantinub3 ай бұрын
What's the difference between Pydantic and Marshmallow? Can they be used to accomplish the same things?
@Carberra3 ай бұрын
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.
@JohnMitchellCalif2 ай бұрын
super useful! Subscribed
@Carberra2 ай бұрын
Thank you!
@basb36033 ай бұрын
What theme is that?
@Carberra3 ай бұрын
Link to setup is in the description (:
@ЕфросинияКуклева18 күн бұрын
Jackson Melissa Lopez Jeffrey Miller Timothy
@NatalieMorgan-q9rАй бұрын
Harris Shirley Garcia Karen Perez Frank
@DennisJoanna-s3x17 күн бұрын
Williams Brian Miller Larry Thomas Amy
@ChaucerGrace-r1j28 күн бұрын
Lee Amy White Brian Jones Patricia
@BealleMoriniEАй бұрын
Wilson Ruth Wilson Jeffrey Walker Timothy
@CarmenMuniz-m2xАй бұрын
Lee Scott Thompson Frank Walker Brenda
@christianlinux82833 ай бұрын
I thought subclass config was deprecated
@Carberra3 ай бұрын
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.
@LSHDackel2 ай бұрын
@@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.
@SusiePerez-d7oАй бұрын
Harris Scott Williams Donna Lopez Karen
@shadmansudipto72873 ай бұрын
Lol. Use a non scripting language.
@greob3 ай бұрын
Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!