Skip to content

Make iscoroutinefunction TypeIs - #15928

Open
InvincibleRMC wants to merge 6 commits into
python:mainfrom
InvincibleRMC:iscoroutinefunction-TypeIs
Open

InvincibleRMC wants to merge 6 commits into
python:mainfrom
InvincibleRMC:iscoroutinefunction-TypeIs

Conversation

@InvincibleRMC

Copy link
Copy Markdown

Attempt to address #15529

InvincibleRMC and others added 4 commits June 20, 2026 08:33
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
@github-actions

This comment has been minimized.

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
@github-actions

This comment has been minimized.

Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

aiohttp (https://github.com/aio-libs/aiohttp)
- aiohttp/worker.py:76:26: error: Too few arguments  [call-arg]
+ aiohttp/worker.py:73:14: error: Left operand of "or" is always false  [redundant-expr]
- aiohttp/worker.py:76:26: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-call-arg for more info
+ aiohttp/worker.py:73:14: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-redundant-expr for more info
- aiohttp/worker.py:76:26: note: "wsgi" is considered instance variable, to make it class variable use ClassVar[...]
+ aiohttp/worker.py:76:13: error: Statement is unreachable  [unreachable]
+ aiohttp/worker.py:88:9: error: Statement is unreachable  [unreachable]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/core.py:2373: error: "Callable[[Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]], Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]]" has no attribute "predicate"  [attr-defined]
- discord/ext/commands/core.py:2448: error: "Callable[[Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]], Command[Any, Any, Any] | Callable[..., Coroutine[Any, Any, Any]]]" has no attribute "predicate"  [attr-defined]

Comment thread stdlib/inspect.pyi
def iscoroutinefunction(obj: Callable[..., Coroutine[Any, Any, Any]]) -> bool: ...
@overload
def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeGuard[Callable[_P, CoroutineType[Any, Any, _T]]]: ...
def iscoroutinefunction(obj: Callable[_P, Awaitable[_T]]) -> TypeIs[Callable[_P, CoroutineType[Any, Any, _T]]]: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect because it does not return true for all callables that return a CoroutineType.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a new type needs to be added to represent async functions, because with the current typing system there is no way to type

def foo(a) -> Awaitable[int]:
    return asyncio.Future()

as being distinct from

async def foo(a) -> int:
    return 5

and no way to distinguish them at runtime other than iscoroutinefunction, which can't be accurately typed as the annotations indicate a narrowing on the return type, while the logic of iscoroutinefunction is using flags on the function's code object.

Thus, you can't know definitively if a function is asynchronous in the current system.

This branch has not been deployed

No deployments
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.

3 participants