unused-courotine should trigger when using a coroutine call as a condition without awaiting it
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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