Aiohttp.web.run_app incompatibility between aiohttp<3.8 and aiohttp>=3.8

H! I use aiohttp in my project Lona and use aiohttp.web.run_app here.

In 3.8 run_app() the keword loop was added, which is mandatory if you don’t want aiohttp to start a new event loop but use your previous created one.

This works fine, but i need a solution that supports aiohttp 3.7.x and 3.8.x. My first thought was to create a helper function in my project like that:

import inspect

from aiohttp.web import run_app as _run_app


def run_app(*args, **kwargs):
    if 'loop' in kwargs:
        arg_spec = inspect.getfullargspec(_run_app)

        if 'loop' not in arg_spec.kwonlyargs:
            kwargs.pop('loop')

    return _run_app(*args, **kwargs)

Is there a better solution?

Sorry, I don’t follow.

Do you want to pass a loop explicitly, reuse the pre-existing default one, or create a new loop always?

I use the pre-existing event loop that runs in the main thread using asyncio.get_event_loop() and i want aiohttp to use that loop too. Now aiohttp creates a ioloop if none is passed in. If i patch my code to run run_app() with the loop argument set, i break compatibility with older aiohttp versions.

version = (int(part) for part in aiohttp.__version__.split('.')[:2])
if version > (3, 8):
    run_app(..., loop=asyncio.get_event_loop())
else:
    run_app(...)

Does it work for you?

Yes that works too! I was hesitant to do string parsing like that because some patch releases contain non-digit characters. Thanks for the quick response!