python / python/mypy

Add truthy-coroutine check

Open
#16,069 3 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-async
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the existing truthy-bool and truthy-function checks, then trace how a Coroutine is handled in boolean context. Add a truthy-coroutine diagnostic for the shown async example, with a message suggesting that await may be missing, and verify the check's behavior against equivalent coroutine and non-coroutine cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.