Skip to content

Commit fdad167

Browse files
authored
Merge pull request #45 from python-trio/anyiono
Don't hard-depend on anyio
2 parents cdd7233 + 2294b86 commit fdad167

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/asyncclick/termui.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
from gettext import gettext as _
1111
from inspect import iscoroutine
1212

13-
import anyio
14-
1513
from ._compat import isatty
1614
from ._compat import strip_ansi
1715
from .exceptions import Abort
@@ -171,9 +169,34 @@ def prompt_func(text: str) -> str:
171169
async def run_prompt_func(text: str) -> str:
172170
return prompt_func(text)
173171
else:
172+
# Not having a hard dependency on any async runtime is harder than it looks.
173+
try:
174+
import anyio
175+
176+
except ImportError:
177+
try:
178+
import sniffio # type: ignore
179+
except ImportError:
180+
backend = "asyncio"
181+
else:
182+
backend = sniffio.current_async_library()
183+
184+
if backend == "trio":
185+
import trio
186+
187+
def run_prompt_func(text: str) -> t.Awaitable[str]:
188+
return trio.to_thread.run_sync(prompt_func, text)
189+
190+
else:
191+
import asyncio
192+
193+
def run_prompt_func(text: str) -> t.Awaitable[str]:
194+
return asyncio.to_thread(prompt_func, text)
195+
196+
else:
174197

175-
def run_prompt_func(text: str) -> t.Awaitable[str]:
176-
return anyio.to_thread.run_sync(prompt_func, text)
198+
def run_prompt_func(text: str) -> t.Awaitable[str]:
199+
return anyio.to_thread.run_sync(prompt_func, text)
177200

178201
if value_proc is None:
179202
value_proc = convert_type(type, default)

0 commit comments

Comments
 (0)