Pydantic Tutorial • Solving Python's Biggest Problem

preview_player
Показать описание
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

📚 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
Рекомендации по теме
Комментарии
Автор

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

pythonantole
Автор

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.

theintjengineer
Автор

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

UTJK.
Автор

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

_aarishalam
Автор

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

ChelseaSaint
Автор

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.

python-for-everyone
Автор

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

darrenlefcoe
Автор

Awesome Pydantic summary - concise and quintessential.
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 =

cyberslot
Автор

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.

jimg
Автор

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!

jason
Автор

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

vzcqcib
Автор

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

FrederikSchumacher
Автор

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

sheshanaturupana
Автор

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

NikolajLepka
Автор

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

jabuci
Автор

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!

GilbertoMadeira
Автор

Nice Video @pixegami. Would have liked to hear about Conversion a bit, example Date with the variety of formats that one encounters.

SathishKumar-qkhu
Автор

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

nas
Автор

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

JohnMitchellCalif
Автор

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

chakib