diff --git a/CHANGES/5585.feature b/CHANGES/5585.feature new file mode 100644 index 00000000000..06ddbe453d4 --- /dev/null +++ b/CHANGES/5585.feature @@ -0,0 +1 @@ +Add ``aiohttp.pytest_plugin.AiohttpClient`` for static typing of pytest plugin. diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 50466410bc3..153db35276b 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -2,6 +2,7 @@ import contextlib import inspect import warnings +from typing import Any, Awaitable, Callable, Dict, Generator, Optional, Type, Union import pytest @@ -28,6 +29,8 @@ except ImportError: # pragma: no cover tokio = None +AiohttpClient = Callable[[Union[Application, BaseTestServer]], Awaitable[TestClient]] + def pytest_addoption(parser): # type: ignore[no-untyped-def] parser.addoption( @@ -300,7 +303,7 @@ async def finalize() -> None: @pytest.fixture -def aiohttp_client_cls(): # type: ignore[no-untyped-def] +def aiohttp_client_cls() -> Type[TestClient]: """ Client class to use in ``aiohttp_client`` factory. @@ -327,7 +330,9 @@ def test_login(aiohttp_client): @pytest.fixture -def aiohttp_client(loop, aiohttp_client_cls): # type: ignore[no-untyped-def] +def aiohttp_client( + loop: asyncio.AbstractEventLoop, aiohttp_client_cls: Type[TestClient] +) -> Generator[AiohttpClient, None, None]: """Factory to create a TestClient instance. aiohttp_client(app, **kwargs) @@ -336,9 +341,12 @@ def aiohttp_client(loop, aiohttp_client_cls): # type: ignore[no-untyped-def] """ clients = [] - async def go( # type: ignore[no-untyped-def] - __param, *, server_kwargs=None, **kwargs - ): + async def go( + __param: Union[Application, BaseTestServer], + *, + server_kwargs: Optional[Dict[str, Any]] = None, + **kwargs: Any + ) -> TestClient: if isinstance(__param, Application): server_kwargs = server_kwargs or {} server = TestServer(__param, **server_kwargs)