diff --git a/CHANGES/4957.bugfix b/CHANGES/4957.bugfix new file mode 100644 index 00000000000..b86f1cd3e7f --- /dev/null +++ b/CHANGES/4957.bugfix @@ -0,0 +1 @@ +Fix run_app typing diff --git a/aiohttp/web.py b/aiohttp/web.py index 219a3654983..b666a4277e1 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -5,7 +5,18 @@ from argparse import ArgumentParser from collections.abc import Iterable from importlib import import_module -from typing import Any, Awaitable, Callable, List, Optional, Set, Type, Union, cast +from typing import ( + Any as Any, + Awaitable as Awaitable, + Callable as Callable, + Iterable as TypingIterable, + List as List, + Optional as Optional, + Set as Set, + Type as Type, + Union as Union, + cast as cast, +) from .abc import AbstractAccessLogger from .helpers import all_tasks @@ -270,11 +281,13 @@ except ImportError: # pragma: no cover SSLContext = Any # type: ignore +HostSequence = TypingIterable[str] + async def _run_app( app: Union[Application, Awaitable[Application]], *, - host: Optional[str] = None, + host: Optional[Union[str, HostSequence]] = None, port: Optional[int] = None, path: Optional[str] = None, sock: Optional[socket.socket] = None, @@ -448,7 +461,7 @@ def run_app( app: Union[Application, Awaitable[Application]], *, debug: bool = False, - host: Optional[str] = None, + host: Optional[Union[str, HostSequence]] = None, port: Optional[int] = None, path: Optional[str] = None, sock: Optional[socket.socket] = None, diff --git a/tests/test_run_app.py b/tests/test_run_app.py index 0bb8910fcae..241d7dbd791 100644 --- a/tests/test_run_app.py +++ b/tests/test_run_app.py @@ -459,6 +459,27 @@ def test_run_app_nondefault_host_port(patched_loop, aiohttp_unused_port) -> None ) +def test_run_app_multiple_hosts(patched_loop) -> None: + hosts = ("127.0.0.1", "127.0.0.2") + + app = web.Application() + web.run_app(app, host=hosts, print=stopper(patched_loop)) + + calls = map( + lambda h: mock.call( + mock.ANY, + h, + 8080, + ssl=None, + backlog=128, + reuse_address=None, + reuse_port=None, + ), + hosts, + ) + patched_loop.create_server.assert_has_calls(calls) + + def test_run_app_custom_backlog(patched_loop) -> None: app = web.Application() web.run_app(app, backlog=10, print=stopper(patched_loop))