@@ -18,31 +18,31 @@ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: ...
18
18
AsyncContext = Literal ["asyncio" , "trio" , "unknown" ]
19
19
20
20
21
- def _is_anyio_available () -> bool :
21
+ def _is_anyio_available () -> bool : # pragma: no cover
22
22
"""Check if anyio is available.
23
23
24
24
Returns:
25
25
bool: True if anyio is available
26
26
"""
27
27
try :
28
- import anyio # pragma: no cover
29
- except ImportError : # pragma: no cover
28
+ import anyio
29
+ except ImportError :
30
30
return False
31
31
return True
32
32
33
33
34
- def _is_trio_available () -> bool :
34
+ def _is_trio_available () -> bool : # pragma: no cover
35
35
"""Check if trio is available.
36
36
37
37
Returns:
38
38
bool: True if trio is available
39
39
"""
40
40
if not _is_anyio_available ():
41
- return False # pragma: no cover
41
+ return False
42
42
43
43
try :
44
- import trio # pragma: no cover
45
- except ImportError : # pragma: no cover
44
+ import trio
45
+ except ImportError :
46
46
return False
47
47
return True
48
48
@@ -58,8 +58,8 @@ def _is_in_trio_context() -> bool:
58
58
Returns:
59
59
bool: True if we're in a trio context
60
60
"""
61
- if not has_trio :
62
- return False # pragma: no cover
61
+ if not has_trio : # pragma: no cover
62
+ return False
63
63
64
64
# Import trio here since we already checked it's available
65
65
import trio
@@ -79,14 +79,11 @@ def detect_async_context() -> AsyncContext:
79
79
Returns:
80
80
AsyncContext: The current async context type
81
81
"""
82
- if not has_anyio : # pragma: no cover
82
+ # This branch is only taken when anyio is not installed
83
+ if not has_anyio or not _is_in_trio_context (): # pragma: no cover
83
84
return "asyncio"
84
85
85
- if _is_in_trio_context ():
86
- return "trio"
87
-
88
- # Default to asyncio
89
- return "asyncio"
86
+ return "trio"
90
87
91
88
92
89
_ValueType = TypeVar ('_ValueType' )
@@ -195,13 +192,13 @@ def _create_lock(self) -> AsyncLock:
195
192
"""Create the appropriate lock based on the current async context."""
196
193
context = detect_async_context ()
197
194
198
- if context == "trio" and has_anyio :
195
+ if context == "trio" and has_anyio : # pragma: no cover
199
196
try :
200
197
import anyio
201
- except Exception : # pragma: no cover
198
+ except Exception :
202
199
# Just continue to asyncio if anyio import fails
203
- return asyncio .Lock () # pragma: no cover
204
- return anyio .Lock () # pragma: no cover
200
+ return asyncio .Lock ()
201
+ return anyio .Lock ()
205
202
206
203
# For asyncio or unknown contexts
207
204
return asyncio .Lock ()
@@ -217,12 +214,11 @@ async def _awaitable(self) -> _ValueType:
217
214
if self ._cache is _sentinel :
218
215
self ._cache = await self ._coro
219
216
return self ._cache # type: ignore
220
- except RuntimeError :
217
+ except RuntimeError : # pragma: no cover
221
218
# Fallback for when running in asyncio context with trio detection
222
219
if self ._cache is _sentinel :
223
220
self ._cache = await self ._coro
224
221
return self ._cache # type: ignore
225
- # pragma: no cover
226
222
227
223
228
224
def reawaitable (
0 commit comments