Skip to content

iscoroutinefunction should use TypeIs #15529

Open

Description

@Redoubts

One of the things I find myself writing a lot is

if iscoroutinefunction(myfunction):
    do_something_with_async(myfunction)
else:
    do_something_with_just_sync(myfunction)

however the else block doesn't narrow the type to non-awaitable. Shouldn't it? Some of the other is... functions in inspect seem to use TypeIs as well.

Activity

  1. srittau commented on Mar 20, 2026

    @srittau
    Collaborator

    I assume changing the return type to TypeIs was deliberately left out of #11823. Maybe @JelleZijlstra can provide some insight?

  2. dchevell commented on Apr 20, 2026

    @dchevell

    My understanding is that TypeIs has no defined behaviour for structural types (i.e. Callable), so making this change in typeshed wouldn’t necessarily guarantee you anything. (Though personally I wish the TypeIs spec would formally support Callable, but hey)

    Edit: see https://discuss.python.org/t/specs-clarification-typeis-works-with-all-types-structural-or-nominal/95120

  3. JelleZijlstra commented on Apr 20, 2026

    @JelleZijlstra
    Member

    That is false.

  4. InvincibleRMC commented on Jun 7, 2026

    @InvincibleRMC

    I have a similar situation as @Redoubts is the solution as simple as making a pr switching from TypeGuard to TypeIs for inspect.iscoroutinefunction? I'm not too familiar with their differences.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: improvementImprove/refactor existing annotations, other stubs issues

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions