Unit 2: Pydantic
Last updated
Last updated
is a Python library used for data parsing and validation using Python type annotations. Its main goal is to provide an easy way to define how data should be in pure, human-readable Python code, and to ensure that the data you work with matches these specifications.
Installation is as simple as:
Pydantic has a few dependencies:
pydantic-core
: Core validation logic for pydantic written in rust.
typing-extensions
: Backport of the standard library typing
module.
annotated-types
: Reusable constraint types to use with typing.Annotated
.
Save the file as Example.py
, then execute the program using the command below:
And the output will look like that
The Python program above demonstrates the use of the Pydantic library, which is designed for data validation and settings management using Python type annotations. Let's break down some important code:
The User
class is defined as a subclass of Pydantic's BaseModel
. This enables automatic validation of data based on the type annotations provided in the class.
The User
class has four fields where signup_ts
is a datetime field for the user's sign-up timestamp. It can also be None
, as indicated by datetime | None
. The | None
is a way to specify that the field is optional and can be None
.
external_data
is a dictionary containing data that will be used to create a User
object. The keys in this dictionary correspond to the fields in the User
class.
user = User(**external_data)
: This line creates an instance of the User
class. The **external_data
syntax unpacks the dictionary into keyword arguments. Pydantic automatically handles the conversion of data types (e.g., converting the string "123" to an integer for the id
field) and validates the data.
If any field is missing in external_data
(except those with default values), or if there's a type mismatch, Pydantic will raise an error.
Pydantic ensures that the data you receive, whether from an external source like a JSON file or an API call, fits your expected structure and type. It validates the data against a predefined schema and can automatically convert types when possible.
Pydantic heavily relies on Python's type annotations. This allows you to define data models using standard Python classes and type hints, making your code clear and easy to understand.
When data doesn't conform to the defined schema, Pydantic raises informative errors. These errors can tell you exactly where and how the data is incorrect, which is incredibly useful for debugging and data cleaning.
Pydantic can also work with ORM (Object-Relational Mapping) models, allowing you to parse and validate data from ORM objects. This makes it compatible with databases using ORMs like SQLAlchemy.
Pydantic is often used for managing application settings, where it can validate environment variables and other configuration data. This ensures that your application starts with the correct and valid configuration.
It can generate JSON Schemas from your Python models. This is particularly useful for API documentation and validation.
Pydantic is integral to FastAPI, a modern web framework for building APIs. FastAPI uses Pydantic models to validate incoming request data and to serialize outgoing response data.
Pydantic can be extended and customized to handle almost any type of data validation. This includes creating custom validators and defining complex data types.
Despite its power, Pydantic remains syntactically simple, leveraging Python's native type hints.