Aiohttp: Deprecation: @coroutine" decorator is deprecated since Python 3.8, use "async def" instead.

Created on 24 Jun 2020  ยท  7Comments  ยท  Source: aio-libs/aiohttp

๐Ÿž Describe the bug
Use of aiohttp issuing deprecation warnings for python 3.8 and will result in error in 3.10.
Apparently, changing do async def has no negative impact on the user.

๐Ÿ’ก To Reproduce
Call any function that is decorated by @coroutine.
For example aiohttp/helpers.py:107

๐Ÿ’ก Expected behavior
Get a deprecation warning:

  "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
-- Docs: https://docs.pytest.org/en/latest/warnings.html

๐Ÿ“‹ Logs/tracebacks

```python-traceback (paste your traceback in the next line)


๐Ÿ“‹ **Your version of the Python**
<!-- Attach your version of the Python. -->
```console
$  python --version
Python 3.8.2
...

๐Ÿ“‹ Your version of the aiohttp/yarl/multidict distributions

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.6.2
...

```console
$ python -m pip show multidict
Name: multidict
Version: 4.7.6
...

```console
$ python -m pip show yarl
Name: yarl
Version: 1.4.2
...
bug

Most helpful comment

The problematic code here (aiohttp.helpers.noop) was actually removed in #3733, over a year ago, but it hasn't been included in a release.

It would be good to have a 3.6.x release which includes a fix for this (since the fix is very easy, replace @asyncio.coroutine with async def), especially if 4.0 is going to take a long time โ€“ https://aio-libs.discourse.group/t/aiohttp-new-release/49/12

All 7 comments

This seems to be implemented through the asyncio library. I got a message during the check. Maybe I didnโ€™t understand to you. You can explain what you would like.

from aiohttp import web
from asyncio import coroutine

routes = web.RouteTableDef()


@coroutine
def hello(request):
    return web.Response(text="Hello, world")


app = web.Application()
app.add_routes(routes)
web.run_app(app)

stdin:

/$PATH/aiohttp/main.py:8: DeprecationWarning: "@coroutine" decorator is deprecated since Python 3.8, use "async def" instead
  def hello(request):
======== Running on http://0.0.0.0:8080 ========
(Press CTRL+C to quit)

It looks like @coroutine should be replaced with async def in front of the function or method, see here: https://docs.python.org/3/reference/compound_stmts.html#async-def

The coroutine decorator has been very practical for quickly ensuring that user provided functions (e.g. callbacks and the like) were actually coroutine functions (so they can be awaited directly). This is my simple replacement, perhaps others might find it useful:

def coroutine(fn: Callable) -> Callable:
    """ Decorator to convert a regular function to a coroutine function.

        Since asyncio.coroutine is set to be removed in 3.10, this allows
        awaiting a regular function. Not useful as a @-based decorator,
        but very helpful for inline conversions of unknown functions, and
        especially lambdas.
    """
    if iscoroutinefunction(fn):
        return fn

    @wraps(fn)
    async def _wrapper(*args, **kwargs):
        return fn(*args, **kwargs)

    return _wrapper

The problematic code here (aiohttp.helpers.noop) was actually removed in #3733, over a year ago, but it hasn't been included in a release.

It would be good to have a 3.6.x release which includes a fix for this (since the fix is very easy, replace @asyncio.coroutine with async def), especially if 4.0 is going to take a long time โ€“ https://aio-libs.discourse.group/t/aiohttp-new-release/49/12

In the meantime, if you use pytest, disable this warning with the following config:

# pytest.ini
[pytest]
filterwarnings =
    ignore:"@coroutine" decorator is deprecated since Python 3.8, use "async def" instead:DeprecationWarning

Related to #4477

Fixed on master and 3.7

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JulienPalard picture JulienPalard  ยท  3Comments

kbaston picture kbaston  ยท  3Comments

rubenvdham picture rubenvdham  ยท  5Comments

AtomsForPeace picture AtomsForPeace  ยท  5Comments

thehesiod picture thehesiod  ยท  4Comments