Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion taskiq/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ async def get_result(self, with_logs: bool = False) -> "TaskiqResult[_ReturnType
self.task_id,
with_logs=with_logs,
)
if self.return_type is not None:
# Errored tasks have no return value to validate,
# so parsing would only produce a misleading warning.
if self.return_type is not None and not res.is_err:
try:
res.return_value = parse_obj_as(
self.return_type,
Expand Down
41 changes: 40 additions & 1 deletion tests/test_task.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import uuid
from typing import TypeVar
from typing import Any, TypeVar

import pytest
from pydantic import BaseModel
Expand Down Expand Up @@ -68,3 +69,41 @@ class MyResult(BaseModel):
sent_task = AsyncTaskiqTask(test_id, res_back, MyResult)
parsed = await sent_task.wait_result()
assert isinstance(parsed.return_value, MyResult)


async def test_res_parsing_skipped_on_error(caplog: pytest.LogCaptureFixture) -> None:
res_back: AsyncResultBackend[Any] = SerializingBackend(serializers.JSONSerializer())
test_id = str(uuid.uuid4())
await res_back.set_result(
test_id,
TaskiqResult(
is_err=True,
return_value=None,
execution_time=0.0,
),
)
sent_task: AsyncTaskiqTask[Any] = AsyncTaskiqTask(test_id, res_back, int)
with caplog.at_level(logging.WARNING, logger="taskiq.task"):
parsed = await sent_task.wait_result()
assert parsed.is_err
assert parsed.return_value is None
assert "Cannot parse return type" not in caplog.text


async def test_res_parsing_failure(caplog: pytest.LogCaptureFixture) -> None:
res_back: AsyncResultBackend[Any] = SerializingBackend(serializers.JSONSerializer())
test_id = str(uuid.uuid4())
await res_back.set_result(
test_id,
TaskiqResult(
is_err=False,
return_value="not-an-int",
execution_time=0.0,
),
)
sent_task: AsyncTaskiqTask[Any] = AsyncTaskiqTask(test_id, res_back, int)
with caplog.at_level(logging.WARNING, logger="taskiq.task"):
parsed = await sent_task.wait_result()
assert not parsed.is_err
assert parsed.return_value == "not-an-int"
assert "Cannot parse return type" in caplog.text
Loading