Welcome to Pydantic-Basic, a curated collection of examples demonstrating the capabilities of Pydantic for data validation and modeling in Python. This repository is designed to help developers understand and utilize Pydantic's features effectively, from foundational concepts to integration with FastAPI.
Pydantic-Basic/
├── 01\_foundation/
├── 02\_fields/
├── 03\_model\_behaviour/
├── 04\_nested\_model/
├── 05\_serialization/
├── fastapi/
├── main.py
├── README.md
├── LICENSE
├── pyproject.toml
└── uv.lock
- 01_foundation/: Introduction to basic Pydantic models and type annotations.
- 02_fields/: Exploration of field types, default values, and field constraints.
- 03_model_behaviour/: Understanding model behaviors, including validators and computed fields.
- 04_nested_model/: Working with nested and recursive models.
- 05_serialization/: Demonstrating serialization and custom JSON encoding.
- fastapi/: Integrating Pydantic models with FastAPI for building APIs.
Ensure you have Python 3.9 or higher installed. You can check your Python version using:
python --version-
Clone the repository:
git clone https://github.com/Sourabh-Kumar04/Pydantic-Basic.git cd Pydantic-Basic -
Install
uv:If you haven't installed
uvyet, you can do so using pip:pip install uv
-
Install project dependencies:
uv sync
This command will install all the dependencies specified in
pyproject.tomland lock them inuv.lock.
Each directory contains Python scripts that can be run independently to observe Pydantic's features in action.
For example, to run the foundational examples:
uv run 01_foundation/example.pyTo start the FastAPI application:
uv run fastapi/fastapi_examples.py- Basic model creation using
BaseModel. - Type enforcement and automatic data parsing.
- Field definitions with default values.
- Field constraints and aliases.
- Custom validators using
@validator. - Computed fields with
@property.
- Nested models to represent complex data structures.
- Recursive models for hierarchical data like comments.
- Model serialization to dictionaries and JSON.
- Custom JSON encoders for complex types like
datetime.
- Building API endpoints with FastAPI.
- Request and response models using Pydantic.
This project is licensed under the Apache-2.0 License.
Sourabh Kumar
Feel free to reach out for collaborations or queries related to this project.
This `README.md` reflects the use of `uv` for dependency management and running the FastAPI application. If you need assistance with setting up `pyproject.toml` or have further questions, feel free to ask!