Skip to content

Add truthy-coroutine check #16069

Open
Open

Description

@ikonst

Feature

async def func() -> bool:
    return True

async def func2() -> None:
    if func():
        print("hi")

In this code, a likely fix is to change func() to await func(). Meanwhile, it'll always be true since a Coroutine is always true. The truthy-bool check could flag it but it's not enabled by default. We can add a new truthy-coroutine check (like truthy-function) for the specific case of using a Coroutine in boolean context.

The error message can suggest that an await might've been mistakenly omitted.

Activity

  1. sterliakov commented on May 4, 2025

    @sterliakov
    Collaborator

    This is supported with --enable-error-code truthy-bool! https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&enable-error-code=truthy-bool%2Cunused-awaitable&gist=90f4e135d45763cdec86fe1917507f93

    main.py:5: error: "__main__.func" returns "Coroutine[Any, Any, bool]" which does not implement __bool__ or __len__ so it could always be true in boolean context  [truthy-bool]
    
  2. ikonst commented on May 5, 2025

    @ikonst
    ContributorAuthor

    @sterliakov You misread the report. I'm aware that truthy-bool would report it.

  3. sterliakov commented on May 5, 2025

    @sterliakov
    Collaborator

    Indeed, my apologies!

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions