Skip to content

Commit

Permalink
Fix Python 3.11 support
Browse files Browse the repository at this point in the history
'asyncio.coroutine' was deprecated in 3.8 and removed in 3.11:
https://docs.python.org/3.9/library/asyncio-task.html\#asyncio.coroutine

On the other hand, the 'async def' syntax is supported since 3.5:
https://peps.python.org/pep-0492/

This also requires bumping the flake8 version, in order to support
'async def' (though it has to be flake8 < 6.0.0, in order to keep
support for Python 3.7).
  • Loading branch information
progval authored and douardda committed Feb 14, 2023
1 parent 8a63e1c commit e524883
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v3
Expand Down
11 changes: 4 additions & 7 deletions aiohttp_utils/negotiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,21 @@ def negotiation_middleware(
"""Middleware which selects a renderer for a given request then renders
a handler's data to a `aiohttp.web.Response`.
"""
@asyncio.coroutine
def factory(app, handler):

@asyncio.coroutine
def middleware(request):
async def factory(app, handler):
async def middleware(request):
content_type, renderer = negotiator(
request,
renderers,
force_negotiation,
)
request['selected_media_type'] = content_type
response = yield from handler(request)
response = await handler(request)

data = getattr(response, 'data', None)
if isinstance(response, Response) and (force_rendering or data):
# Render data with the selected renderer
if asyncio.iscoroutinefunction(renderer):
render_result = yield from renderer(request, data)
render_result = await renderer(request, data)
else:
render_result = renderer(request, data)
else:
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ wheel
twine

# Syntax checking
flake8==2.4.1
flake8==5.0.4

-e .
8 changes: 2 additions & 6 deletions examples/kitchen_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
$ http :8000/
$ http :8000/api/
"""
from asyncio import coroutine

from aiohttp import web
from aiohttp_utils import Response, routing, negotiation

app = web.Application(router=routing.ResourceRouter())


@coroutine
def index(request):
async def index(request):
return Response('Welcome!')


class HelloResource:

@coroutine
def get(self, request):
async def get(self, request):
return Response({
'message': 'Welcome to the API!'
})
Expand Down
4 changes: 1 addition & 3 deletions examples/mako_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""
from collections import OrderedDict
from collections.abc import Mapping
from asyncio import coroutine

from aiohttp import web
from aiohttp_utils import Response, negotiation
Expand All @@ -39,8 +38,7 @@

# ##### Handlers #####

@coroutine
def index(request):
async def index(request):
return Response({
'message': 'Hello ' + request.query.get('name', 'World')
})
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def maker(app, *args, **kwargs):


def make_dummy_handler(**kwargs):
@asyncio.coroutine
def dummy(request):
async def dummy(request):
return web.Response(**kwargs)
return dummy
7 changes: 2 additions & 5 deletions tests/test_negotiation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import asyncio
from collections import OrderedDict

from aiohttp import web
Expand Down Expand Up @@ -28,12 +27,10 @@ def handler_false(request):
def handler_none(request):
return Response(None)

@asyncio.coroutine
def coro_handler(request):
async def coro_handler(request):
return Response({'message': 'Hello coro'})

@asyncio.coroutine
def post_coro_handler(request):
async def post_coro_handler(request):
return Response({'message': 'Post coro'}, status=201)

def handler_no_nego(request):
Expand Down
4 changes: 1 addition & 3 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import pytest
import asyncio

from aiohttp import web

Expand All @@ -25,8 +24,7 @@ class MyResource:
def get(self, request):
return web.Response(body=b'Got it', content_type='text/plain')

@asyncio.coroutine
def post(self, request):
async def post(self, request):
return web.Response(body=b'Posted it', content_type='text/plain')

class MyResource2:
Expand Down
26 changes: 8 additions & 18 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
"""Handlers for testing routing"""
from asyncio import coroutine

from aiohttp import web


@coroutine
def index(request):
async def index(request):
return web.Response()


@coroutine
def list_projects(request):
async def list_projects(request):
return web.Response()


@coroutine
def create_projects(request):
async def create_projects(request):
return web.Response()


class ArticleResource:

@coroutine
def get(self, request):
async def get(self, request):
return web.Response()

@coroutine
def post(self, request):
async def post(self, request):
return web.Response()


class ArticleList:

@coroutine
def get(self, request):
async def get(self, request):
return web.Response()

@coroutine
def post(self, request):
async def post(self, request):
return web.Response()


Expand All @@ -46,6 +37,5 @@ class AuthorList:
def __init__(self, db):
self.db = db

@coroutine
def get(self, request):
async def get(self, request):
return web.Response()
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ python =
3.8: py38
3.9: py39
3.10: py310
3.11: py311

[testenv]
deps=
Expand Down

0 comments on commit e524883

Please sign in to comment.