Skip to content

Commit eede455

Browse files
committed
Sync from aiohttp
1 parent 74b6faf commit eede455

File tree

6 files changed

+120
-126
lines changed

6 files changed

+120
-126
lines changed

.mypy.ini

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[mypy]
2+
files = pytest_aiohttp, tests
3+
check_untyped_defs = True
4+
follow_imports_for_stubs = True
5+
disallow_any_decorated = True
6+
disallow_any_generics = True
7+
disallow_any_unimported = True
8+
disallow_incomplete_defs = True
9+
disallow_subclassing_any = True
10+
disallow_untyped_calls = True
11+
disallow_untyped_decorators = True
12+
disallow_untyped_defs = True
13+
# TODO(PY312): explicit-override
14+
enable_error_code = ignore-without-code, possibly-undefined, redundant-expr, redundant-self, truthy-bool, truthy-iterable, unused-awaitable
15+
extra_checks = True
16+
implicit_reexport = False
17+
no_implicit_optional = True
18+
pretty = True
19+
show_column_numbers = True
20+
show_error_codes = True
21+
show_error_code_links = True
22+
strict_equality = True
23+
warn_incomplete_stub = True
24+
warn_redundant_casts = True
25+
warn_return_any = True
26+
warn_unreachable = True
27+
warn_unused_ignores = True

pytest_aiohttp/plugin.py

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,60 @@
11
import asyncio
2-
import warnings
3-
from typing import Any, Awaitable, Callable, Dict, Generator, Optional, Type, Union
2+
from typing import (
3+
Any,
4+
Awaitable,
5+
Callable,
6+
Dict,
7+
Iterator,
8+
Optional,
9+
Protocol,
10+
Type,
11+
TypeVar,
12+
Union,
13+
overload,
14+
)
415

516
import pytest
617
import pytest_asyncio
718
from aiohttp.test_utils import BaseTestServer, RawTestServer, TestClient, TestServer
8-
from aiohttp.web import Application, BaseRequest, StreamResponse
19+
from aiohttp.web import Application, BaseRequest, Request
20+
from aiohttp.web_protocol import _RequestHandler
21+
22+
_Request = TypeVar("_Request", bound=BaseRequest)
923

10-
AiohttpClient = Callable[[Union[Application, BaseTestServer]], Awaitable[TestClient]]
24+
25+
class AiohttpClient(Protocol):
26+
@overload
27+
async def __call__(
28+
self,
29+
__param: Application,
30+
*,
31+
server_kwargs: Optional[Dict[str, Any]] = None,
32+
**kwargs: Any
33+
) -> TestClient[Request, Application]: ...
34+
@overload
35+
async def __call__(
36+
self,
37+
__param: BaseTestServer[_Request],
38+
*,
39+
server_kwargs: Optional[Dict[str, Any]] = None,
40+
**kwargs: Any
41+
) -> TestClient[_Request, None]: ...
42+
43+
44+
class AiohttpServer(Protocol):
45+
def __call__(
46+
self, app: Application, *, port: Optional[int] = None, **kwargs: Any
47+
) -> Awaitable[TestServer]: ...
48+
49+
50+
class AiohttpRawServer(Protocol):
51+
def __call__(
52+
self,
53+
handler: _RequestHandler[BaseRequest],
54+
*,
55+
port: Optional[int] = None,
56+
**kwargs: Any
57+
) -> Awaitable[RawTestServer]: ...
1158

1259

