.. _AwaitAsyncCallRule:

==================
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:







.. code-block:: python


    async def async_func():
        await async_foo()





# 2:







.. code-block:: python


    def foo(): pass
    foo()





# 3:







.. code-block:: python


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





.. container:: toggle


    # 4:







    .. code-block:: python


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






    # 5:







    .. code-block:: python


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






    # 6:







    .. code-block:: python


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






---------------------
INVALID Code Examples
---------------------

# 1:







.. code-block:: python


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





Autofix:

.. code-block:: python

    --- 
    +++ 
    @@ -1,4 +1,4 @@
 
     async def foo(): pass
     async def bar():
    -    foo()
    +    await foo()

# 2:







.. code-block:: python


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





Autofix:

.. code-block:: python

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

# 3:







.. code-block:: python


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





Autofix:

.. code-block:: python

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

.. container:: toggle


    # 4:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 5:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 6:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 7:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 8:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -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:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1,4 +1,4 @@
 
         async def foo() -> bool: pass
        -if foo():
        +if await foo():
             do_stuff()


    # 10:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1,4 +1,4 @@
 
         async def foo() -> bool: pass
        -if not foo():
        +if not await foo():
             do_stuff()


    # 11:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 12:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -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:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 14:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 15:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1,3 +1,3 @@
 
         async def bar() -> bool: pass
        -while bar(): pass
        +while await bar(): pass


    # 16:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

        --- 
        +++ 
        @@ -1,3 +1,3 @@
 
         async def bar() -> bool: pass
        -while not bar(): pass
        +while not await bar(): pass


    # 17:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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


    # 18:







    .. code-block:: python


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





    Autofix:

    .. code-block:: python

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

