Skip to content

Commit

Permalink
Add types for pytest plugin (#5600)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam Bull <[email protected]>
  • Loading branch information
Dreamsorcerer and Dreamsorcerer authored Apr 7, 2021
1 parent 3e1abca commit c525654
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 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.
16 changes: 12 additions & 4 deletions aiohttp/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import contextlib
import warnings
from collections.abc import Callable
from typing import Any, Awaitable, Callable, Dict, Generator, Optional, Union

import pytest

Expand Down Expand Up @@ -29,6 +30,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 @@ -331,7 +334,9 @@ def raw_test_server( # type: ignore[no-untyped-def] # pragma: no cover


@pytest.fixture
def aiohttp_client(loop): # type: ignore[no-untyped-def]
def aiohttp_client(
loop: asyncio.AbstractEventLoop,
) -> Generator[AiohttpClient, None, None]:
"""Factory to create a TestClient instance.
aiohttp_client(app, **kwargs)
Expand All @@ -340,9 +345,12 @@ def aiohttp_client(loop): # type: ignore[no-untyped-def]
"""
clients = []

async def go( # type: ignore[no-untyped-def]
__param, *args, server_kwargs=None, **kwargs
):
async def go(
__param: Union[Application, BaseTestServer],
*args: Any,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any
) -> TestClient:

if isinstance(__param, Callable) and not isinstance( # type: ignore[arg-type]
__param, (Application, BaseTestServer)
Expand Down

0 comments on commit c525654

Please sign in to comment.