1360
LEGACY_MODE = DeprecationWarning(
@@ -28,41 +75,8 @@ def pytest_configure(config) -> None:
2875
config.issue_config_time_warning(LEGACY_MODE, stacklevel=2)
2976

3077

31-
@pytest.fixture
32-
def loop(event_loop: asyncio.AbstractEventLoop) -> asyncio.AbstractEventLoop:
33-
warnings.warn(
34-
"'loop' fixture is deprecated and scheduled for removal, "
35-
"please use 'event_loop' instead",
36-
DeprecationWarning,
37-
)
38-
return event_loop
39-
40-
41-
@pytest.fixture
42-
def proactor_loop(event_loop: asyncio.AbstractEventLoop) -> asyncio.AbstractEventLoop:
43-
warnings.warn(
44-
"'proactor_loop' fixture is deprecated and scheduled for removal, "
45-
"please use 'event_loop' instead",
46-
DeprecationWarning,
47-
)
48-
return event_loop
49-
50-
51-
@pytest.fixture
52-
def aiohttp_unused_port(
53-
unused_tcp_port_factory: Callable[[], int]
54-
) -> Callable[[], int]:
55-
warnings.warn(
56-
"'aiohttp_unused_port' fixture is deprecated "
57-
"and scheduled for removal, "
58-
"please use 'unused_tcp_port_factory' instead",
59-
DeprecationWarning,
60-
)
61-
return unused_tcp_port_factory
62-
63-
6478
@pytest_asyncio.fixture
65-
async def aiohttp_server() -> Callable[..., Awaitable[TestServer]]:
79+
async def aiohttp_server() -> Iterator[AiohttpServer]:
6680
"""Factory to create a TestServer instance, given an app.
6781
6882
aiohttp_server(app, **kwargs)
@@ -84,18 +98,18 @@ async def go(
8498

8599

86100
@pytest_asyncio.fixture
87-
async def aiohttp_raw_server() -> Callable[..., Awaitable[RawTestServer]]:
101+
async def aiohttp_raw_server() -> Iterator[AiohttpRawServer]:
88102
"""Factory to create a RawTestServer instance, given a web handler.
89103
90104
aiohttp_raw_server(handler, **kwargs)
91105
"""
92106
servers = []
93107

94108
async def go(
95-
handler: Callable[[BaseRequest], Awaitable[StreamResponse]],
109+
handler: _RequestHandler[BaseRequest],
96110
*,
97111
port: Optional[int] = None,
98-
**kwargs: Any,
112+
**kwargs: Any
99113
) -> RawTestServer:
100114
server = RawTestServer(handler, port=port)
101115
await server.start_server(**kwargs)
@@ -108,8 +122,8 @@ async def go(
108122
await servers.pop().close()
109123

110124

111-
@pytest.fixture
112-
def aiohttp_client_cls() -> Type[TestClient]:
125+
@pytest_asyncio.fixture
126+
def aiohttp_client_cls() -> Type[TestClient[Any, Any]]:
113127
"""
114128
Client class to use in ``aiohttp_client`` factory.
115129
@@ -137,8 +151,8 @@ def test_login(aiohttp_client):
137151

138152
@pytest_asyncio.fixture
139153
async def aiohttp_client(
140-
aiohttp_client_cls: Type[TestClient],
141-
) -> Generator[AiohttpClient, None, None]:
154+
aiohttp_client_cls: Type[TestClient[Any, Any]]
155+
) -> Iterator[AiohttpClient]:
142156
"""Factory to create a TestClient instance.
143157
144158
aiohttp_client(app, **kwargs)
@@ -147,12 +161,26 @@ async def aiohttp_client(
147161
"""
148162
clients = []
149163

164+
@overload
165+
async def go(
166+
__param: Application,
167+
*,
168+
server_kwargs: Optional[Dict[str, Any]] = None,
169+
**kwargs: Any
170+
) -> TestClient[Request, Application]: ...
171+
@overload
172+
async def go(
173+
__param: BaseTestServer[_Request],
174+
*,
175+
server_kwargs: Optional[Dict[str, Any]] = None,
176+
**kwargs: Any
177+
) -> TestClient[_Request, None]: ...
150178
async def go(
151-
__param: Union[Application, BaseTestServer],
179+
__param: Union[Application, BaseTestServer[Any]],
152180
*,
153181
server_kwargs: Optional[Dict[str, Any]] = None,
154-
**kwargs: Any,
155-
) -> TestClient:
182+
**kwargs: Any
183+
) -> TestClient[Any, Any]:
156184
if isinstance(__param, Application):
157185
server_kwargs = server_kwargs or {}
158186
server = TestServer(__param, **server_kwargs)

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ setup_requires =
4141

4242
install_requires =
4343
pytest >= 6.1.0
44-
aiohttp >= 3.8.1
44+
aiohttp >= 3.11.0
4545
pytest-asyncio >= 0.17.2
4646

4747
[options.extras_require]
4848
testing =
4949
coverage == 6.2
50-
mypy == 0.931
50+
mypy == 1.12.1
5151

