Skip to content

Add truthy-coroutine check #16069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ikonst opened this issue Sep 7, 2023 · 3 comments
Open

Add truthy-coroutine check #16069

ikonst opened this issue Sep 7, 2023 · 3 comments
Labels
feature topic-async async, await, asyncio

Comments

@ikonst
Copy link
Contributor

ikonst commented Sep 7, 2023

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.

@ikonst ikonst added the feature label Sep 7, 2023
@AlexWaygood AlexWaygood added the topic-async async, await, asyncio label Sep 9, 2023
erictraut pushed a commit to microsoft/pyright that referenced this issue Sep 9, 2023
…e, which always evaluates to True. This is likely indicative of a missing `await` keyword. This addresses a feature request logged in the mypy issue tracker: python/mypy#16069.
@sterliakov
Copy link
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]

@ikonst
Copy link
Contributor Author

ikonst commented May 5, 2025

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

@sterliakov
Copy link
Collaborator

Indeed, my apologies!

@sterliakov sterliakov reopened this May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature topic-async async, await, asyncio
Projects
None yet
Development

No branches or pull requests

3 participants