Pydantic Tips & Tricks
Pydantic is a Python library for data validation and settings management using Python-type hinting. In this post, we will look at some of the tips and tricks of Pydantic
Recently, I got introduced to Pydantic. I was heavily using FastAPI and absolutely love how it enforces you to use Pydantic for Data serialization and validation. Life before Pydantic was mostly Flask and Django. While both of these were great frameworks, we need something like FastAPI even to see where it is flawed. Enough about FastAPI. This post is not about that.
Let's go back to Pydantic.
What is Pydantic?
It is a Python library. You do pip install pydantic
and come into some powerful stuff. It is primarily used to validate data coming into your application and serialize data going out of your application.
A simple pydantic model looks like this
For the rest of this post, we will use the above snippet as our example and make changes to it and explore Pydantic features.
Data Serialization
1. Data serialization from Dict
Data serialization from JSON
Pretty cool, right? Now let's see what happens when you poke around with the wrong data format
Data Validation
Marking fields as optional
Check if the year is greater than 1800
Validate ISBN
To add custom validation to a field, you can use the validator
decorator
Validate all fields
There will be cases where you want to validate one field based on another field. You can use the root_validator
decorator to validate all fields
So how does it work?
After adding all the above validators, run the following code. You will encounter a series of validation errors. Now imagine this class hooked to an HTTP request body. You no longer have to handle independent validations.
Configure Models
Configuring fields
Configuring model
We can add Config
class to configure the model to tweak the behavior of the model. For example, we can set extra
to forbid
to prevent extra fields from being added to the model. We can also set allow_population_by_field_name
to True
to allow the population of the model by field name. We can also set fields
to a dictionary of field names and its configuration. For example, we can set alias
for a field.
Setting common alias
There are a lot more configs which you can explore in the docs
Inheritance woohoo!
Inheritance with pydantic becomes even more powerful since we are also inheriting the config from the parent class.
DeSerializing Data
Serialize to Dict
Serialize to JSON
Pickle
Comparison with other libraries
Dataclasses
Let's start by writing a sample dataclass
Now let's see how we can use this dataclass to validate data
Gives an error TypeError: __init__() got an unexpected keyword argument 'publisher'
. Let's fix that and retry.
That passed. But notice how we didn't get any error for the price
field. That's because dataclasses don't validate data.
Let's see how we can add validation to dataclasses
That's a lot of hoops to jump through. The code doesn't look clean. We cannot blame Dataclass completely for this. Dataclasses were not designed to validate data. They were designed to create classes with less boilerplate. To keep it generic we had to compensate on lack of powerful features like pydantic.
Want validation? but with dataclass?
Pydantic got you covered in that aspect from pydantic.dataclasses import dataclass
, and you can use it just like you would use dataclass.
Attrs
Attrs is another library which is similar to dataclasses. Attrs is more closer to pydantic than dataclasses. Let's see how we can use attrs to validate data. They have pretty good argument on why you should use attrs over dataclasses. You can read it here
Last updated