Last active
March 14, 2025 18:07
-
-
Save birkin/8cf1db0ece2ea3edc01c053f113b5342 to your computer and use it in GitHub Desktop.
pydantic example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# requires-python = "==3.12.*" | |
# dependencies = ["pydantic==2.10.*"] | |
# /// | |
""" | |
Pydantic example | |
- Pydantic is a data validation package for Python. Using rust, it's fast. | |
- <https://docs.pydantic.dev/latest/> | |
Usage: | |
uv run ./pydantic_example.py | |
See output at bottom. | |
""" | |
import pprint | |
from datetime import datetime | |
from pydantic import BaseModel, Field, ValidationError | |
class User(BaseModel): | |
username: str | |
age: int = Field(..., gt=0, lt=120) # age must be between 1 and 119 | |
email: str | |
signup_ts: datetime | None = None | |
## valid example -------------------------------- | |
user1 = User(username='john_doe', age=30, email='[email protected]') | |
print('') | |
print('all good...') | |
print(user1.username) | |
print('---\n') | |
## invalid example ------------------------------`` | |
try: | |
user2 = User(username='john2_doe2', age=200) | |
except ValidationError as e: | |
print('human-readable errors...') | |
print(e) | |
print('---\n') | |
print('errors as list of dicts...') | |
errors: list = e.errors() | |
print(pprint.pformat(errors, indent=2, sort_dicts=False)) | |
print('---\n') | |
print('errors as json...') | |
errors_json: list = e.json(indent=2) | |
print(errors_json) | |
print('---\n') | |
""" | |
Outuput... | |
% uv run ./pydantic_example.py | |
all good... | |
john_doe | |
--- | |
human-readable errors... | |
2 validation errors for User | |
age | |
Input should be less than 120 [type=less_than, input_value=200, input_type=int] | |
For further information visit https://errors.pydantic.dev/2.10/v/less_than | |
Field required [type=missing, input_value={'username': 'john2_doe2', 'age': 200}, input_type=dict] | |
For further information visit https://errors.pydantic.dev/2.10/v/missing | |
--- | |
errors as list of dicts... | |
[ { 'type': 'less_than', | |
'loc': ('age',), | |
'msg': 'Input should be less than 120', | |
'input': 200, | |
'ctx': {'lt': 120}, | |
'url': 'https://errors.pydantic.dev/2.10/v/less_than'}, | |
{ 'type': 'missing', | |
'loc': ('email',), | |
'msg': 'Field required', | |
'input': {'username': 'john2_doe2', 'age': 200}, | |
'url': 'https://errors.pydantic.dev/2.10/v/missing'}] | |
--- | |
errors as json... | |
[ | |
{ | |
"type": "less_than", | |
"loc": [ | |
"age" | |
], | |
"msg": "Input should be less than 120", | |
"input": 200, | |
"ctx": { | |
"lt": 120 | |
}, | |
"url": "https://errors.pydantic.dev/2.10/v/less_than" | |
}, | |
{ | |
"type": "missing", | |
"loc": [ | |
"email" | |
], | |
"msg": "Field required", | |
"input": { | |
"username": "john2_doe2", | |
"age": 200 | |
}, | |
"url": "https://errors.pydantic.dev/2.10/v/missing" | |
} | |
] | |
--- | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment