Skip to content

Spurious type error with async function + generic callable argument + optional return type #19716

@anishathalye

Description

@anishathalye

Here's a minimized repro:

from collections.abc import Callable

async def f[T](g: Callable[[T], bool]) -> T | None:
    return None

async def f2[T](g: Callable[[T], bool]) -> T | None:
    return await f(g)
$ mypy bug.py
bug.py:7: error: Argument 1 to "f" has incompatible type "Callable[[T], bool]"; expected "Callable[[T | None], bool]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

$ mypy --version
mypy 1.17.1 (compiled: yes)
$ python --version
Python 3.13.3
$ uname -v
Darwin Kernel Version 24.6.0: Mon Jul 14 11:28:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6030

An alternative type checker works fine with the above code:

$ pyright bug.py
0 errors, 0 warnings, 0 informations
$ pyright --version
pyright 1.1.404

Close variations seem to work fine:

No async

from collections.abc import Callable

def f[T](g: Callable[[T], bool]) -> T | None:
    return None

def f2[T](g: Callable[[T], bool]) -> T | None:
    return f(g)
$ mypy bug.py
Success: no issues found in 1 source file

No generic in callable argument

from collections.abc import Callable

async def f[T](g: Callable[[int], bool]) -> T | None:
    return None

async def f2[T](g: Callable[[int], bool]) -> T | None:
    return await f(g)
$ mypy bug.py
Success: no issues found in 1 source file

No optional return type

from collections.abc import Callable

async def f[T](g: Callable[[T], bool]) -> None:
    return None

async def f2[T](g: Callable[[T], bool]) -> None:
    return await f(g)
$ mypy bug.py
Success: no issues found in 1 source file

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions