11import asyncio
2+ from logging import getLogger
23from time import time
34from typing import TYPE_CHECKING , Any , Generic , Optional
45
6+ from pydantic import TypeAdapter
57from typing_extensions import TypeVar
68
79from taskiq .exceptions import (
1517 from taskiq .depends .progress_tracker import TaskProgress
1618 from taskiq .result import TaskiqResult
1719
20+ logger = getLogger ("taskiq.task" )
21+
1822_ReturnType = TypeVar ("_ReturnType" )
1923
2024
@@ -25,9 +29,11 @@ def __init__(
2529 self ,
2630 task_id : str ,
2731 result_backend : "AsyncResultBackend[_ReturnType]" ,
32+ return_type : Optional [TypeAdapter [_ReturnType ]] = None ,
2833 ) -> None :
2934 self .task_id = task_id
3035 self .result_backend = result_backend
36+ self .return_type = return_type
3137
3238 async def is_ready (self ) -> bool :
3339 """
@@ -53,10 +59,18 @@ async def get_result(self, with_logs: bool = False) -> "TaskiqResult[_ReturnType
5359 :return: task's return value.
5460 """
5561 try :
56- return await self .result_backend .get_result (
62+ res = await self .result_backend .get_result (
5763 self .task_id ,
5864 with_logs = with_logs ,
5965 )
66+ if self .return_type is not None :
67+ try :
68+ res .return_value = self .return_type .validate_python (
69+ res .return_value ,
70+ )
71+ except ValueError :
72+ logger .warning ("Cannot parse return type into %s" , self .return_type )
73+ return res
6074 except Exception as exc :
6175 raise ResultGetError from exc
6276
0 commit comments