~nora/fastapi-sandbox

96b1a0c8a7bfa833fe70abc9c359cf3e49693605 — nora 9 months ago 172ccd2
Migrate to pydantic 2
4 files changed, 53 insertions(+), 51 deletions(-)

M app/config.py
M app/items/schemas.py
M app/users/schemas.py
M requirements.txt
M app/config.py => app/config.py +4 -4
@@ 1,15 1,15 @@
from functools import lru_cache

from pydantic import BaseSettings, EmailStr
from pydantic import EmailStr
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    app_name: str = "fastapi-sandbox"
    admin_email: EmailStr | None
    admin_email: EmailStr = "admin@example.com"
    database_url: str = "sqlite:///db.sqlite3"

    class Config:
        env_file = ".env"
    model_config = SettingsConfigDict(env_file=".env")


@lru_cache()

M app/items/schemas.py => app/items/schemas.py +17 -18
@@ 1,4 1,4 @@
from pydantic import BaseModel, Field, HttpUrl, validator
from pydantic import BaseModel, ConfigDict, Field, HttpUrl, field_validator


class Image(BaseModel):


@@ 8,31 8,30 @@ class Image(BaseModel):

class Item(BaseModel):
    name: str
    description: str | None = Field(title="Description", max_length=300, example="A description")
    description: str | None = Field(title="Description", max_length=300)
    price: float = Field(lt=100, description="The price must be lesser than 100")
    tags: set[str] = set()
    image: Image | None = None

    @validator("price")
    @field_validator("price")
    def price_must_be_positive(cls, value):
        if value <= 0:
            raise ValueError(f"The price must be greater than zero, received: {value}")
        return value

    class Config:
        schema_extra = {
            "example": {
                "name": "Foo",
                "description": "Foo description",
                "price": 42.0,
                "tags": [
                    "foo",
                    "bar",
                    "baz"
                ],
                "image": {
                    "url": "http://example.com/foo.jpg",
                    "name": "The Foo"
                }
    model_config = ConfigDict(json_schema_extra={
        "example": {
            "name": "Foo",
            "description": "Foo description",
            "price": 42.0,
            "tags": [
                "foo",
                "bar",
                "baz"
            ],
            "image": {
                "url": "http://example.com/foo.jpg",
                "name": "The Foo"
            }
        }
    })

M app/users/schemas.py => app/users/schemas.py +3 -5
@@ 1,4 1,4 @@
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel, ConfigDict, EmailStr


class ItemBase(BaseModel):


@@ 14,8 14,7 @@ class Item(ItemBase):
    id: int
    owner_id: int

    class Config:
        orm_mode = True
    model_config = ConfigDict(from_attributes=True)


class UserBase(BaseModel):


@@ 31,5 30,4 @@ class User(UserBase):
    is_active: bool
    items: list[Item] = []

    class Config:
        orm_mode = True
    model_config = ConfigDict(from_attributes=True)

M requirements.txt => requirements.txt +29 -24
@@ 1,32 1,37 @@
anyio==3.6.2
arrow==1.2.3
attrs==22.2.0
certifi==2022.12.7
click==8.1.3
dnspython==2.3.0
email-validator==1.3.1
exceptiongroup==1.1.1
fastapi==0.95.0
annotated-types==0.5.0
anyio==3.7.1
certifi==2023.7.22
click==8.1.7
dnspython==2.4.2
email-validator==2.0.0.post2
fastapi==0.103.1
greenlet==2.0.2
h11==0.14.0
httpcore==0.16.3
httpx==0.23.3
httpcore==0.18.0
httptools==0.6.0
httpx==0.25.0
idna==3.4
iniconfig==2.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
packaging==23.0
pluggy==1.0.0
pydantic==1.10.7
pytest==7.2.2
python-dateutil==2.8.2
MarkupSafe==2.1.3
orjson==3.9.7
packaging==23.1
pluggy==1.3.0
pydantic==2.3.0
pydantic-extra-types==2.1.0
pydantic-settings==2.0.3
pydantic_core==2.6.3
pytest==7.4.2
python-dotenv==1.0.0
python-multipart==0.0.6
rfc3986==1.5.0
six==1.16.0
PyYAML==6.0.1
sniffio==1.3.0
SQLAlchemy==2.0.8
starlette==0.26.1
tomli==2.0.1
typing_extensions==4.5.0
uvicorn==0.21.1
SQLAlchemy==2.0.20
starlette==0.27.0
typing_extensions==4.7.1
ujson==5.8.0
uvicorn==0.23.2
uvloop==0.17.0
watchfiles==0.20.0
websockets==11.0.3