Skip to content

Commit

Permalink
Add types for pytest plugin (aio-libs#5585)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored and commonism committed Apr 27, 2021
1 parent 691be5e commit 59fe3b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/5585.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``aiohttp.pytest_plugin.AiohttpClient`` for static typing of pytest plugin.
18 changes: 13 additions & 5 deletions aiohttp/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import contextlib
import inspect
import warnings
from typing import Any, Awaitable, Callable, Dict, Generator, Optional, Type, Union

import pytest

Expand All @@ -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(
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 59fe3b5

Please sign in to comment.