Pydantic Tutorial • Solving Python's Biggest Problem

243,366
139
Published 2023-09-18
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

All Comments (21)
  • @NikolajLepka
    it's hilarious how many decades it took for python users to understand the benefit of strict typing
  • @ChelseaSaint
    You have a gift to explain complex things in a very simple to understand way.... great video Keep up the awesome work 💪
  • @pythonantole9892
    The explanation on the why and what problems Pydantic solves is one of the best that i have seen. I just had to subscribe!
  • @user-mf1xr5ki9j
    Nice video,  -not too big, not to short -presents the most popular points without sticking to details -compares to alternatives Thank you!
  • What a great overview and comparison. Thank you for the video. By the way, the fact that variables in Python do not need to be annotated is not what makes it dynamically typed (0:11). It is what makes Python implicitly typed. As you say later (1:34) dynamic type checking more has to do with when types are checked.
  • @jabuci
    Best intro to pydantic I've seen so far. Thanks!
  • @DaniEIdiomas
    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
  • @UTJK.
    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
  • 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
  • @002_aarishalam8
    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
  • @darrenlefcoe
    A really nice explanation of the difference and use cases between the two modules, pydanic vs dataclasses. Well done.
  • @theintjengineer
    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.
  • @chakib2378
    Excellent video. Thank you for getting straight to the point with clarity.
  • @Nardiso
    Man, awesome content like I never saw before! Really, really good! Thank you!
  • @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.
  • @gatorpika
    Wow, never understood that stuff until I watched this and it's pretty simple the way you explain it. Thanks...subbed!
  • @jason6569
    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!
  • @user-vz9cq8ci9b
    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
  • @roberthuff3122
    🎯 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 HA
  • @kentuckeytom
    very clear and concise, well organized, thanks.