Skip to content
Myr

fix type annotation for MockTransport (#2581)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix type annotation for MockTransport * add type ignore * better type checks * better type checks * add pragma ---------

authored by Adrian Garcia Badaracco · committed by GitHub · parent 18e0ae4 · Browse files · 4 files+12 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

4 files+12 added−8 removed

  • tests:+3 added−3 removed
  • other:+9 added−5 removed

4 files+12 added−8 removed

modified:httpx/_transports/mock.py+9 added-5 removed
1import asyncio1import typing23from .._models import Request, Response4from .base import AsyncBaseTransport, BaseTransport56SyncHandler = typing.Callable[[Request], Response]7AsyncHandler = typing.Callable[[Request], typing.Coroutine[None, None, Response]]8910class MockTransport(AsyncBaseTransport, BaseTransport):9 def __init__(self, handler: typing.Callable[[Request], Response]) -> None:11 def __init__(self, handler: typing.Union[SyncHandler, AsyncHandler]) -> None:12 self.handler = handler1314 def handle_request(15 self,16 request: Request,17 ) -> Response:18 request.read()17 return self.handler(request)19 response = self.handler(request)20 if not isinstance(response, Response): # pragma: no cover21 raise TypeError("Cannot use an async handler in a sync Client")22 return response2324 async def handle_async_request(25 self,26 request: Request,
4 unmodified lines
31 # Allow handler to *optionally* be an `async` function.32 # If it is, then the `response` variable need to be awaited to actually33 # return the result.3430 # https://simonwillison.net/2020/Sep/2/await-me-maybe/31 if asyncio.iscoroutine(response):35 if not isinstance(response, Response):36 response = await response3738 return response
modified:tests/client/test_async_client.py+1 added-1 removed
311 unmodified lines
312313314@pytest.mark.anyio315async def test_async_mock_transport():316 async def hello_world(request):316 async def hello_world(request: httpx.Request) -> httpx.Response:317 return httpx.Response(200, text="Hello, world!")318319 transport = httpx.MockTransport(hello_world)320
53 unmodified lines
modified:tests/client/test_auth.py+1 added-1 removed
708 unmodified lines
709class ConsumeBodyTransport(httpx.MockTransport):710 async def handle_async_request(self, request: httpx.Request) -> httpx.Response:711 assert isinstance(request.stream, httpx.AsyncByteStream)712 [_ async for _ in request.stream]713 return self.handler(request)713 return self.handler(request) # type: ignore[return-value]714715716@pytest.mark.anyio717async def test_digest_auth_unavailable_streaming_body():
76 unmodified lines
modified:tests/client/test_redirects.py+1 added-1 removed
344 unmodified lines
345class ConsumeBodyTransport(httpx.MockTransport):346 def handle_request(self, request: httpx.Request) -> httpx.Response:347 assert isinstance(request.stream, httpx.SyncByteStream)348 [_ for _ in request.stream]349 return self.handler(request)349 return self.handler(request) # type: ignore[return-value]350351352def test_cannot_redirect_streaming_body():353 client = httpx.Client(transport=ConsumeBodyTransport(redirects))
94 unmodified lines