Pydantic Tutorial • Solving Python's Biggest Problem

Поділитися
Вставка
  • Опубліковано 22 тра 2024
  • Learn how to use Pydantic in this short tutorial!
    Pydantic is the most widely used data validation library for Python. It lets you structure your data, gives you type-hints and auto-complete in your IDE, and helps to serialize to and from JSON. Learn how to use it in just 10 minutes!
    👉 Links
    🔗 Pydantic Docs: docs.pydantic.dev/
    🔗 Pydantic GitHub: github.com/samuelcolvin/pydantic
    📚 Chapters
    00:00 Python's Dynamic Typing Problem
    02:11 How To Use Pydantic
    05:04 Validating Data with Pydantic
    06:36 Custom Field Validation
    07:58 JSON Serialization
    08:49 Pydantic vs Dataclasses
    #pixegami #python

КОМЕНТАРІ • 283

  • @UTJK.
    @UTJK. 5 місяців тому +22

    Best moments of the video:
    7:22 the whole example of Pydantic with data and custom fields validation
    9:12 the alternative built-in Python datclasses
    9:33 discussing differences between Pydantic and built-in dataclasses

  • @NikolajLepka
    @NikolajLepka 5 місяців тому +16

    it's hilarious how many decades it took for python users to understand the benefit of strict typing

    • @pixegami
      @pixegami  5 місяців тому +4

      I suspect it's not native Python users bringing that change, but users of other strictly typed languages coming into Python and feeling like something is missing 😬

    • @OctagonalSquare
      @OctagonalSquare 4 місяці тому +7

      I think it’s hilarious that non-Python users think dynamic typing is a negative

    • @NikolajLepka
      @NikolajLepka 4 місяці тому

      @@OctagonalSquare if it wasn't, python users wouldn't be working so hard to put static typing into the language

    • @shaurryabaheti
      @shaurryabaheti 4 місяці тому

      i love dynamic typing... as much as it slows my code, i can handle errors better and create my own errors and etc... ints also have no bounds (they have, virtually but you can bypass it), extremely easy data modification support, etc etc etc... so no I'm not fighting to make it static... dynamic is what makes python python ​@@NikolajLepka

    • @MikeHurt
      @MikeHurt 2 місяці тому +2

      ​@@NikolajLepka there's nothing inherently better, or worse for that matter, with either static or dynamic typing. Users bring their own biases and preferences.

  • @user-mf1xr5ki9j
    @user-mf1xr5ki9j 7 місяців тому +30

    Nice video,
    -not too big, not to short
    -presents the most popular points without sticking to details
    -compares to alternatives
    Thank you!

    • @pixegami
      @pixegami  7 місяців тому +1

      Thank you!

  • @002_aarishalam8
    @002_aarishalam8 5 місяців тому +11

    The pydantic model still does not do strict typechecking , for eg you can pass int to a class of pydantic model which accepts str , it'll typecast the int to str and pass the check. Although there is a solution which you can use , you can import StrictStr from pydantic for such cases

  • @pythonantole9892
    @pythonantole9892 8 місяців тому +39

    The explanation on the why and what problems Pydantic solves is one of the best that i have seen. I just had to subscribe!

  • @ChelseaSaint
    @ChelseaSaint 8 місяців тому +41

    You have a gift to explain complex things in a very simple to understand way.... great video
    Keep up the awesome work 💪

    • @pixegami
      @pixegami  7 місяців тому +1

      Thank you! Glad you enjoyed it :)

  • @theintjengineer
    @theintjengineer 6 місяців тому +12

    Coming from C++ and Rust, it's not that I want to work with Types, I kinda cannot work without them haha. So, that's the first topic I looked for when I had to do some stuff in Python.
    Well-made video, Mate.
    Thanks.

    • @pixegami
      @pixegami  6 місяців тому +1

      Thanks :) I totally understand - once you've come to rely on type-defs it's harder to work without them.

  • @jabuci
    @jabuci 6 місяців тому +2

    Best intro to pydantic I've seen so far. Thanks!

    • @pixegami
      @pixegami  6 місяців тому

      Glad you enjoyed it!

  • @jimg8296
    @jimg8296 Місяць тому

    Crap! First 4 videos of yours I watch and they all solve real world problems I have been
    facing. Freak'n AWESOME! Thank you. FYI I'm coming from the Typescript world. Saves a lot of custom decorators we create.

  • @DaniEIdiomas
    @DaniEIdiomas 5 місяців тому +2

    Very nice. Had been working with some parts of dataclasses in the past and used guard clauses instead of validators. Will try to use it in the future. Thank you

    • @pixegami
      @pixegami  5 місяців тому

      Thanks! I hope it is useful for you :)

  • @chakib2378
    @chakib2378 4 місяці тому +2

    Excellent video. Thank you for getting straight to the point with clarity.

  • @sheshanaturupana2481
    @sheshanaturupana2481 5 місяців тому +4

    Thanks for the hint and this will less stress me finding solutions to what you have explained. Keep it up you have a natural gift to explain in a way that anyone can understand

    • @pixegami
      @pixegami  5 місяців тому

      Thank you :) I appreciate the comment!

  • @jason6569
    @jason6569 6 місяців тому +4

    This is being improved upon in 3.12. I did not use it but I saw something about it in patch notes. I could be wrong but it is nice of Python to actually fix things if this is the case!

    • @pixegami
      @pixegami  6 місяців тому +3

      Actually my biggest issue (type hinting) is already available in Python since 3.8 and I've found it good enough for the most part. If I need more, I go for @dataclass decorator. But I think overall, a lot of frameworks and teams still use Pydantic for the validation too.

  • @gatorpika
    @gatorpika 4 місяці тому +1

    Wow, never understood that stuff until I watched this and it's pretty simple the way you explain it. Thanks...subbed!

    • @pixegami
      @pixegami  3 місяці тому

      Awesome, thank you!

  • @user-vz9cq8ci9b
    @user-vz9cq8ci9b 8 місяців тому +11

    Mypy solves kind of a similar problem (regarding validation), though with static type checking. It is generally more useful in my opinion, but yeah, pydantic seems to be a great tool for runtime validation

    • @pixegami
      @pixegami  7 місяців тому +1

      Sure, there's a lot of tools that solve similar problems, and I'm not saying Pydantic is necessarily the best one. Definitely use what works best for you (and if Python's inbuilt features are enough, even better).

  • @Nardiso
    @Nardiso 5 місяців тому +1

    Man, awesome content like I never saw before! Really, really good! Thank you!

    • @pixegami
      @pixegami  5 місяців тому

      Glad you enjoyed it!

  • @drewsarkisian9375
    @drewsarkisian9375 8 місяців тому +11

    Either use a statically-typed language if you absolutely need one OR use available tools to add typedefs. Dynamic typing is a feature, not a problem.

    • @Holasticlogger
      @Holasticlogger 7 місяців тому +3

      That's what I believe too and that's make it special too tbh !!

    • @pixegami
      @pixegami  7 місяців тому +3

      Perhaps it was unfair for me to call "lack of static typing" a problem. The real issue I'm referring to is the user experience from having type hints and validation.
      "Use a statically-typed language" - Although this is a nice user experience, it's rarely the top reason for choosing a language (for me anyways) - especially over say, frameworks or domain utility.
      "Use available tools to add typedefs" - Sure, if you only need typedefs, Python's inbuilt support is good enough. If you also want validation and deep serialisation too-well, at what point is re-implementing all those features easier/better than a pip install?

    • @SOLDAT_MENDES
      @SOLDAT_MENDES 7 місяців тому +2

      totally agree, dynamic typing can be powerful and flexible. if you need stricter type checking, statically-typed languages or tools like typedefs can be helpful. it's all about choosing the right tool for the job.

    • @blackdereker4023
      @blackdereker4023 5 місяців тому +2

      @@SOLDAT_MENDES The good thing about pydantic is that it's possible to have strict type checking and validations without having to change the whole language of your Python project. A lot of times it's better to install an extra library than to refactor your entire project.

  • @kentuckeytom
    @kentuckeytom 3 місяці тому +1

    very clear and concise, well organized, thanks.

  • @luftstolle
    @luftstolle 5 місяців тому +1

    Just wanted to say I love your video! Learned something new!

    • @pixegami
      @pixegami  5 місяців тому

      Thank you! Glad it was helpful :)

  • @alexhasha
    @alexhasha 5 місяців тому +3

    I love the production value of your videos (in addition to the content, of course!) I’m curious what tools you like for recording and editing videos?

    • @pixegami
      @pixegami  5 місяців тому +1

      Thank you! I use OBS studio to record, and I use Premiere Pro to edit. Actually my editing is really light and minimal (just direct cuts) so any editing software is fine.

  • @GilbertoMadeira83
    @GilbertoMadeira83 4 місяці тому +1

    Amazing video, I am starting my journey in python, and pydantic appeared to me in a fastapi course, I was so confused, but now I fully understand its utility, thank you!

    • @pixegami
      @pixegami  3 місяці тому

      Yay, glad it was helpful!

  • @JohnMitchellCalif
    @JohnMitchellCalif 3 місяці тому +1

    very useful! I'll start adding Dataclass support to my code, and when it matters, Pydantic.
    Subscribed!

    • @pixegami
      @pixegami  3 місяці тому

      Thank you, glad it was useful :)

  • @shedrachugochukwu6245
    @shedrachugochukwu6245 23 дні тому +1

    Dynamic typing for me is what makes python cool. If i need to switch data type, i just cast it to the type if the value are the same

    • @pixegami
      @pixegami  11 днів тому

      Hints and validation are still good for more complex data-types that can't be casted (think nested objects), or tuples where the order matters.
      And if you want a field that can understand multiple data types you can also use something like "Union[str, int]" or "str | int" in the type hint.

  • @christophehanon8396
    @christophehanon8396 2 місяці тому +1

    Very clear explanation - thanks for sharing !

  • @cyberslot
    @cyberslot 5 місяців тому +2

    Awesome Pydantic summary - concise and quintessential.
    Actually in Pydantic v2 '@validator' is deprecated and replaced with '@field_validator'.
    The same is valid for these three:
    - 'json' method is deprecated and replaced with 'model_dump_json' instead.
    - 'dict' method is deprecated and replaced with 'model_dump' instead.
    - 'parse_raw' method is deprecated and replaced with 'model_validate_json' instead.
    Have that in mind.
    Amended code snippet accordingly:
    user_json_str = user.model_dump_json()
    user_json_obj = user.model_dump()
    user = User.model_validate_json(user_json_str)
    print(f'{user_json_str}
    {user_json_obj}
    {user}')

    • @pixegami
      @pixegami  5 місяців тому

      I see - thank you for sharing those updates :)

    • @cyberslot
      @cyberslot 5 місяців тому

      @@pixegami You're very welcome! :)

    • @aryangoyal5176
      @aryangoyal5176 3 місяці тому

      @field_validator("id")
      def validate_id(cls, value: int) -> int:
      if value

  • @Andromeda26_
    @Andromeda26_ Місяць тому +1

    Great Video! Thank you! Thank you! Keep up the great work!

    • @pixegami
      @pixegami  Місяць тому

      Glad you enjoyed it!

  • @404nohandlefound
    @404nohandlefound 5 місяців тому +11

    I wouldn’t call dynamic typing as an “issue”. It’s just how the language is, with its own design.

    • @pixegami
      @pixegami  5 місяців тому +1

      True, it’s not really the issue. It’s just an oversimplification of what the real issue is: the user experience.

  • @luis96xd
    @luis96xd 4 місяці тому +1

    Amazing video, everything was well explained, thanks!

    • @pixegami
      @pixegami  3 місяці тому +1

      Glad you enjoyed it!

  • @FrederikSchumacher
    @FrederikSchumacher 5 місяців тому +17

    "One of the biggest issues is Python's lack of static typing" You got that the wrong way around. The biggest issue in most typed languages is that the typing system is static. Python does something incredibly useful: value and runtime typing. Static typing is not the end-game of programming language systems, and the assumption that static typing will make programs fault-free is flawed.
    Without this assumption static typing is left with: being useful for code completion in IDEs, efficiency optimization in resource-constrained environments, and documentation. Python once again does something incredibly useful here: type annotations. Type annotations support the code completion case, and support the code documentation case, while also being part of the runtime information which can be leverages at runtime to perform validation or type checking. As for resource-constraints, it's Python, an interpreted language, if resource constraints where an issue, you wouldn't be using Python or any other interpreted language.
    Reflect on these questions: When and where do the errors happen? And what am I doing about it?
    The answers are: During runtime when in use. And adding user input validation code. Notice the absence of static or "compile time typing".
    Most static typing can only detect faults during compile time and not during runtime, since during runtime most static typed languages don't have typing information. Either the language compiler doesn't add this typing information, or the developers prefer to remove the typing information for runtime for various "optimization" reasons. Some examples of this: TypeScript is only static typed, yet the core runtime, JavaScript, has a very loose runtime type concept, and the TypeScript static types are usually stripped in transpilation. C has static typed compilers, but assembly or hex-code itself is only minimally typed (bits, bytes, addresses).
    Additionally, static typing systems in the most common languages are incredibly naive and in some cases counter-productive. Classic and everyday examples of this can be found in the Java-memes. These memes are founded on the reality of regular Java code regularly requiring extensive type coupling code in the form of Interface/Adapter/Factory and the resulting coupling code, solely transforming namespaced data types from almost identical static type hierarchy to another. In professional environment, this code incurs heavy technical depths: it makes coupling two or more systems incredibly brittle, and is a great source of simple programming errors. Another feature that points to problems with static typing systems are Generics. A complex and error-fraud feature in many languages, developed to somehow improve code reusability while mostly simply increasing code base complexity.
    In addition to Python's type annotations, other solutions developed from these typing problems are Test Driven Development and Unit Testing. These cover the runtime problems that are not covered by static typing. Another great solution to the coupling types problem are Go Interfaces.
    Just to make this clear: Static typing does provide value, but it's extremely specific where and when it provides that value. And it's these specifics exactly that don't make static typing the ideal solution to programming language complexity.

    • @pixegami
      @pixegami  5 місяців тому +3

      A thoughtful deep-dive into the topic and good counter-arguments for what's claimed in the video. I appreciate you sharing these thoughts (and I don't disagree with them either - I felt my script may have been too simplified/opinionated and lost a lot of critical detail).

    • @FrederikSchumacher
      @FrederikSchumacher 5 місяців тому +1

      @@pixegami I appreciate you taking time to answer in such a reflective and respectful way. Other than the premise at the very start, your video is a great introduction to validation and introduction to Pydantic. I enjoyed you including that example for email address validation, which highlights one of the issues with typing systems and validation systems very well. It's an example I use regularly when discussing typing and teaching junior developers, because it highlights the confusion of typing vs validation very well. Validation can be expressed via (static) types, but it must feature runtime code. Some languages like Python allow implementing this via typing, while others (like C/C++) cannot or only with great difficulty. And while Pydantic presents this as declarative feature looking like (static) typing, it constructs code that's pretty much pure runtime.
      The video title is a little provocative and click-baity, but can't say it's incorrect, as input validation is one of the most common problems developers have to solve in many applications in many languages - which includes Python. Even as viewer I understand how "the algoritm" pushes creators into doing this, I just wish they didn't have to.
      Here's an idea if you want to explore both validation and decorators more perhaps in a future video on advanced Python - an extension to your previous primer video on decorators and this video: You can leverage the type annotations and decorators to implement "functional" validation on function arguments to enable code like:
      @validate_args
      def cool_func(person_value: has_keys("mail"), mail_value: matches_pattern("\w+@\w+")):
      person_value["mail"] = mail_value
      This can of course be extended into less trivial and less nonsensical applications. It's mostly a more "functional" approach to the classic declarative types approach and more an exercise. Personally I mostly prefer declarative types for this sort of thing. However, I find such code examples help understand why and how several Python concepts exist in the form they do.

  • @RuudJeursen
    @RuudJeursen 4 місяці тому +1

    Thanks, nice & clear explanation!

    • @pixegami
      @pixegami  4 місяці тому

      Glad you enjoyed it!

  • @balapraneeth9708
    @balapraneeth9708 4 місяці тому +1

    Great explanation!! Thanks for creating such as amazing content

    • @pixegami
      @pixegami  3 місяці тому

      Thank you, glad you liked it!

  • @thebuggser2752
    @thebuggser2752 3 місяці тому +1

    Very well presented. Thanks!

    • @pixegami
      @pixegami  3 місяці тому

      Thanks for watching!

  • @alexanderzikal7244
    @alexanderzikal7244 4 місяці тому +1

    Very useful, Thank You!

    • @pixegami
      @pixegami  3 місяці тому

      Glad to hear that!

  • @nas8318
    @nas8318 19 днів тому +1

    If you want static typing in Python just use Numpy. It's both faster and takes less memory.

    • @pixegami
      @pixegami  11 днів тому

      Numpy is excellent, but I think that's solving a different problem for people who want to add type hints and validation to their custom data types.

  • @ahmednfaihi9091
    @ahmednfaihi9091 5 місяців тому +1

    Great explanation. Btw which vs code theme u r using?

    • @pixegami
      @pixegami  5 місяців тому

      Monokai Pro :)

  • @ananyamahapatra6597
    @ananyamahapatra6597 21 день тому +1

    Thank you so very much buddy 🤩 its a great help 🥳

    • @pixegami
      @pixegami  11 днів тому

      Thank you, glad you liked it!

  • @mayanksharma6927
    @mayanksharma6927 4 місяці тому

    Great video, thanks a lot!
    I hear some background noise in your audio though. Do you not use audacity to remove the background noise?

    • @pixegami
      @pixegami  3 місяці тому

      Thanks! I live in quite a loud area, so a lot of noise does get through. I've used software to remove it in the past, but sometimes it does make the sound a bit less crisp. Thanks for the feedback though, I'm working on it :)

    • @mayanksharma6927
      @mayanksharma6927 3 місяці тому

      ​@@pixegamiI sometimes use the app Dolby On to remove noise, it's very good.

  • @footflaps
    @footflaps 2 місяці тому

    Very helpful!

  • @cjbarroso
    @cjbarroso 6 місяців тому +5

    love your explanation, as advanced programmer I don't feel I spent too much time to learn something useful. Keep it up! You have a new subscriber.

    • @pixegami
      @pixegami  6 місяців тому

      Glad to hear it :) Thank you!

  • @romsthe
    @romsthe 8 місяців тому +2

    I don't remember exactly why, but I prefer attrs. Could you compare both ? With custom validation, custom initialization for fields, also post init processing ? I think that's all that I'm using. I'm only deserializing from yaml though

    • @pixegami
      @pixegami  7 місяців тому

      I haven't used attrs myself, but I understand it's a bit more heavyweight than the inbuilt dataclasses. Not sure how it differs from Pydantic, but it's probably something I should compare and check out :)

  • @dogzabob
    @dogzabob 3 місяці тому +1

    Hey man, I love the tutorial. It's very easy to follow along with your explanations.
    I do have one issue in that when attempting to use your @validator function in pycharm and kaggle it does not seem to work and still allows me to enter negative numbers without raising an error.
    i changed the value to

    • @pixegami
      @pixegami  3 місяці тому

      Thank you! Glad you're enjoying the video. Hmm, maybe there's an issue with the way you've set up the validator, but it's hard to say without seeing your code.
      I recommend dropping your code into ChatGPT and ask to see if it can spot the bug. But the best thing to do is probably to read through the manuals directly and understand how the validators work and how to debug them: docs.pydantic.dev/latest/concepts/validators/

  • @guohaigao2397
    @guohaigao2397 4 місяці тому +1

    Thank you , like this video so much.

    • @pixegami
      @pixegami  4 місяці тому

      Glad you liked it!

  • @claudemirtonmacedo6639
    @claudemirtonmacedo6639 5 місяців тому +1

    Awesome video. But what about including Marshmallow on your comparison matrix?

    • @pixegami
      @pixegami  5 місяців тому

      Thanks for the suggestion. I haven't used it before. But I'll leave a link here for anyone who is reading and is curious: marshmallow.readthedocs.io/en/stable/why.html

  • @youssef.elyaakoubi
    @youssef.elyaakoubi 18 днів тому +1

    Thanks Bro!

  • @yashwanthsai3401
    @yashwanthsai3401 5 місяців тому +1

    Thanks. U have explained very well

  • @a0um
    @a0um 5 місяців тому +1

    I’m wondering how it compares with the typing package included in Python.
    And, is the typing module the same as mypy?

    • @pixegami
      @pixegami  5 місяців тому +1

      I think for just the "typing" part, Pydantic doesn't really give you much advantage over the built-in Python type hints. The real value add of Pydantic comes from its validation and serialisation abilities-and the use case is integrating with external databases or APIs with your data :)

  • @fatmasliti2161
    @fatmasliti2161 5 місяців тому +1

    great explanation!

  • @adam_fakes
    @adam_fakes 4 місяці тому +5

    Nice video, though lack of static typing is not a problem, it is a feature. It was done on purpose. Like JavaScript vs TypeScript, I would rather see Tython (Typed Python).

    • @catsupchutney
      @catsupchutney 4 місяці тому

      Yes, these bugs/features are very much a non issue, meaning it's a facet of the language that is lauded as much as it is hated. One could consider Sqlite's lack of types to be a feature, or two pass versus one pass interpreters; neither is better. Python occupies that odd zone where it might be used for a ten line convenience script, or for the basis of a serious application.

    • @pixegami
      @pixegami  3 місяці тому

      I think it's more of a "UX" problem for developers who are used to the nice experience that static typing or typed languages give you. So it's not really a big deal, but it does make the experience better for many people.

  • @morespinach9832
    @morespinach9832 24 дні тому +1

    Not sure what the fascination with type is. It’s a helpful feature to not have to type variables. The web in general benefits from this, if one is sensible and clever in how to use this - and when validation of specific types is needed, induce it.

    • @pixegami
      @pixegami  11 днів тому +1

      It's think just a big "user experience" problem for a lot of people who are more comfortable working with types/validation.

  • @jteds711
    @jteds711 6 місяців тому +2

    New here, also not a software engineer by trade. Is this essentially allowing you to create structs like you would in golang? Learning here, just seemed familiar in its use case. Any commentary is welcomed. Thanks!

    • @pixegami
      @pixegami  6 місяців тому

      Welcome! Yes, it's quite similar. Structs is built into Golang (which is "typed" by design). Python is dynamic (or "untyped"), so to get similar functionality you need to add it in deliberate (using something like Pydantic).

    • @BosonCollider
      @BosonCollider 5 місяців тому +1

      @@pixegami The other similarity is that Golang's marshalling in the standard library also provides the serialization/deserialization part (which is what pydantic actually offers as opposed to dataclass libraries like attrs).

    • @pixegami
      @pixegami  5 місяців тому

      @@BosonCollider Oh that’s cool, I’m not super familiar with Go and didn’t know they did JSON marshalling by default. TIL!

  • @mohibahmed5098
    @mohibahmed5098 23 дні тому +1

    I love your videos man. Just a quick question. Can't we define the default types of our instances in python for example
    x = 10
    x: int = 10

    • @pixegami
      @pixegami  11 днів тому

      Thanks! Glad you enjoyed it. Yes if you just want type hints, that's exactly what I'd do. Pydantic becomes more useful if you need validation, or you need to work with APIs or databases.

  • @krzysiekkrzysiek9059
    @krzysiekkrzysiek9059 8 місяців тому +7

    Dynamic typing is what Python stands for. I use type hints only in functions or methods parameters for better understanding to other developers.

    • @pixegami
      @pixegami  7 місяців тому +1

      I agree - I just wanted to highlight the value of having type hints and validation. Normally for my own projects, unless I need all three Pydantic features I mentioned (serialisation, validation, and typing), just the built-in type hinting is good enough.

    • @blackdereker4023
      @blackdereker4023 5 місяців тому +1

      When you are building an API or a CLI application, validations is a must.

  • @fengjeremy7878
    @fengjeremy7878 4 місяці тому +1

    Good tutorial!

  • @AdityaDodda
    @AdityaDodda 4 місяці тому +3

    How do these things affect performance? I'd assume they add a good amount of overhead.
    If you are going to take the pain of making things statically typed, why not just used a language that is efficient with it. Kotlin is a great bet.
    Is the use case to have the external api typed strongly with internals relaxed so that you can stay in Python and still use it's libraries for an application?
    Just curious.

    • @pixegami
      @pixegami  4 місяці тому +1

      Thanks for the great question. The performance hit is not noticeable (probably a few microseconds on each request at most) - and I don't think there's any Python use-cases where that would matter (if milli-sec latency was a concern, then Python itself already isn't a good fit).
      As for using a (different) statically typed language - sure, if you are picking a new language to learn or starting a new project. But Python has a lot of libraries/SDKs/support that do not exist in other languages. It also has high adoption. Some people may just want to continue using Python but just want to upgrade this one part of the user experience.
      I think the best use-case for Pydantic is for writing Python logic that interacts with other services (or databases) via API, and you want an easy way to validate/serialize your data.

  • @bobchannell3553
    @bobchannell3553 3 місяці тому +1

    The bigger problem is the GIL. Although I can really see how accidentally changing a variable's type might cause a difficult to spot problem in a large program.

    • @pixegami
      @pixegami  3 місяці тому +1

      Haha yes, truly the "biggest problem" is a subjective thing. But the static typing thing is just something I hear people complain about most often (anecdotally).

  • @nulops
    @nulops 4 місяці тому +1

    Excellent vidéo. What tools do you use to create this kind of content

    • @pixegami
      @pixegami  3 місяці тому

      Thank you! I use OBS to record the video/screen, Adobe Premiere to edit it, and I also have a couple of my own Python scripts to create slides/titles/diagrams.

  • @UTJK.
    @UTJK. 5 місяців тому +1

    Nice presentation.

    • @pixegami
      @pixegami  5 місяців тому +1

      Glad you liked it

  • @deepschoolai
    @deepschoolai 3 місяці тому +1

    how do you create those code boxes? It looks so good!

    • @pixegami
      @pixegami  3 місяці тому +1

      I used a custom script in Python, using libraries like pygments.org/ and PIL :)

  • @PaulaBean
    @PaulaBean 2 місяці тому +2

    I would say the 'lack of static typing' is a feature, not a bug of the Python language.

    • @pixegami
      @pixegami  2 місяці тому

      I think everyone has different views about the technology and implementation itself. But if we consider the actual user experience, I don't think anyone _prefers_ to have errors show up late in the runtime, or lack the type hints at coding time.

    • @PaulaBean
      @PaulaBean 2 місяці тому

      @@pixegami Indeed. I think Swift hits this sweet spot.

  • @tirsky
    @tirsky 5 місяців тому +2

    Это всё называется - костыли)

  • @PeterRichardsandYoureNot
    @PeterRichardsandYoureNot 4 місяці тому +1

    Ok, I was thinking what’s the need for this if you are organized. But, when you said that the library has built in format validation for things like emails I now have a lightbulb above my head that has literally erased so many lines of code later using any type of my own format validation.

    • @pixegami
      @pixegami  3 місяці тому

      Thanks for sharing that lightbulb moment :) I'm glad the example helped it to click.

  • @Dopefeezy
    @Dopefeezy 8 місяців тому +1

    Can pydentic work with the terminal editors like vim or neovim?

    • @pixegami
      @pixegami  7 місяців тому

      I'm not sure, I haven't really used them. But I think all of the type hinting features in an IDE come from having a language server or linter anyway, so I think it should be possible.

  • @thygrrr
    @thygrrr 6 місяців тому +2

    1:24, so age is, in fact, not just a number. It's settled now.

    • @pixegami
      @pixegami  6 місяців тому

      It never was :P

  • @jairajsahgal7101
    @jairajsahgal7101 5 місяців тому +1

    Thank you

    • @pixegami
      @pixegami  5 місяців тому

      You're welcome!

  • @ThankYouESM
    @ThankYouESM 2 місяці тому

    I always do user_input = int(user_input) etc after each input only where needed which prevents every such error.

  • @chriskeo392
    @chriskeo392 5 місяців тому

    We can mix wit Sqlalchemy models?

  • @LeeYeon-qv1tz
    @LeeYeon-qv1tz 4 місяці тому +1

    Do we really need data validation? At 5:55 ? I mean, we can change the data type of age variable when we get from user, right? Like age= int(age) . Correct me if iam wrong.

    • @pixegami
      @pixegami  3 місяці тому +1

      What you're suggesting is "casting" the data. It works if it's small transitions of things like ints to floats. But the validation becomes useful if it's something more complex like named dicts, such as (e.g. "vCPU: [1024, 2048, 9096]"), or percentages (e.g. must be between 0-100).

    • @LeeYeon-qv1tz
      @LeeYeon-qv1tz 3 місяці тому

      ​@@pixegamithanks!

  • @thebatprem9271
    @thebatprem9271 5 місяців тому +1

    I have experience using Pydantic, but it's somehow wordy for me especially if I don't have a plan to do OOP. My preference would be "mypy" (or pytype) + "typedict" instead.

    • @pixegami
      @pixegami  5 місяців тому

      Yes. Truthfully I just stick to dataclasses since that’s good enough for 90% of my use cases too. But here I just wanted to open with a common problem as a way to introduce Pydantic.

  • @hananamar2686
    @hananamar2686 5 місяців тому +2

    I like pydantic a lot. But when working on projects where packages size is a consideration, I couldnt understand why such lightweight functionality requires code that is so heavy. That's a bummer

    • @pixegami
      @pixegami  5 місяців тому

      Hmm, what do you mean by "heavy"? As in the implementation of Pydantic itself?
      On that note-if you just want type hinting, Python has that built-in. And if you need structs, @dataclass will do it for you (also built in). But if you need validation and serialisation that's probably when Pydantic might be useful.

  • @amirjamal4803
    @amirjamal4803 3 місяці тому +1

    "If your software has to fail, then it is better to fail as early as possible"

  • @iwatchtvwithportal5367
    @iwatchtvwithportal5367 Місяць тому +2

    Still easier to code using type-hinted python than to write java 😂

  • @Learnbyflow
    @Learnbyflow 4 місяці тому +1

    We can use it like this in normal python
    myvalue:str ="hello"

  • @akshatgupta1658
    @akshatgupta1658 2 місяці тому

    What is your vscode theme and font?

    • @pixegami
      @pixegami  2 місяці тому

      I'm using Monokai Pro theme, and Roboto Mono font.

  • @nedac279
    @nedac279 5 місяців тому +1

    How is this different (or better) than dataclasses?

    • @pixegami
      @pixegami  5 місяців тому

      I cover this a bit towards the end of the video, but Pydantic gives you a bit more validation and serialisation capabilities. It's also generally more widely used in frameworks that deal with APIs or databases (e.g. FastAPI).
      But if the typing and structure is all I want, I'd go with dataclasses personally :)

  • @noname2588o
    @noname2588o Місяць тому +1

    If your software has to fail, it's better it fails as early as possible.
    - pixegami

  • @lupusexperience
    @lupusexperience 5 місяців тому +1

    nice video, but for non-negatives you only
    declare Field(ge=0)

  • @kaimildner2153
    @kaimildner2153 4 місяці тому +1

    I really don't understand why not just using a typed programming language? Don't get me wrong. I love python, but sometimes it is not the right tool. For small cli applications I love python. But it's not the right tool for everything. The last videos I watched about python was all about getting around it's downsides. Especially the Gil and multiprocessing. Now a way to make it typed? For me that's the wrong way. When I want a typed PL also with parallel processing I just use dotnet. When I want to have a beautiful UI with the best performance, I use c++ with Qt. When I want a small and fast to implement webservice I use python, as long as I don't need some multiprocessing.
    What I try to say is, that there is the right tool for every problem. You don't have to use the same tool for everything.

    • @pixegami
      @pixegami  3 місяці тому

      I think the difference in perspective for me here is that Python's typing system (nor multi-processing or GIL) are big enough factors to make it not the right tool for the job in a lot of use-cases.
      For example, it's more important that you're using a language that has the vast library selection that Python has (especially in DS/ML/AI), or has wide platform and SDK support, or maybe it's a tactical decision because your team already knows it (and it's easy to hire for).
      At that stage, the typing experience just kinda becomes a sharp edge that people who have already settled on Python tend to focus on. Not enough of a show stopper to migrate to Rust or GoLang, but also painful enough to look for an easy solution to.

  • @programmingwiththotho4641
    @programmingwiththotho4641 Місяць тому +1

    thank you for the explanation, and please next time leave a link for donation

    • @pixegami
      @pixegami  Місяць тому

      Thank you! Glad it was useful :)

  • @roberthuff3122
    @roberthuff3122 3 місяці тому +1

    🎯 Key Takeaways for quick navigation:
    00:00 🐍 *Python's Dynamic Typing Limitation*
    - Discusses the limitation of Python's dynamic typing and the problems it can cause,
    - Highlights the risk of accidental creation of invalid objects due to loosely defined variable types,
    - Emphasizes the difficulties in debugging failures caused by incorrect use of dynamic typing.
    01:53 🎁 *Introduction to Pydantic*
    - Introduces Pydantic as a powerful tool to model data, with validation capabilities for avoiding the problems caused by Python's dynamic typing,
    - Discusses Pydantic's use in popular Python modules and its benefits,
    - Explains how Pydantic improves IDE support for type-hints and allows for easy serialization.
    03:19 💻 *Creating and Using Pydantic Models*
    - Demonstrates how to create models in Pydantic and how Pydantic ensures that only valid data is used in models,
    - Shows the added advantage of type hinting provided by Pydantic models in an IDE,
    - Illustrates custom data validation in Pydantic by enforcing that all account IDs must be a positive number.
    06:36 🔄 *JSON Serialization with Pydantic*
    - Details how Pydantic supports JSON serialization, which proves beneficial in integrating Python code with external applications,
    - Provides an example of converting a Pydantic model to a JSON string using the JSON method.
    08:03 📊 *Comparing Pydantic to Dataclasses*
    - Compares Pydantic to Python's inbuilt dataclasses module,
    - Evaluates both modules based on type hints, data validation, and JSON serialization,
    - Suggests appropriate cases of usage for both modules depending on specific programming needs.
    Made with HARPA AI

  • @Andy_B.
    @Andy_B. Місяць тому +1

    👍

  • @agnichatian
    @agnichatian 2 місяці тому +1

    gj!

  • @Ironication
    @Ironication 6 місяців тому +1

    So this is like Zod for python.

  • @kikocometa
    @kikocometa 5 місяців тому +1

    Problem with static typing in python is not true.Theres an optional typing module for python for your information.

    • @pixegami
      @pixegami  5 місяців тому

      Yup, if you just want to get type hints, then I recommend just using the built-in Python features (I think I also mentioned them at the end of the video).

  • @fstemarie
    @fstemarie 6 місяців тому +4

    If you need data typing, why not use another language that uses it? I don't understand why you would use python and then use an external library to bend it to your needs. Why not use go?

    • @pixegami
      @pixegami  6 місяців тому +1

      Hmm, as nice as data-typing is, I don't think it's a first-class criteria for language section. The most important things will probably be stuff like: 1) what frameworks/libraries are available? 2) what SDKs do the services I want to use support? 3) How popular is the language (e.g. if you wanted to hire people to work on it)?
      That said, it's still a commonly cited pain point amongst Python developers, and a lot of popular libraries have approached this problem using libraries like Pydantic. This is especially useful if you need to save/store the data between databases or with other apps as well.

    • @minciNashu
      @minciNashu 5 місяців тому +1

      pydantic is about validation. Look at how FastAPI is using it.

  • @shahiburRM
    @shahiburRM 5 місяців тому

    Ide theme name?

    • @pixegami
      @pixegami  5 місяців тому

      Monokai Pro :)

  • @nicholastoo858
    @nicholastoo858 2 місяці тому

    Python debugging needs to up its game. So it cant tell you where the error is coming from and the reason for the error? That’s weak.

  • @jnsougata
    @jnsougata 4 місяці тому

    People are still writing codes in notepad?

  • @hanyanglee9018
    @hanyanglee9018 5 місяців тому

    emmm, it's time for some neural nets.

  • @ThomasVanhelden
    @ThomasVanhelden 6 місяців тому +3

    First of all, Python's dynamic typing is not a problem. Secondly, even if it was, it's not the problem Pydantic is trying to solve.
    I can't think of a problem where your Person example results in hard-to-find issues, or any issue at all, for that matter. If the age variable absolutely has to be an integer, your program will likely use it in a way that only integers can be used, and Python's duck typing will infer that you can only pass an integer to it.
    Pydantic is meant for data parsing. Not for validation (in the dictionary sense of the word).

    • @pixegami
      @pixegami  6 місяців тому

      Thanks for the thoughtful counter-point. I do admit the title/intro was a bit click-baity but I still think the data-validation and type-hinting aspects of Pydantic are extremely useful. Although truth be told, if typing is all I wanted I would use @dataclass and type hints instead (I think I mentioned that towards the end of the video too).

  • @aitools24
    @aitools24 6 місяців тому +1

    00:02 Pydantic module helps solve Python's lack of static typing.
    01:20 Pydantic is a data validation library in Python with powerful tools to model and validate data.
    02:49 Pydantic allows you to create models in Python with type hints
    04:12 Pydantic provides data validation and type hinting, making code easier to work with
    05:36 Pydantic provides built-in validation for different types of data.
    07:02 Pydantic provides built-in support for JSON serialization.
    08:26 Pydantic provides easy integration with JSON and external applications
    09:50 Pydantic is recommended for complex data models and JSON serialization.
    Crafted by Merlin AI.

  • @egonkirchof
    @egonkirchof 2 місяці тому

    Variables don't have types in Python. The value they keep has a type.

  • @SkyyySi
    @SkyyySi 5 місяців тому +1

    If you use Pydantic to model internal app state, you are doing it wrong.

    • @pixegami
      @pixegami  5 місяців тому

      Right. If you don't need the validation and serialisation capabilities, then dataclasses is a better choice.

  • @liuzhibo3918
    @liuzhibo3918 4 місяці тому +1

    what about marshmallow

    • @pixegami
      @pixegami  4 місяці тому

      I haven't looked into that yet :( I'm a little behind the curve...

  • @0LoneTech
    @0LoneTech 5 місяців тому +1

    Static typing and mandatory type declarations are not the same thing. Type inference can make the latter unnecessary.

  • @ReflectionOcean
    @ReflectionOcean 4 місяці тому +1

    - Understand the drawbacks of Python's dynamic typing at [0:04].
    - Recognize the potential for creating invalid objects in Python at [1:06].
    - Consider using Pydantic to solve type-related issues in Python at [1:49].
    - Install Pydantic to enhance data validation and typing at [2:53].
    - Create a Pydantic model by inheriting from the base model class at [2:59].
    - Utilize Pydantic's type hints and autocomplete for easier coding at [3:49].
    - Implement Pydantic's data validation to catch errors early at [5:03].
    - Add custom validation logic to your Pydantic model at [6:43].
    - Use Pydantic's JSON serialization for easy data interchange at [8:00].
    - Compare Pydantic to Python's built-in dataclasses for your needs at [8:47].

  • @RodyDavis
    @RodyDavis 6 місяців тому +1

    Mojo?

    • @pixegami
      @pixegami  6 місяців тому

      I'm actually just starting to learn about it!

  • @CristiNeagu
    @CristiNeagu 5 місяців тому +4

    I think that people saying Python's dynamic typing is a problem are doing Python wrong. They're using Python to write C. Python's dynamic typing is a benefit, not a problem. And with type hinting, it's the best of both worlds. You can keep track of your data types without being tied to anything.

    • @pixegami
      @pixegami  5 місяців тому

      Good perspective :)

  • @OctagonalSquare
    @OctagonalSquare 4 місяці тому +2

    I really don’t see the actual point. If you can’t use dynamic typing without getting mixed up, then use a language with strict typing. It’s like buying an F-150 but putting a Ram’s engine in it. Just buy the Ram to begin with.
    If you feel a feature of a programming language is a problem, then you need to use a different language or learn to leverage the feature better

    • @pixegami
      @pixegami  3 місяці тому

      Hmm, I think the analogy of the "typing" being the engine doesn't really hit the mark here. It's more of a comfort feature, so in terms of car analogy, it's more like air-conditioned seats or tinted windows or a sun-roof. Maybe you want the engine of the F-150 (i.e. Python's vast libraries, ubiquity in AI/data, and wide platform/SDK support), but just want UX experience of runtime type validation.

  • @davidmurphy563
    @davidmurphy563 Місяць тому +1

    If you're incapable of maintaining a codebase without static typing then you should reconsider your career.
    Muddling data types is a myth. It just doesn't happen. That's why none of the major libraries use type hinting. It's not a real issue.

    • @pixegami
      @pixegami  28 днів тому

      I think if the Python community (and creator) saw it that way, then PEP 484 would not have passed: "There has now been enough 3rd party usage for static type analysis that the community would benefit from a standard vocabulary and baseline tools within the standard library."
      This isn't about making it mandatory, it's just about offering it as an option for those who prefer it (and it seems like a lot of people do).

    • @davidmurphy563
      @davidmurphy563 28 днів тому

      @@pixegami I entirely agree with the statement in 484. I would not have type hinting removed from python.
      There are clear use cases; it can be helpful for beginners who often struggle to understand types, immensely useful tools like dataclasses employ them and they save time / boilerplate, it allows you to recompile into C for speed and it can help with IDE tools, albeit marginally. Rational reasons aside, the reality is that many people are coming from strongly typed languages (whose strong typing serves a performance purpose) and it can just give them a little familiarity / a crutch. I've no objections to any of that.
      I take issue only with the prevailing myth that type hinting saves python from some natural dynamic state of type spaghetti and _proper python code_ is type hinted. That's false.
      Pick the top libraries/frameworks in python written and maintained by some of the best coders of the language; requests, numpy, pandas, django, a long list. There's no type hinting and if there were a nett benefit they would take advantage of it.
      In that context, forcing type hinting is not good practice in my view.

  • @5508Vanderdekken
    @5508Vanderdekken 5 місяців тому +1

    Do people really get confused about what their variables are? I always prefix all my vars with tags that identify what they are: i_ for int, str_, df_, ld_ for list of dicts, etc

    • @pixegami
      @pixegami  5 місяців тому

      This is called "Hungarian Notation" and it used to be quite a widespread tactic. But I think since IDEs improved and Python support for type hinting has been added, the way to signal the type is usually just to use the built-in type hints now.
      But back to the topic- the "typing" feature is a nice introduction to one of the main problem Pydantic solves (since it's quite a common grievance in Python). However the library offers more value (over type-hints) in its validation and serialisation capabilities.