AwaitAsyncCallRule

Enforces calls to coroutines are preceeded by the await keyword. Awaiting on a coroutine will execute it while simply calling a coroutine returns a coroutine object (https://docs.python.org/3/library/asyncio-task.html#coroutines).

Message

Async function call will only be executed with await statement. Did you forget to add await? If you intend to not await, please add comment to disable this warning: # lint-fixme: AwaitAsyncCallRule

Has Autofix: Yes

VALID Code Examples

# 1:

async def async_func():
    await async_foo()

# 2:

def foo(): pass
foo()

# 3:

async def foo(): pass
async def bar():
    await foo()

# 4:

async def foo(): pass
async def bar():
    x = await foo()

# 5:

async def foo() -> bool: pass
async def bar():
    while not await foo(): pass

# 6:

import asyncio
async def foo(): pass
asyncio.run(foo())

INVALID Code Examples

# 1:

async def foo(): pass
async def bar():
    foo()

Autofix:

---
+++
@@ -1,4 +1,4 @@

 async def foo(): pass
 async def bar():
-    foo()
+    await foo()

# 2:

class Foo:
    async def _attr(self): pass
obj = Foo()
obj._attr

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     async def _attr(self): pass
 obj = Foo()
-obj._attr
+await obj._attr

# 3:

class Foo:
    async def _method(self): pass
obj = Foo()
obj._method()

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     async def _method(self): pass
 obj = Foo()
-obj._method()
+await obj._method()

# 4:

class Foo:
    async def _method(self): pass
obj = Foo()
result = obj._method()

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     async def _method(self): pass
 obj = Foo()
-result = obj._method()
+result = await obj._method()

# 5:

class Foo:
    async def bar(): pass
class NodeUser:
    async def get():
        do_stuff()
        return Foo()
user = NodeUser.get().bar()

Autofix:

---
+++
@@ -5,4 +5,4 @@
     async def get():
         do_stuff()
         return Foo()
-user = NodeUser.get().bar()
+user = await NodeUser.get().bar()

# 6:

class Foo:
    async def _attr(self): pass
obj = Foo()
attribute = obj._attr

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     async def _attr(self): pass
 obj = Foo()
-attribute = obj._attr
+attribute = await obj._attr

# 7:

async def foo() -> bool: pass
x = True
if x and foo(): pass

Autofix:

---
+++
@@ -1,4 +1,4 @@

 async def foo() -> bool: pass
 x = True
-if x and foo(): pass
+if x and await foo(): pass

# 8:

async def foo() -> bool: pass
x = True
are_both_true = x and foo()

Autofix:

---
+++
@@ -1,4 +1,4 @@

 async def foo() -> bool: pass
 x = True
-are_both_true = x and foo()
+are_both_true = x and await foo()

# 9:

async def foo() -> bool: pass
if foo():
    do_stuff()

Autofix:

---
+++
@@ -1,4 +1,4 @@

 async def foo() -> bool: pass
-if foo():
+if await foo():
     do_stuff()

# 10:

async def foo() -> bool: pass
if not foo():
    do_stuff()

Autofix:

---
+++
@@ -1,4 +1,4 @@

 async def foo() -> bool: pass
-if not foo():
+if not await foo():
     do_stuff()

# 11:

class Foo:
    async def _attr(self): pass
    def bar(self):
        if self._attr: pass

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     async def _attr(self): pass
     def bar(self):
-        if self._attr: pass
+        if await self._attr: pass

# 12:

class Foo:
    async def _attr(self): pass
    def bar(self):
        if not self._attr: pass

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     async def _attr(self): pass
     def bar(self):
-        if not self._attr: pass
+        if not await self._attr: pass

# 13:

class Foo:
    async def _attr(self): pass
def bar() -> Foo:
    return Foo()
attribute = bar()._attr

Autofix:

---
+++
@@ -3,4 +3,4 @@
     async def _attr(self): pass
 def bar() -> Foo:
     return Foo()
-attribute = bar()._attr
+attribute = await bar()._attr

# 14:

class Foo:
    def _attr(self): pass
async def bar():
    await do_stuff()
    return Foo()
attribute = bar()._attr

Autofix:

---
+++
@@ -4,4 +4,4 @@
 async def bar():
     await do_stuff()
     return Foo()
-attribute = bar()._attr
+attribute = await bar()._attr

# 15:

async def bar() -> bool: pass
while bar(): pass

Autofix:

---
+++
@@ -1,3 +1,3 @@

 async def bar() -> bool: pass
-while bar(): pass
+while await bar(): pass

# 16:

async def bar() -> bool: pass
while not bar(): pass

Autofix:

---
+++
@@ -1,3 +1,3 @@

 async def bar() -> bool: pass
-while not bar(): pass
+while not await bar(): pass

# 17:

class Foo:
    @classmethod
    async def _method(cls): pass
Foo._method()

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     @classmethod
     async def _method(cls): pass
-Foo._method()
+await Foo._method()

# 18:

class Foo:
    @staticmethod
    async def _method(self): pass
Foo._method()

Autofix:

---
+++
@@ -2,4 +2,4 @@
 class Foo:
     @staticmethod
     async def _method(self): pass
-Foo._method()
+await Foo._method()