Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch backend detection to use entrypoints. #810

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Version history

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Switched backend loading to use
`import entrypoints <https://setuptools.pypa.io/en/latest/userguide/entry_point.html>`_.

**4.6.2**

- Fixed regression caused by (`#807 <https://github.com/agronholm/anyio/pull/807>`_)
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ doc = [
[project.entry-points]
pytest11 = {anyio = "anyio.pytest_plugin"}

[project.entry-points."anyio.backends"]
trio = "anyio._backends._trio:backend_class"
asyncio = "anyio._backends._asyncio:backend_class"

[tool.setuptools_scm]
version_scheme = "post-release"
local_scheme = "dirty-tag"
Expand Down
39 changes: 32 additions & 7 deletions src/anyio/_core/_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import threading
from collections.abc import Awaitable, Callable, Generator
from contextlib import contextmanager
from importlib import import_module
from importlib.metadata import EntryPoint, entry_points
from typing import TYPE_CHECKING, Any, TypeVar

import sniffio
Expand All @@ -18,13 +18,38 @@
if TYPE_CHECKING:
from ..abc import AsyncBackend


def find_backends() -> dict[str, EntryPoint]:
"""
Loads the available backends from setuptools entrypoints.
"""

backends: dict[str, EntryPoint]

# for some reason, in 3.12 and above, EntryPoints.__getitem__ was changed to return a
# *single* entrypoint, rather than a list.

if sys.version_info < (3, 12):
eps = entry_points()
found_backends = eps["anyio.backends"]
backends = {ep.name: ep for ep in found_backends}
else:
eps = entry_points(group="anyio.backends")
backends = {}
for ep in eps:
backends[ep.name] = ep

return backends


# This must be updated when new backends are introduced
BACKENDS = "asyncio", "trio"

T_Retval = TypeVar("T_Retval")
PosArgsT = TypeVarTuple("PosArgsT")

threadlocals = threading.local()
available_backends: dict[str, EntryPoint] = find_backends()
loaded_backends: dict[str, type[AsyncBackend]] = {}


Expand Down Expand Up @@ -60,7 +85,7 @@ def run(

try:
async_backend = get_async_backend(backend)
except ImportError as exc:
except (ImportError, KeyError) as exc:
raise LookupError(f"No such backend: {backend}") from exc

token = None
Expand Down Expand Up @@ -124,8 +149,8 @@ def current_time() -> float:


def get_all_backends() -> tuple[str, ...]:
"""Return a tuple of the names of all built-in backends."""
return BACKENDS
"""Return a tuple of the names of all available backends."""
return tuple(available_backends.keys())


def get_cancelled_exc_class() -> type[BaseException]:
Expand Down Expand Up @@ -161,6 +186,6 @@ def get_async_backend(asynclib_name: str | None = None) -> type[AsyncBackend]:
try:
return loaded_backends[asynclib_name]
except KeyError:
module = import_module(f"anyio._backends._{asynclib_name}")
loaded_backends[asynclib_name] = module.backend_class
return module.backend_class
loaded_backend: type[AsyncBackend] = available_backends[asynclib_name].load()
loaded_backends[asynclib_name] = loaded_backend
return loaded_backend
9 changes: 8 additions & 1 deletion tests/test_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pytest import MonkeyPatch
from pytest_mock.plugin import MockerFixture

from anyio import run, sleep_forever, sleep_until
from anyio import get_all_backends, run, sleep_forever, sleep_until

pytestmark = pytest.mark.anyio
fake_current_time = 1620581544.0
Expand Down Expand Up @@ -48,6 +48,13 @@ async def async_add(x: int, y: int) -> int:
assert result == 3


def test_find_builtin_backends() -> None:
backends = get_all_backends()
assert len(backends) >= 2
assert any(x == "trio" for x in backends)
assert any(x == "asyncio" for x in backends)


class TestAsyncioOptions:
def test_debug(self) -> None:
async def main() -> bool:
Expand Down