Skip to content
Myr

Enforce that sync client uses asyncio-based backend (#232)

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

authored by Florimond Manca · committed by Seth Michael Larson · parent caba785 · Browse files · 2 files+34 added−0 removed
This commit

State

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

Diff stats

2 files+34 added−0 removed

  • tests:+16 added−0 removed
  • other:+18 added−0 removed

2 files+34 added−0 removed

modified:httpx/client.py+18 added
68 unmodified lines
69 ):70 if backend is None:71 backend = AsyncioBackend()7273 self.check_concurrency_backend(backend)7475 if app is not None:76 param_count = len(inspect.signature(app).parameters)77 assert param_count in (2, 3)78 if param_count == 2:
30 unmodified lines
109 self.dispatch = async_dispatch110 self.concurrency_backend = backend111 self.trust_env = True if trust_env is None else trust_env112113 def check_concurrency_backend(self, backend: ConcurrencyBackend) -> None:114 pass # pragma: no cover115116 def merge_url(self, url: URLTypes) -> URL:117 url = self.base_url.join(relative_url=url)118 if url.scheme == "http" and hstspreload.in_hsts_preload(url.host):119 url = url.copy_with(scheme="https")
507 unmodified lines
627 await self.close()628629630class Client(BaseClient):631 def check_concurrency_backend(self, backend: ConcurrencyBackend) -> None:632 # Iterating over response content allocates an async environment on each step.633 # This is relatively cheap on asyncio, but cannot be guaranteed for all634 # concurrency backends.635 # The sync client performs I/O on its own, so it doesn't need to support636 # arbitrary concurrency backends.637 # Therefore, we kept the `backend` parameter (for testing/mocking), but enforce638 # that the concurrency backend derives from the asyncio one.639 if not isinstance(backend, AsyncioBackend):640 raise ValueError(641 "'Client' only supports asyncio-based concurrency backends"642 )643644 def _async_request_data(645 self, data: RequestData = None646 ) -> typing.Optional[AsyncRequestData]:647 """
341 unmodified lines
modified:tests/client/test_client.py+16 added
156 unmodified lines
157 url = client.merge_url("http://www.paypal.com")158159 assert url.scheme == "https"160 assert url.is_ssl161162163class DerivedFromAsyncioBackend(httpx.AsyncioBackend):164 pass165166167class AnyBackend:168 pass169170171def test_client_backend_must_be_asyncio_based():172 httpx.Client(backend=httpx.AsyncioBackend())173 httpx.Client(backend=DerivedFromAsyncioBackend())174175 with pytest.raises(ValueError):176 httpx.Client(backend=AnyBackend())