Skip to content

Commit

Permalink
Add support for pydantic 2
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw committed Jul 12, 2023
1 parent 3ef27a6 commit 4c8a714
Show file tree
Hide file tree
Showing 9 changed files with 1,453 additions and 1,415 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
max-parallel: 3
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v1
Expand Down
13 changes: 7 additions & 6 deletions fastapi_utils/api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from functools import partial

from pydantic import BaseConfig, BaseModel
from pydantic import BaseModel, ConfigDict

from fastapi_utils.camelcase import snake2camel

Expand All @@ -14,14 +14,15 @@ class APIModel(BaseModel):
Any models that inherit from this class will:
* accept fields using snake_case or camelCase keys
* use camelCase keys in the generated OpenAPI spec
* have orm_mode on by default
* have from_attributes on by default
* Because of this, FastAPI will automatically attempt to parse returned orm instances into the model
"""

class Config(BaseConfig):
orm_mode = True
allow_population_by_field_name = True
alias_generator = partial(snake2camel, start_lower=True)
model_config = ConfigDict(
from_attributes=True,
populate_by_name=True,
alias_generator=partial(snake2camel, start_lower=True),
)


class APIMessage(APIModel):
Expand Down
6 changes: 2 additions & 4 deletions fastapi_utils/api_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import lru_cache
from typing import Any

from pydantic import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict


class APISettings(BaseSettings):
Expand Down Expand Up @@ -53,9 +53,7 @@ def fastapi_kwargs(self) -> dict[str, Any]:
fastapi_kwargs.update({"docs_url": None, "openapi_url": None, "redoc_url": None})
return fastapi_kwargs

class Config:
env_prefix = "api_"
validate_assignment = True
model_config = SettingsConfigDict(env_prefix="api_", validate_assignment=True)


@lru_cache()
Expand Down
5 changes: 2 additions & 3 deletions fastapi_utils/cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import inspect
from collections.abc import Callable
from typing import Any, TypeVar, get_type_hints
from typing import Any, ClassVar, TypeVar, get_origin, get_type_hints

from fastapi import APIRouter, Depends
from pydantic.typing import is_classvar
from starlette.routing import Route, WebSocketRoute

T = TypeVar("T")
Expand Down Expand Up @@ -69,7 +68,7 @@ def _init_cbv(cls: type[Any]) -> None:
]
dependency_names: list[str] = []
for name, hint in get_type_hints(cls).items():
if is_classvar(hint):
if get_origin(hint) is ClassVar:
continue
parameter_kwargs = {"default": getattr(cls, name, Ellipsis)}
dependency_names.append(name)
Expand Down
1,716 changes: 849 additions & 867 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ classifiers = [
]

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.8"

fastapi = "*"
pydantic = "^1.10,<2.0"
pydantic = "^2.0.0"
pydantic-settings = "^2.0"
sqlalchemy = "^1.4,<2.0"

[tool.poetry.dev-dependencies]
Expand Down
1,117 changes: 587 additions & 530 deletions requirements.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/test_api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Data:
class Model(APIModel):
x: int

assert Model.from_orm(Data(x=1)).x == 1
assert Model.model_validate(Data(x=1)).x == 1


def test_aliases() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_inferring_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

openapi_spec = {
"info": {"title": "FastAPI", "version": "0.1.0"},
"openapi": "3.0.2",
"openapi": "3.1.0",
"paths": {
"/1": {
"get": {
Expand Down

0 comments on commit 4c8a714

Please sign in to comment.