From 2294b865948a2ce79a4586c2d982a2f54b1d0558 Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Tue, 7 Oct 2025 17:49:51 +0200 Subject: [PATCH] Don't hard-depend on anyio Closes:#44 (we hope) --- src/asyncclick/termui.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/asyncclick/termui.py b/src/asyncclick/termui.py index ef21c3cfc..069d36a99 100644 --- a/src/asyncclick/termui.py +++ b/src/asyncclick/termui.py @@ -10,8 +10,6 @@ from gettext import gettext as _ from inspect import iscoroutine -import anyio - from ._compat import isatty from ._compat import strip_ansi from .exceptions import Abort @@ -171,9 +169,34 @@ def prompt_func(text: str) -> str: async def run_prompt_func(text: str) -> str: return prompt_func(text) else: + # Not having a hard dependency on any async runtime is harder than it looks. + try: + import anyio + + except ImportError: + try: + import sniffio # type: ignore + except ImportError: + backend = "asyncio" + else: + backend = sniffio.current_async_library() + + if backend == "trio": + import trio + + def run_prompt_func(text: str) -> t.Awaitable[str]: + return trio.to_thread.run_sync(prompt_func, text) + + else: + import asyncio + + def run_prompt_func(text: str) -> t.Awaitable[str]: + return asyncio.to_thread(prompt_func, text) + + else: - def run_prompt_func(text: str) -> t.Awaitable[str]: - return anyio.to_thread.run_sync(prompt_func, text) + def run_prompt_func(text: str) -> t.Awaitable[str]: + return anyio.to_thread.run_sync(prompt_func, text) if value_proc is None: value_proc = convert_type(type, default)