Skip to content

Commit ba55f43

Browse files
committed
Fixed pydantic v1 support.
1 parent e99799a commit ba55f43

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

taskiq/abc/broker.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
)
2424
from uuid import uuid4
2525

26-
from pydantic import TypeAdapter
2726
from typing_extensions import ParamSpec, Self, TypeAlias
2827

2928
from taskiq.abc.middleware import TaskiqMiddleware
@@ -331,15 +330,15 @@ def inner(
331330
sign = get_type_hints(func)
332331
return_type = None
333332
if "return" in sign:
334-
return_type = TypeAdapter(sign["return"])
333+
return_type = sign["return"]
335334

336335
decorated_task = wrapper(
337336
self.decorator_class(
338337
broker=self,
339338
original_func=func,
340339
labels=inner_labels,
341340
task_name=inner_task_name,
342-
return_type=return_type,
341+
return_type=return_type, # type: ignore
343342
),
344343
)
345344

taskiq/compat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# flake8: noqa
22
from functools import lru_cache
3-
from typing import Any, Dict, Optional, Type, TypeVar, Union
3+
from typing import Any, Dict, Hashable, Optional, Type, TypeVar, Union
44

55
import pydantic
66
from importlib_metadata import version
@@ -12,13 +12,13 @@
1212
IS_PYDANTIC2 = PYDANTIC_VER >= Version("2.0")
1313

1414
if IS_PYDANTIC2:
15-
T = TypeVar("T")
15+
T = TypeVar("T", bound=Hashable)
1616

1717
@lru_cache()
18-
def create_type_adapter(annot: T) -> pydantic.TypeAdapter[T]:
18+
def create_type_adapter(annot: Type[T]) -> pydantic.TypeAdapter[T]:
1919
return pydantic.TypeAdapter(annot)
2020

21-
def parse_obj_as(annot: T, obj: Any) -> T:
21+
def parse_obj_as(annot: Type[T], obj: Any) -> T:
2222
return create_type_adapter(annot).validate_python(obj)
2323

2424
def model_validate(

taskiq/decor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
Dict,
99
Generic,
1010
Optional,
11+
Type,
1112
TypeVar,
1213
Union,
1314
overload,
1415
)
1516

16-
from pydantic import TypeAdapter
1717
from typing_extensions import ParamSpec
1818

1919
from taskiq.kicker import AsyncKicker
@@ -52,7 +52,7 @@ def __init__(
5252
task_name: str,
5353
original_func: Callable[_FuncParams, _ReturnType],
5454
labels: Dict[str, Any],
55-
return_type: Optional[TypeAdapter[_ReturnType]] = None,
55+
return_type: Optional[Type[_ReturnType]] = None,
5656
) -> None:
5757
self.broker = broker
5858
self.task_name = task_name

taskiq/kicker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
Dict,
1010
Generic,
1111
Optional,
12+
Type,
1213
TypeVar,
1314
Union,
1415
overload,
1516
)
1617

17-
from pydantic import BaseModel, TypeAdapter
18+
from pydantic import BaseModel
1819
from typing_extensions import ParamSpec
1920

2021
from taskiq.abc.middleware import TaskiqMiddleware
@@ -46,7 +47,7 @@ def __init__(
4647
task_name: str,
4748
broker: "AsyncBroker",
4849
labels: Dict[str, Any],
49-
return_type: Optional[TypeAdapter[_ReturnType]] = None,
50+
return_type: Optional[Type[_ReturnType]] = None,
5051
) -> None:
5152
self.task_name = task_name
5253
self.broker = broker

taskiq/task.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import asyncio
22
from logging import getLogger
33
from time import time
4-
from typing import TYPE_CHECKING, Any, Generic, Optional
4+
from typing import TYPE_CHECKING, Any, Generic, Optional, Type
55

6-
from pydantic import TypeAdapter
76
from typing_extensions import TypeVar
87

8+
from taskiq.compat import parse_obj_as
99
from taskiq.exceptions import (
1010
ResultGetError,
1111
ResultIsReadyError,
@@ -29,7 +29,7 @@ def __init__(
2929
self,
3030
task_id: str,
3131
result_backend: "AsyncResultBackend[_ReturnType]",
32-
return_type: Optional[TypeAdapter[_ReturnType]] = None,
32+
return_type: Optional[Type[_ReturnType]] = None,
3333
) -> None:
3434
self.task_id = task_id
3535
self.result_backend = result_backend
@@ -65,7 +65,8 @@ async def get_result(self, with_logs: bool = False) -> "TaskiqResult[_ReturnType
6565
)
6666
if self.return_type is not None:
6767
try:
68-
res.return_value = self.return_type.validate_python(
68+
res.return_value = parse_obj_as(
69+
self.return_type,
6970
res.return_value,
7071
)
7172
except ValueError:

0 commit comments

Comments
 (0)