Overview
Pydantic 1.10, released on July 16, 2022, introduces strict mode and model_rebuild for dynamic model reconstruction.
Main Features
Strict mode
Strict mode disables automatic type coercion, rejecting values that don't exactly match the declared type.
python
from pydantic import BaseModel, validator
class User(BaseModel):
name: str
age: int
class Config:
strict = True
# OK
u = User(name='Alice', age=30)
print(u)
# In strict mode, age='30' would raise an error
# because str -> int coercion is disabled
model_rebuild
The model_rebuild method reconstructs a model after modifying its references, useful for models with circular dependencies.
python
from __future__ import annotations
from pydantic import BaseModel
from typing import Optional, List
class Node(BaseModel):
value: int
children: List[Node] = []
Node.model_rebuild() # resolve references
tree = Node(value=1, children=[
Node(value=2),
Node(value=3, children=[Node(value=4)]),
])
print(tree.json())
