Declarative, AOP-based Pydantic validation for pico-ioc managed components. Validates method arguments against BaseModel type hints before execution.
Project description
๐ฆ pico-pydantic
Pico-Pydantic
Pico-Pydantic integrates Pico-IoC with Pydantic, enabling declarative, aspect-oriented validation of method arguments within your service layer.
It uses Pico-IoC's MethodInterceptor system to perform validation based on Pydantic BaseModel type hints before your method's business logic runs. This is the ideal tool for ensuring arguments passed between IoC-managed services are structurally correct.
๐ Requires Python 3.11+ ๐งฉ Works with Pydantic 2.0+ ๐ Supports async and sync methods ๐งช Enables unit testing of validation separate from business logic
๐ฏ Why pico-pydantic
While web frameworks handle validation at the HTTP boundary, business services often need to guarantee input integrity internally, especially when components are called from CLI tools, workers, or other services.
Pico-Pydantic provides:
- Declarative
@validateboundaries for service methods. - Aspect-Oriented Programming (AOP) for argument validation.
- Clear error handling with
ValidationFailedError. - Centralized validation logic, decoupled from the core service code.
| Concern | Pico-IoC Default | pico-pydantic |
|---|---|---|
| Argument checking | Manual if/raise |
Declarative @validate |
| Schema definition | None | Pydantic BaseModel type hints |
| Handling errors | Raw ValidationError |
Wrapped ValidationFailedError |
๐งฑ Core Features
- Method validation via
@validatedecorator. ValidationInterceptorfor AOP execution.- Seamless compatibility with
BaseModeltype annotations. - Correct handling of positional, keyword, and default arguments.
- Zero coupling to web frameworks.
๐ฆ Installation
pip install pico-pydantic
๐ Quick Example
1. Define the Data Model and Service:
from pydantic import BaseModel, Field
from pico_ioc import component
from pico_pydantic import validate
class ItemData(BaseModel):
name: str = Field(min_length=3)
price: float = Field(gt=0)
@component
class InventoryService:
@validate
async def add_item(self, data: ItemData) -> dict:
# Validation happens BEFORE this line
print(f"Adding item: {data.name}")
return data.model_dump()
2. Full Example: Initialization, Success, and Failure Handling
import asyncio
from pico_boot import init
from pico_pydantic import ValidationFailedError
# 'components' is used here as the module containing InventoryService.
container = init(modules=["components"])
async def main():
service = container.get(InventoryService)
# --- Success: Validation Passes ---
print("--- Testing Success ---")
result = await service.add_item({"name": "Hammer", "price": 10.50})
print(f"Result: {result}")
# --- Failure: ValidationFailedError is thrown by the Interceptor ---
print("\n--- Testing Failure ---")
try:
# Fails: 'price' is negative, violating Field(gt=0)
await service.add_item({"name": "A", "price": -5})
except ValidationFailedError as e:
print(f"Validation failed for method '{e.method_name}'.")
print(e.pydantic_error) # Shows the detailed Pydantic error
await container.cleanup_all_async()
container.shutdown()
if __name__ == "__main__":
asyncio.run(main())
โ๏ธ How It Works
- The
@validatedecorator attachesValidationInterceptorto the method's AOP chain via@intercepted_by. - When the method is called:
- The interceptor captures the call arguments.
- It inspects the method signature for arguments with the
BaseModeltype hint. - It validates each argument using
TypeAdapter.validate_python(value). - If validation fails, it wraps the error in
ValidationFailedErrorand stops execution. - If successful,
call_nextis executed, and the original method runs.
No manual checks inside the service method. Logic stays clean.
๐ก Architecture Overview
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your App โ
โ (Service Layer) โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ
@validate called
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ Pico-IoC โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
ValidationInterceptor (AOP)
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ pico-pydantic โ
โ Inspect & validate_python() โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
Pydantic 2.0+
AI Coding Skills
Install Claude Code or OpenAI Codex skills for AI-assisted development with pico-pydantic:
curl -sL https://raw.githubusercontent.com/dperezcabrera/pico-skills/main/install.sh | bash -s -- pydantic
| Command | Description |
|---|---|
/add-validation |
Add Pydantic validation to component methods |
/add-component |
Add components, factories, interceptors, settings |
/add-tests |
Generate tests for pico-framework components |
All skills: curl -sL https://raw.githubusercontent.com/dperezcabrera/pico-skills/main/install.sh | bash
See pico-skills for details.
๐ License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pico_pydantic-0.2.2.tar.gz.
File metadata
- Download URL: pico_pydantic-0.2.2.tar.gz
- Upload date:
- Size: 39.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca2e5d7d226868b450ebb9d3ac78ca0fd9aa61f63cc6608bdd272e94e060fcd6
|
|
| MD5 |
847d0b267a0f9d3b761cfdfa628e74d5
|
|
| BLAKE2b-256 |
b04b27c1b7a2d0eb9ca0259fa388daaa7a68f709dcc74004732ad1c3e346b3d6
|
Provenance
The following attestation bundles were made for pico_pydantic-0.2.2.tar.gz:
Publisher:
publish-to-pypi.yml on dperezcabrera/pico-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pico_pydantic-0.2.2.tar.gz -
Subject digest:
ca2e5d7d226868b450ebb9d3ac78ca0fd9aa61f63cc6608bdd272e94e060fcd6 - Sigstore transparency entry: 1189304772
- Sigstore integration time:
-
Permalink:
dperezcabrera/pico-pydantic@5f85839593fa318fb47837b1407b0e200b540e03 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/dperezcabrera
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@5f85839593fa318fb47837b1407b0e200b540e03 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pico_pydantic-0.2.2-py3-none-any.whl.
File metadata
- Download URL: pico_pydantic-0.2.2-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f51df3e68988ee90ea9fd1f5b4900f01290f1655aa84e2dd13b68a375e988f5
|
|
| MD5 |
7a463d38f55ed72244f3be3eeceb152a
|
|
| BLAKE2b-256 |
9802f544b1c938579ebe909bfb545817bf595f01a0cdc821461164d21de96e49
|
Provenance
The following attestation bundles were made for pico_pydantic-0.2.2-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on dperezcabrera/pico-pydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pico_pydantic-0.2.2-py3-none-any.whl -
Subject digest:
2f51df3e68988ee90ea9fd1f5b4900f01290f1655aa84e2dd13b68a375e988f5 - Sigstore transparency entry: 1189304775
- Sigstore integration time:
-
Permalink:
dperezcabrera/pico-pydantic@5f85839593fa318fb47837b1407b0e200b540e03 -
Branch / Tag:
refs/tags/v0.2.2 - Owner: https://github.com/dperezcabrera
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@5f85839593fa318fb47837b1407b0e200b540e03 -
Trigger Event:
release
-
Statement type: