Pydantic is OP, here's why

Поделиться
HTML-код
  • Опубликовано: 19 янв 2025

Комментарии • 64

  • @DuncanBooth
    @DuncanBooth 6 месяцев назад +30

    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.

    • @Carberra
      @Carberra  6 месяцев назад +1

      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-lg2qg
      @VictorOliveira-lg2qg 6 месяцев назад +4

      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

    • @DuncanBooth
      @DuncanBooth 6 месяцев назад +1

      @@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-soft
      @ndamu-soft 4 месяца назад

      This is a great tip, thanks for sharing!

  • @ButchCassidyAndSundanceKid
    @ButchCassidyAndSundanceKid 6 месяцев назад +40

    The new version of Pydantic is re-written in Rust, should be a lot faster than before.

    • @Carberra
      @Carberra  6 месяцев назад +7

      I've seen benchmarks claiming it's about 7x as fast as V1, didn't know that was the reason though!

    • @ButchCassidyAndSundanceKid
      @ButchCassidyAndSundanceKid 6 месяцев назад +1

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

    • @irlshrek
      @irlshrek 6 месяцев назад

      i think if people have the time they should learn Rust!

    • @ButchCassidyAndSundanceKid
      @ButchCassidyAndSundanceKid 6 месяцев назад +1

      @@irlshrek The question is "IF people have the time".

    • @gabriellevesque2185
      @gabriellevesque2185 6 месяцев назад

      ​@@ButchCassidyAndSundanceKid Same League as C 😅

  • @ayehavgunne
    @ayehavgunne 6 месяцев назад +1

    I like the library called apischema which lets you use dataclasses to achieve the same thing.

  • @HerozTech
    @HerozTech 6 месяцев назад +1

    one more thing
    What color scheme are you using, it looks awesome

  • @mjackstewart
    @mjackstewart 6 месяцев назад

    Does it include Michaelmas?

  • @JohnMitchellCalif
    @JohnMitchellCalif 6 месяцев назад

    super useful! Subscribed

    • @Carberra
      @Carberra  6 месяцев назад

      Thank you!

  • @santiagohal6747
    @santiagohal6747 6 месяцев назад

    I love this. This is brilliant.

  • @HerozTech
    @HerozTech 6 месяцев назад +1

    I was recently learning golang and coming back to python i missed struct but pydantic more than makes up for that
    Great video👍

  • @davidmurphy563
    @davidmurphy563 6 месяцев назад +1

    3:33 It took you a month to release this video? Are they better when they age? ;)

    • @Carberra
      @Carberra  6 месяцев назад +6

      Like a fine wine 🍷

  • @konstantinub
    @konstantinub 6 месяцев назад

    What's the difference between Pydantic and Marshmallow? Can they be used to accomplish the same things?

    • @Carberra
      @Carberra  6 месяцев назад +1

      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.

  • @illiasukonnik9966
    @illiasukonnik9966 6 месяцев назад

    Excellent video, thanks!
    Please use caching when playing with external APIs, be polite ))

  • @aeggeska1
    @aeggeska1 4 месяца назад

    What is bunting

    • @nibblrrr7124
      @nibblrrr7124 2 месяца назад

      Putting up national flags as decoration. (Had to look it up too.)

  • @pythonwithjames
    @pythonwithjames 6 месяцев назад

    Really nice!

  • @basb3603
    @basb3603 6 месяцев назад

    What theme is that?

    • @Carberra
      @Carberra  6 месяцев назад

      Link to setup is in the description (:

  • @greob
    @greob 6 месяцев назад +1

    Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!

    • @Carberra
      @Carberra  6 месяцев назад

      You're very welcome!

  • @ruslanoid
    @ruslanoid 6 месяцев назад +1

    TIL about `kebab-case` (until now just called it dashes or hyphens)

  • @gownerjones
    @gownerjones 6 месяцев назад +2

    Isn't the whole point of it being spelled "pydantic" that you pronounce it "pie-dantic"? :D

    • @Carberra
      @Carberra  6 месяцев назад

      It may well be, I actually don't know, never heard anyone else say it 😅

    • @gownerjones
      @gownerjones 6 месяцев назад +3

      @@Carberra Well I'll be pedantic and pronounce it pie-dantic just for the irony lol

  • @christianlinux8283
    @christianlinux8283 6 месяцев назад +1

    I thought subclass config was deprecated

    • @Carberra
      @Carberra  6 месяцев назад

      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.

    • @LSHDackel
      @LSHDackel 6 месяцев назад

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

  • @Mywifeleftme3
    @Mywifeleftme3 6 месяцев назад +2

    My wife left me

  • @ChrisAdaline
    @ChrisAdaline 6 месяцев назад +10

    Who else had to look up what bunting is?

  • @NatalieMorgan-q9r
    @NatalieMorgan-q9r 4 месяца назад

    Harris Shirley Garcia Karen Perez Frank

  • @MoniRina
    @MoniRina 4 месяца назад

    Jones Lisa Williams Jose Lewis Gary

  • @CooperArmstrong-y4m
    @CooperArmstrong-y4m 4 месяца назад

    Brown Nancy Lewis Jason Miller Kenneth

  • @damonguzman
    @damonguzman 6 месяцев назад

    Explainer is better than code along.

  • @BealleMoriniE
    @BealleMoriniE 4 месяца назад

    Wilson Ruth Wilson Jeffrey Walker Timothy

  • @SusiePerez-d7o
    @SusiePerez-d7o 5 месяцев назад

    Harris Scott Williams Donna Lopez Karen

  • @JoeyojHolmsop
    @JoeyojHolmsop 4 месяца назад

    Hall Jeffrey Jones Frank Rodriguez Susan

  • @shadmansudipto7287
    @shadmansudipto7287 6 месяцев назад

    Lol. Use a non scripting language.