Skip to content

MAINT: Make batch_task_async generic over the task result (unsound-assignment follow-up) #4550

Open

Description

@romanlutz

Context

PR #4544 (UA-2 of #4402) fixed the unsound-assignment finding in PromptNormalizer.send_prompt_batch_to_target_async with a local cast("list[Message]", ...), because the root cause is out of UA-2's scope: batch_task_async in pyrit/prompt_target/batch_helper.py takes task_func: Callable[..., Any] and returns list[Any], even though it always returns one task_func result per batched item.

What to change

Make the helper generic over the task result:

_BatchResult = TypeVar("_BatchResult")

async def batch_task_async(
    *,
    prompt_target: PromptTarget | None = None,
    batch_size: int,
    items_to_batch: Sequence[Sequence[Any]],
    task_func: Callable[..., Awaitable[_BatchResult]],
    task_arguments: list[str],
    **task_kwargs: Any,
) -> list[_BatchResult]: ...

The arguments side stays Callable[..., ...] — the helper builds kwargs dynamically from task_arguments strings, so a ParamSpec can't model it. The win is the return type.

Call sites that benefit

Acceptance

  • batch_task_async returns list[_BatchResult] inferred from task_func.
  • The cast in send_prompt_batch_to_target_async is removed and the "Each batch result is..." comment with it.
  • make ty stays clean; no new entries in the temporary unsound-assignment override in pyproject.toml.
  • Unit tests for prompt_target, prompt_normalizer and score pass unchanged (no runtime behavior change — this is annotation-only).

If you'd like to work on this, please claim it by commenting first; the first commenter may open a PR.

Activity

  1. szKubica commented on Nov 21, 2026

    @szKubica

    I would like to take this. Plan: add a _BatchResult TypeVar to batch_task_async so the return type is inferred from task_func, remove the cast in send_prompt_batch_to_target_async, and tighten the scorer / scorer_evaluator call sites, keeping make ty clean and the existing unit tests unchanged.
    Roman Lutz (@romanlutz) please share your thought

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

help wantedExtra attention is needed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions