Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add types for pytest plugin #5585

Merged
merged 4 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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