⚙️Unit 2: Pydantic
Pydantic 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
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 librarytyping
module.annotated-types
: Reusable constraint types to use withtyping.Annotated
.
Sample Program
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'sBaseModel
. This enables automatic validation of data based on the type annotations provided in the class.The
User
class has four fields wheresignup_ts
is a datetime field for the user's sign-up timestamp. It can also beNone
, as indicated bydatetime | None
. The| None
is a way to specify that the field is optional and can beNone
.external_data
is a dictionary containing data that will be used to create aUser
object. The keys in this dictionary correspond to the fields in theUser
class.user = User(**external_data)
: This line creates an instance of theUser
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 theid
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.
Key Features
Data Validation and Parsing
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.
Type Annotations
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.
Error Handling
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.
ORM Mode
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.
Settings Management
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.
JSON Schema Support
It can generate JSON Schemas from your Python models. This is particularly useful for API documentation and validation.
FastAPI Integration
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.
Extensibility
Pydantic can be extended and customized to handle almost any type of data validation. This includes creating custom validators and defining complex data types.
Simple Syntax
Despite its power, Pydantic remains syntactically simple, leveraging Python's native type hints.
Last updated