This rule raises an issue when a function is declared async but does not use any asynchronous features.

Why is this an issue?

Declaring a function with async def signals that it’s a coroutine, typically because it needs to use await for non-blocking operations (like I/O), or employs other asynchronous features like async for or async with.

If none of these asynchronous mechanisms are utilized within the function’s body, the async declaration is superfluous.

Using async unnecessarily can:

How to fix it

Either remove the async keyword, or start using the appropriate asynchronous features.

Code examples

Noncompliant code example

async def my_function():  # Noncompliant
    print("Hello from my function")

Compliant solution

def my_function():  # Compliant
    print("Hello from my function")

Noncompliant code example

def another_function(): ...

async def my_function():  # Noncompliant
    return another_function()

Compliant solution

async def another_function(): ...

async def my_function():  # Compliant
    return await another_function()

Resources

Articles & blog posts