5252
[options.entry_points]
5353
pytest11 =

tests/test_fixtures.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
from typing import Any
1+
import pytest
22

3-
pytest_plugins: str = "pytester"
3+
pytest_plugins = "pytester"
44

55

6-
def test_aiohttp_plugin(testdir: Any) -> None:
6+
def test_aiohttp_plugin(testdir: pytest.Testdir) -> None:
77
testdir.makepyfile(
88
"""\
99
import pytest
1010
from unittest import mock
1111
1212
from aiohttp import web
1313
14+
value = web.AppKey('value', str)
15+
1416
1517
async def hello(request):
1618
return web.Response(body=b'Hello, world')
@@ -54,11 +56,11 @@ async def test_noop() -> None:
5456
5557
async def previous(request):
5658
if request.method == 'POST':
57-
with pytest.warns(DeprecationWarning):
58-
request.app['value'] = (await request.post())['value']
59+
with pytest.deprecated_call(): # FIXME: this isn't actually called
60+
request.app[value] = (await request.post())['value']
5961
return web.Response(body=b'thanks for the data')
6062
else:
61-
v = request.app.get('value', 'unknown')
63+
v = request.app.get(value, 'unknown')
6264
return web.Response(body='value: {}'.format(v).encode())
6365
6466
@@ -100,14 +102,13 @@ async def test_custom_port_test_server(aiohttp_server, unused_tcp_port):
100102
app = await create_app()
101103
server = await aiohttp_server(app, port=unused_tcp_port)
102104
assert server.port == unused_tcp_port
103-
104105
"""
105106
)
106107
result = testdir.runpytest("--asyncio-mode=auto")
107108
result.assert_outcomes(passed=8)
108109

109110

110-
def test_aiohttp_raw_server(testdir: Any) -> None:
111+
def test_aiohttp_raw_server(testdir: pytest.Testdir) -> None:
111112
testdir.makepyfile(
112113
"""\
113114
import pytest
@@ -135,14 +136,13 @@ async def test_hello(cli) -> None:
135136
assert resp.status == 200
136137
text = await resp.text()
137138
assert 'OK' in text
138-
139139
"""
140140
)
141141
result = testdir.runpytest("--asyncio-mode=auto")
142142
result.assert_outcomes(passed=1)
143143

144144

145-
def test_aiohttp_client_cls_fixture_custom_client_used(testdir: Any) -> None:
145+
def test_aiohttp_client_cls_fixture_custom_client_used(testdir: pytest.Testdir) -> None:
146146
testdir.makepyfile(
147147
"""
148148
import pytest
@@ -169,7 +169,7 @@ async def test_hello(aiohttp_client) -> None:
169169
result.assert_outcomes(passed=1)
170170

171171

172-
def test_aiohttp_client_cls_fixture_factory(testdir: Any) -> None:
172+
def test_aiohttp_client_cls_fixture_factory(testdir: pytest.Testdir) -> None:
173173
testdir.makeconftest(
174174
"""\
175175

tests/test_obsolete_fixtures.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

tests/test_switch_mode.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from typing import Any
2-
1+
import pytest
32
from pytest_aiohttp.plugin import LEGACY_MODE
43

54
pytest_plugins: str = "pytester"
65

76

8-
def test_warning_for_legacy_mode(testdir: Any) -> None:
7+
def test_warning_for_legacy_mode(testdir: pytest.Testdir) -> None:
98
testdir.makepyfile(
109
"""\
1110
async def test_a():
@@ -18,7 +17,7 @@ async def test_a():
1817
result.stdout.fnmatch_lines(["*" + str(LEGACY_MODE) + "*"])
1918

2019

21-
def test_auto_mode(testdir: Any) -> None:
20+
def test_auto_mode(testdir: pytest.Testdir) -> None:
2221
testdir.makepyfile(
2322
"""\
2423
async def test_a():
@@ -31,7 +30,7 @@ async def test_a():
3130
result.stdout.no_fnmatch_line("*" + str(LEGACY_MODE) + "*")
3231

3332

34-
def test_strict_mode(testdir: Any) -> None:
33+
def test_strict_mode(testdir: pytest.Testdir) -> None:
3534
testdir.makepyfile(
3635
"""\
3736
import pytest

0 commit comments

Comments
 (0)