Skip to content

Fix run_worker typing to support async def callables under mypy stric…#6604

Open
Arghyann wants to merge 1 commit into
Textualize:mainfrom
Arghyann:fix-run-worker-typing
Open

Fix run_worker typing to support async def callables under mypy stric…#6604
Arghyann wants to merge 1 commit into
Textualize:mainfrom
Arghyann:fix-run-worker-typing

Conversation

@Arghyann

@Arghyann Arghyann commented Jun 27, 2026

Copy link
Copy Markdown

Description

This PR resolves issue #6386 by updating the WorkType type alias and overloading run_worker on DOMNode to prevent mypy type-inference failures when passing async def callables.

Root Cause:
In strict mode, mypy type-checks async def functions as returning Coroutine[Any, Any, ReturnType]. Since WorkType originally required Coroutine[None, None, ReturnType], it failed. Simply widening WorkType to Coroutine[Any, Any, ResultType] is insufficient because mypy's generic type inference conflicts when matching a callable return type against both Callable[[], Coroutine[Any, Any, ResultType]] and Callable[[], ResultType], causing it to infer ResultType as Never.

Fix:

  1. Updated WorkType to accept Coroutine[Any, Any, ResultType] instead of Coroutine[None, None, ResultType].
  2. Added three @overload signatures for run_worker in DOMNode to evaluate the callable/awaitable structures independently.
  3. Updated the run_worker implementation signature to accept WorkType[Any] and return Worker[Any] to allow the overloads to drive the external type-checking.

Link to issue or discussion

Fixes #6386

Testing

  • Verified that strict mypy checking passes on user reproduction files.
  • Ran all existing worker unit tests via pytest tests/workers/ and verified they pass successfully.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WorkType alias too restrictive for async def functions

1 participant