Pydantic Tutorial • Solving Python's Biggest Problem

Поделиться
HTML-код
  • Опубликовано: 6 июн 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

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

  • @pythonantole9892
    @pythonantole9892 8 месяцев назад +40

    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 месяцев назад +42

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

    • @pixegami
      @pixegami  8 месяцев назад +1

      Thank you! Glad you enjoyed it :)

  • @UTJK.
    @UTJK. 6 месяцев назад +23

    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

  • @darrenlefcoe
    @darrenlefcoe 10 дней назад +2

    A really nice explanation of the difference and use cases between the two modules, pydanic vs dataclasses. Well done.

    • @pixegami
      @pixegami  10 дней назад

      Thank you! Glad it was clear :)

  • @user-mf1xr5ki9j
    @user-mf1xr5ki9j 8 месяцев назад +30

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

  • @NikolajLepka
    @NikolajLepka 5 месяцев назад +17

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

    • @pixegami
      @pixegami  5 месяцев назад +5

      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 месяца назад +8

      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.

  • @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!

  • @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 :)

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

    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.

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

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

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

      Glad you enjoyed it!

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

  • @chakib2378
    @chakib2378 4 месяца назад +2

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

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

  • @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!

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

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

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

      Glad you enjoyed it!

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

      Yay, glad it was helpful!

  • @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  8 месяцев назад +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).

  • @luftstolle
    @luftstolle 5 месяцев назад +1

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

    • @pixegami
      @pixegami  5 месяцев назад

      Thank you! Glad it was helpful :)

  • @JohnMitchellCalif
    @JohnMitchellCalif 4 месяца назад +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 :)

  • @kentuckeytom
    @kentuckeytom 4 месяца назад +1

    very clear and concise, well organized, thanks.

  • @ZeeshaanAli
    @ZeeshaanAli День назад +1

    Absolutely precise and concise.

  • @alexhasha
    @alexhasha 6 месяцев назад +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  6 месяцев назад +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.

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

  • @christophehanon8396
    @christophehanon8396 3 месяца назад +1

    Very clear explanation - thanks for sharing !

  • @Andromeda26_
    @Andromeda26_ 2 месяца назад +1

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

    • @pixegami
      @pixegami  Месяц назад

      Glad you enjoyed it!

  • @shedrachugochukwu6245
    @shedrachugochukwu6245 Месяц назад +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  25 дней назад

      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.

  • @luis96xd
    @luis96xd 4 месяца назад +1

    Amazing video, everything was well explained, thanks!

    • @pixegami
      @pixegami  3 месяца назад +1

      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!

  • @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 8 месяцев назад +3

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

    • @pixegami
      @pixegami  8 месяцев назад +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 8 месяцев назад +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 6 месяцев назад +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.

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

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

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

      Monokai Pro :)

  • @RuudJeursen
    @RuudJeursen 5 месяцев назад +1

    Thanks, nice & clear explanation!

    • @pixegami
      @pixegami  5 месяцев назад

      Glad you enjoyed it!

  • @thebuggser2752
    @thebuggser2752 4 месяца назад +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!

  • @cjbarroso
    @cjbarroso 7 месяцев назад +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!

  • @ananyamahapatra6597
    @ananyamahapatra6597 Месяц назад +1

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

    • @pixegami
      @pixegami  25 дней назад

      Thank you, glad you liked it!

  • @nas8318
    @nas8318 Месяц назад +1

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

    • @pixegami
      @pixegami  26 дней назад

      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.

  • @404nohandlefound
    @404nohandlefound 6 месяцев назад +11

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

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

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

  • @footflaps
    @footflaps 3 месяца назад

    Very helpful!

  • @yashwanthsai3401
    @yashwanthsai3401 5 месяцев назад +1

    Thanks. U have explained very well

    • @pixegami
      @pixegami  5 месяцев назад

      Thank you!

  • @guohaigao2397
    @guohaigao2397 5 месяцев назад +1

    Thank you , like this video so much.

    • @pixegami
      @pixegami  5 месяцев назад

      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 :)

  • @Reb-012
    @Reb-012 9 дней назад +1

    Thanks man

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

  • @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  8 месяцев назад

      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 :)

  • @fengjeremy7878
    @fengjeremy7878 5 месяцев назад +1

    Good tutorial!

  • @fatmasliti2161
    @fatmasliti2161 5 месяцев назад +1

    great explanation!

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

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

  • @youssef.elyaakoubi
    @youssef.elyaakoubi Месяц назад +1

    Thanks Bro!

    • @pixegami
      @pixegami  26 дней назад

      You're welcome!

  • @FrederikSchumacher
    @FrederikSchumacher 6 месяцев назад +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  6 месяцев назад +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 6 месяцев назад +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.

  • @dogzabob
    @dogzabob 4 месяца назад +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/

  • @amirjamal4803
    @amirjamal4803 3 месяца назад +1

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

  • @mohibahmed5098
    @mohibahmed5098 Месяц назад +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  25 дней назад

      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.

  • @a0um
    @a0um 6 месяцев назад +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 :)

  • @morespinach9832
    @morespinach9832 Месяц назад +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  25 дней назад +1

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

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

    Nice presentation.

    • @pixegami
      @pixegami  5 месяцев назад +1

      Glad you liked it

  • @jairajsahgal7101
    @jairajsahgal7101 5 месяцев назад +1

    Thank you

    • @pixegami
      @pixegami  5 месяцев назад

      You're welcome!

  • @roberthuff3122
    @roberthuff3122 4 месяца назад +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

  • @adam_fakes
    @adam_fakes 5 месяцев назад +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  4 месяца назад

      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.

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

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

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

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

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

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

  • @AdityaDodda
    @AdityaDodda 5 месяцев назад +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  5 месяцев назад +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 4 месяца назад +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).

  • @PeterRichardsandYoureNot
    @PeterRichardsandYoureNot 5 месяцев назад +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  4 месяца назад

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

  • @tirsky
    @tirsky 5 месяцев назад +2

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

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

      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.

  • @feezysted
    @feezysted 8 месяцев назад +1

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

    • @pixegami
      @pixegami  8 месяцев назад

      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.

  • @PaulaBean
    @PaulaBean 3 месяца назад +2

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

    • @pixegami
      @pixegami  3 месяца назад

      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 3 месяца назад

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

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

    We can mix wit Sqlalchemy models?

  • @Learnbyflow
    @Learnbyflow 5 месяцев назад +1

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

  • @noname2588o
    @noname2588o Месяц назад +1

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

  • @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!

  • @akshatgupta1658
    @akshatgupta1658 3 месяца назад

    What is your vscode theme and font?

    • @pixegami
      @pixegami  3 месяца назад

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

  • @aitools24
    @aitools24 7 месяцев назад +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.

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

  • @programmingwiththotho4641
    @programmingwiththotho4641 2 месяца назад +1

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

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

      Thank you! Glad it was useful :)

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

  • @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  8 месяцев назад +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 6 месяцев назад +1

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

  • @nedac279
    @nedac279 6 месяцев назад +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 :)

  • @lupusexperience
    @lupusexperience 5 месяцев назад +1

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

    • @pixegami
      @pixegami  5 месяцев назад

      Thank you!

  • @agnichatian
    @agnichatian 2 месяца назад +1

    gj!

  • @Andy_B.
    @Andy_B. 2 месяца назад +1

    👍

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

  • @egonkirchof
    @egonkirchof 3 месяца назад

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

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

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

    So this is like Zod for python.

  • @jnsougata
    @jnsougata 5 месяцев назад

    People are still writing codes in notepad?

  • @shahiburRM
    @shahiburRM 5 месяцев назад

    Ide theme name?

    • @pixegami
      @pixegami  5 месяцев назад

      Monokai Pro :)

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

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

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

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

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

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

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

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

    Mojo?

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

      I'm actually just starting to learn about it!

  • @liuzhibo3918
    @liuzhibo3918 5 месяцев назад +1

    what about marshmallow

    • @pixegami
      @pixegami  5 месяцев назад

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

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

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

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

      Good perspective :)

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

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

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