Skip to content

Commit f0fd919

Browse files
fix type annotation for MockTransport (#2581)
* fix type annotation for MockTransport * add type ignore * better type checks * better type checks * add pragma --------- Co-authored-by: Tom Christie <[email protected]>
1 parent 18e0ae4 commit f0fd919

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

httpx/_transports/mock.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import asyncio
21
import typing
32

43
from .._models import Request, Response
54
from .base import AsyncBaseTransport, BaseTransport
65

6+
SyncHandler = typing.Callable[[Request], Response]
7+
AsyncHandler = typing.Callable[[Request], typing.Coroutine[None, None, Response]]
8+
79

810
class MockTransport(AsyncBaseTransport, BaseTransport):
9-
def __init__(self, handler: typing.Callable[[Request], Response]) -> None:
11+
def __init__(self, handler: typing.Union[SyncHandler, AsyncHandler]) -> None:
1012
self.handler = handler
1113

1214
def handle_request(
1315
self,
1416
request: Request,
1517
) -> Response:
1618
request.read()
17-
return self.handler(request)
19+
response = self.handler(request)
20+
if not isinstance(response, Response): # pragma: no cover
21+
raise TypeError("Cannot use an async handler in a sync Client")
22+
return response
1823

1924
async def handle_async_request(
2025
self,
@@ -27,8 +32,7 @@ async def handle_async_request(
2732
# If it is, then the `response` variable need to be awaited to actually
2833
# return the result.
2934

30-
# https://simonwillison.net/2020/Sep/2/await-me-maybe/
31-
if asyncio.iscoroutine(response):
35+
if not isinstance(response, Response):
3236
response = await response
3337

3438
return response

tests/client/test_async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ async def test_mounted_transport():
313313

314314
@pytest.mark.anyio
315315
async def test_async_mock_transport():
316-
async def hello_world(request):
316+
async def hello_world(request: httpx.Request) -> httpx.Response:
317317
return httpx.Response(200, text="Hello, world!")
318318

319319
transport = httpx.MockTransport(hello_world)

tests/client/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ class ConsumeBodyTransport(httpx.MockTransport):
710710
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
711711
assert isinstance(request.stream, httpx.AsyncByteStream)
712712
[_ async for _ in request.stream]
713-
return self.handler(request)
713+
return self.handler(request) # type: ignore[return-value]
714714

715715

716716
@pytest.mark.anyio

tests/client/test_redirects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class ConsumeBodyTransport(httpx.MockTransport):
346346
def handle_request(self, request: httpx.Request) -> httpx.Response:
347347
assert isinstance(request.stream, httpx.SyncByteStream)
348348
[_ for _ in request.stream]
349-
return self.handler(request)
349+
return self.handler(request) # type: ignore[return-value]
350350

351351

352352
def test_cannot_redirect_streaming_body():

0 commit comments

Comments
 (0)