From bd7d0df2406d251bc2be8dcdc513a84f205d9e8d Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 2 Apr 2021 17:49:33 +0100 Subject: [PATCH 1/4] Add types for pytest plugin --- aiohttp/pytest_plugin.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 50466410bc3..d0d61d0415b 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, Optional, 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() -> TestClient: """ Client class to use in ``aiohttp_client`` factory. @@ -327,7 +330,7 @@ def test_login(aiohttp_client): @pytest.fixture -def aiohttp_client(loop, aiohttp_client_cls): # type: ignore[no-untyped-def] +def aiohttp_client(loop, aiohttp_client_cls: TestClient) -> AiohttpClient: """Factory to create a TestClient instance. aiohttp_client(app, **kwargs) @@ -336,9 +339,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) From dad459be0fe7dc2d9a2e42c4f2ccfa125e2b595c Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 2 Apr 2021 19:06:39 +0100 Subject: [PATCH 2/4] Update pytest_plugin.py --- aiohttp/pytest_plugin.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index d0d61d0415b..49f1a8ede8a 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -2,7 +2,7 @@ import contextlib import inspect import warnings -from typing import Any, Awaitable, Callable, Dict, Optional, Union +from typing import Any, Awaitable, Callable, Dict, Generator, Optional, Type, Union import pytest @@ -303,7 +303,7 @@ async def finalize() -> None: @pytest.fixture -def aiohttp_client_cls() -> TestClient: +def aiohttp_client_cls() -> Type[TestClient]: """ Client class to use in ``aiohttp_client`` factory. @@ -330,7 +330,9 @@ def test_login(aiohttp_client): @pytest.fixture -def aiohttp_client(loop, aiohttp_client_cls: TestClient) -> AiohttpClient: +def aiohttp_client( + loop: asyncio.AbstractEventLoop, aiohttp_client_cls: TestClient +) -> Generator[AiohttpClient, None, None]: """Factory to create a TestClient instance. aiohttp_client(app, **kwargs) From 6b5533ebb42780b383bc5dee4ae32d5ac897e279 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 2 Apr 2021 19:08:41 +0100 Subject: [PATCH 3/4] Update pytest_plugin.py --- aiohttp/pytest_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 49f1a8ede8a..153db35276b 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -331,7 +331,7 @@ def test_login(aiohttp_client): @pytest.fixture def aiohttp_client( - loop: asyncio.AbstractEventLoop, aiohttp_client_cls: TestClient + loop: asyncio.AbstractEventLoop, aiohttp_client_cls: Type[TestClient] ) -> Generator[AiohttpClient, None, None]: """Factory to create a TestClient instance. From f7fb4cbaa43afce402c9070b3361436512fae5b8 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 2 Apr 2021 19:10:29 +0100 Subject: [PATCH 4/4] Create 5585.feature --- CHANGES/5585.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/5585.feature 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.