python / python/mypy

unused-courotine should trigger when using a coroutine call as a condition without awaiting it

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

Nobody has claimed this yet.

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

Description

Feature
Extend unused-coroutine to also encompass directly using the unawaited result with if/while, or perhaps also in other instances where it's used as a condition such as boolean operations etc. Or since the return value technically is used, introduce another error code.

Pitch
Currently no warning is raised for the following code:

async def foo() -> bool:
    return False
if foo():
    print("foo is true!")

because the return value of the coroutine technically is used. But it's very clearly an error, will give a RuntimeWarning, and bool(foo()) always evaluates to True.
I also have a hard time coming up with reasonable scenarios where the above functionality is desired, other than perhaps some weird logic for figuring out if a function is async or not - but in that case the object should be a union of those types and the type checker should be able to handle it.

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 reproducing the Python example with an async function used directly as an if condition, then inspect how mypy currently reports unused-coroutine cases. Define whether the diagnostic covers if/while only or boolean conditions as well, and whether it extends unused-coroutine or uses a new error code; done means the intended misuse is diagnosed consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.