Skip to content

Commit 872b1c2

Browse files
committed
Added initial pydanticV2 support.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 0b8c6d6 commit 872b1c2

File tree

8 files changed

+312
-216
lines changed

8 files changed

+312
-216
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class UserInfo(BaseModel):
154154

155155
@router.post("/users")
156156
async def new_data(user: UserInfo = Depends(Json())):
157-
return web.json_response({"user": user.dict()})
157+
return web.json_response({"user": user.model_dump()})
158158

159159
```
160160

@@ -168,7 +168,7 @@ If you want to make this data optional, just mark it as optional.
168168
async def new_data(user: Optional[UserInfo] = Depends(Json())):
169169
if user is None:
170170
return web.json_response({"user": None})
171-
return web.json_response({"user": user.dict()})
171+
return web.json_response({"user": user.model_dump()})
172172

173173
```
174174

@@ -275,19 +275,18 @@ To make the magic happen, please add `arbitrary_types_allowed` to the config of
275275

276276

277277
```python
278-
from pydantic import BaseModel
278+
import pydantic
279279
from aiohttp_deps import Router, Depends, Form
280280
from aiohttp import web
281281

282282
router = Router()
283283

284284

285-
class MyForm(BaseModel):
285+
class MyForm(pydantic.BaseModel):
286286
id: int
287287
file: web.FileField
288288

289-
class Config:
290-
arbitrary_types_allowed = True
289+
model_config = pydantic.ConfigDict(arbitrary_types_allowed=True)
291290

292291

293292
@router.post("/")

aiohttp_deps/swagger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from logging import getLogger
44
from typing import Any, Awaitable, Callable, Dict, Optional, Union
55

6+
import pydantic
67
from aiohttp import web
7-
from pydantic import schema_of
88
from pydantic.utils import deep_update
99
from taskiq_dependencies import DependencyGraph
1010

@@ -103,8 +103,9 @@ def _add_route_def( # noqa: C901
103103
dependency.signature
104104
and dependency.signature.annotation != inspect.Parameter.empty
105105
):
106-
input_schema = schema_of(
106+
input_schema = pydantic.TypeAdapter(
107107
dependency.signature.annotation,
108+
).json_schema(
108109
ref_template=REF_TEMPLATE,
109110
)
110111
openapi_schema["components"]["schemas"].update(

aiohttp_deps/utils.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ def __call__( # noqa: C901, WPS210
6868
return value
6969

7070
try:
71-
return pydantic.parse_obj_as(definition, value)
71+
return pydantic.TypeAdapter(definition).validate_python(value)
7272
except pydantic.ValidationError as err:
73-
errors = err.errors()
73+
errors = err.errors(include_url=False)
7474
for error in errors:
7575
error["loc"] = (
7676
"header",
7777
header_name,
7878
) + error["loc"]
79+
error.pop("input", None) # type: ignore
7980
raise web.HTTPBadRequest(
8081
headers={"Content-Type": "application/json"},
8182
text=json.dumps(errors),
@@ -120,11 +121,12 @@ async def __call__( # noqa: C901
120121
return body
121122

122123
try:
123-
return pydantic.parse_obj_as(definition, body)
124+
return pydantic.TypeAdapter(definition).validate_python(body)
124125
except pydantic.ValidationError as err:
125-
errors = err.errors()
126+
errors = err.errors(include_url=False)
126127
for error in errors:
127128
error["loc"] = ("body",) + error["loc"]
129+
error.pop("input", None) # type: ignore
128130
raise web.HTTPBadRequest(
129131
headers={"Content-Type": "application/json"},
130132
text=json.dumps(errors),
@@ -192,14 +194,15 @@ def __call__( # noqa: C901, WPS210
192194
return value
193195

194196
try:
195-
return pydantic.parse_obj_as(definition, value)
197+
return pydantic.TypeAdapter(definition).validate_python(value)
196198
except pydantic.ValidationError as err:
197-
errors = err.errors()
199+
errors = err.errors(include_url=False)
198200
for error in errors:
199201
error["loc"] = (
200202
"query",
201203
param_name,
202204
) + error["loc"]
205+
error.pop("input", None) # type: ignore
203206
raise web.HTTPBadRequest(
204207
headers={"Content-Type": "application/json"},
205208
text=json.dumps(errors),
@@ -242,10 +245,11 @@ async def __call__(
242245
return form_data
243246

244247
try:
245-
return pydantic.parse_obj_as(definition, form_data)
248+
return pydantic.TypeAdapter(definition).validate_python(form_data)
246249
except pydantic.ValidationError as err:
247-
errors = err.errors()
250+
errors = err.errors(include_url=False)
248251
for error in errors:
252+
error.pop("input", None) # type: ignore
249253
error["loc"] = ("form",) + error["loc"]
250254
raise web.HTTPBadRequest(
251255
headers={"Content-Type": "application/json"},
@@ -299,10 +303,11 @@ def __call__(
299303
return matched_data
300304

301305
try:
302-
return pydantic.parse_obj_as(definition, matched_data)
306+
return pydantic.TypeAdapter(definition).validate_python(matched_data)
303307
except pydantic.ValidationError as err:
304-
errors = err.errors()
308+
errors = err.errors(include_url=False)
305309
for error in errors:
310+
error.pop("input", None) # type: ignore
306311
error["loc"] = ("path",) + error["loc"]
307312
raise web.HTTPBadRequest(
308313
headers={"Content-Type": "application/json"},

0 commit comments

Comments
 (0)