Suspicious Boolean Coroutine

ID

python.suspicious_boolean_coroutine

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Python

Tags

reliability, suspicious-construct

Description

Reports a bare reference to an async def function used as the boolean condition of an if or while. Function objects are always truthy in Python, so the condition never evaluates to false — this is almost always a forgotten parenthesis-and-await.

async def is_ready():
    return True

async def caller():
    if is_ready:                  # FLAW — function reference, always truthy
        do_x()
    if await is_ready():          # OK
        do_y()

The companion case if is_ready(): (calling an async function and using the resulting coroutine as condition) is caught by python.missing_await_coroutine.

Rationale

A coroutine function reference is a perfectly callable object that always evaluates to truthy in a boolean context — the writer almost always meant to call it and await the result.

Remediation

Add the () and the await so the condition tests the resolved value, not the function object.