diff --git a/httpcore/_backends/auto.py b/httpcore/_backends/auto.py index 3ac05f4d..9b362764 100644 --- a/httpcore/_backends/auto.py +++ b/httpcore/_backends/auto.py @@ -1,14 +1,14 @@ import typing from typing import Optional -from .._synchronization import current_async_library +from .._synchronization import current_async_backend from .base import SOCKET_OPTION, AsyncNetworkBackend, AsyncNetworkStream class AutoBackend(AsyncNetworkBackend): async def _init_backend(self) -> None: if not (hasattr(self, "_backend")): - backend = current_async_library() + backend = current_async_backend() if backend == "trio": from .trio import TrioBackend diff --git a/httpcore/_synchronization.py b/httpcore/_synchronization.py index 3dc4cf85..2d86f8b8 100644 --- a/httpcore/_synchronization.py +++ b/httpcore/_synchronization.py @@ -1,5 +1,4 @@ import asyncio -import os import threading from types import TracebackType from typing import ( @@ -31,7 +30,6 @@ AsyncBackend = Literal["asyncio", "trio"] -AsyncLibrary = Literal["asyncio", "trio", "anyio"] def current_async_backend() -> AsyncBackend: @@ -47,6 +45,11 @@ def current_async_backend() -> AsyncBackend: if environment not in ("asyncio", "trio"): # pragma: nocover raise RuntimeError("Running under an unsupported async environment.") + if environment == "asyncio" and anyio is None: # pragma: nocover + raise RuntimeError( + "Running with asyncio requires installation of 'httpcore[asyncio]'." + ) + if environment == "trio" and trio is None: # pragma: nocover raise RuntimeError( "Running with trio requires installation of 'httpcore[trio]'." @@ -55,18 +58,6 @@ def current_async_backend() -> AsyncBackend: return environment -def current_async_library() -> AsyncLibrary: - if current_async_backend() == "trio": - return "trio" - - if anyio is not None: - anyio_env = os.environ.get("HTTPCORE_PREFER_ANYIO", "true").lower() - if anyio_env in ("true", "1"): - return "anyio" - - return "asyncio" - - class _LockProto(Protocol): async def acquire(self) -> Any: ... def release(self) -> None: ... diff --git a/tests/test_synchronization.py b/tests/test_synchronization.py deleted file mode 100644 index d74e58e9..00000000 --- a/tests/test_synchronization.py +++ /dev/null @@ -1,42 +0,0 @@ -import os -import typing -from typing import Generator, List - -import pytest - -from httpcore import AnyIOBackend, TrioBackend -from httpcore._backends.auto import AutoBackend -from httpcore._synchronization import AsyncLibrary, current_async_library - - -@pytest.fixture(scope="session", autouse=True) -def check_tested_async_libraries() -> Generator[List[str], None, None]: - # Ensure tests cover all supported async variants - async_libraries: List[str] = [] - yield async_libraries - expected = sorted(["asyncio", "anyio", "trio"]) - assert sorted(async_libraries) == expected - assert sorted(typing.get_args(AsyncLibrary)) == expected - - -@pytest.mark.anyio -async def test_current_async_library(anyio_backend, check_tested_async_libraries): - current = current_async_library() - check_tested_async_libraries.append(current) - - backend_name, _ = anyio_backend - auto_backend = AutoBackend() - await auto_backend._init_backend() - - if backend_name == "trio": - assert current == "trio" - assert isinstance(auto_backend._backend, TrioBackend) - else: - assert backend_name == "asyncio" - if os.environ["HTTPCORE_PREFER_ANYIO"] == "1": - assert current == "anyio" - assert isinstance(auto_backend._backend, AnyIOBackend) - else: - assert os.environ["HTTPCORE_PREFER_ANYIO"] == "0" - assert current == "asyncio" - assert isinstance(auto_backend._backend, AnyIOBackend)