Skip to content

Conversation

@zzstoatzz
Copy link
Collaborator

closes #19326

Summary

Fixes a confusing pyright error message when calling async tasks with incorrect argument types.

Before: Calling an async task with wrong arg types showed:

"State[CoroutineType[Any, Any, None]]" is not awaitable

After: Shows normal argument type error (or no error if types are correct)

Root Cause

When pyright couldn't match task arguments to the specific overload (e.g., passing None instead of str), it fell back to the *args: Unknown overloads. The return_state=True overload was listed first, so pyright picked it and inferred State[Coroutine[...]] instead of Coroutine[...].

Changes

  1. Added explicit Task[P, Coroutine[Any, Any, R]] overloads to Task.__call__ (similar to what submit() and map() already have)
  2. Reordered overloads so return_state=False comes before return_state=True - ensures pyright picks the correct default behavior when it can't match argument types

Testing

  • Reproduced the original issue
  • Verified the fix resolves it
  • All existing tests pass (tests/test_tasks.py::TestTaskCall)
Reproduction
from prefect import task

@task
async def f(s: str) -> None:
    print(s)

async def test_wrong_arg_type():
    await f(None)  # Before: "State[Coroutine...] is not awaitable"
                    # After: Normal type error or no confusing error

🤖 Generated with Claude Code

…ent types

When calling an async task with incorrect argument types, pyright would
show a confusing error: "State[CoroutineType[...]] is not awaitable"
instead of a normal argument type error.

This happened because:
1. Task.__call__ lacked explicit overloads for async tasks (Task[P, Coroutine[Any, Any, R]])
2. The return_state=True overload came before return_state=False
3. When pyright couldn't match argument types, it fell back to the *args overloads
   and picked the first match (return_state=True), inferring State[Coroutine[...]]

Fix:
- Add explicit Task[P, Coroutine[Any, Any, R]] overloads (matching submit/map)
- Reorder overloads so return_state=False comes first (the default behavior)

Now pyright shows normal argument type errors instead of the confusing
"State is not awaitable" message.

Closes #19326

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-actions github-actions bot added the bug Something isn't working label Nov 3, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 3, 2025

CodSpeed Performance Report

Merging #19327 will not alter performance

Comparing fix-task-call-overload-ordering (50d1603) with main (7d86c93)

Summary

✅ 2 untouched

@zzstoatzz zzstoatzz added development Tech debt, refactors, CI, tests, and other related work. typing and removed bug Something isn't working labels Nov 3, 2025
@zzstoatzz zzstoatzz marked this pull request as ready for review November 3, 2025 15:10
@github-actions github-actions bot added the bug Something isn't working label Nov 3, 2025
@zzstoatzz zzstoatzz merged commit 422b995 into main Nov 3, 2025
61 checks passed
@zzstoatzz zzstoatzz deleted the fix-task-call-overload-ordering branch November 3, 2025 18:34
@zzstoatzz
Copy link
Collaborator Author

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

Labels

bug Something isn't working development Tech debt, refactors, CI, tests, and other related work. typing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Task call with incorrect argument type yields incorrect error in pyright: "State[CoroutineType[...]]" is not awaitable

3 participants