@@ -18,31 +18,31 @@ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: ...
1818AsyncContext = Literal ["asyncio" , "trio" , "unknown" ]
1919
2020
21- def _is_anyio_available () -> bool :
21+ def _is_anyio_available () -> bool : # pragma: no cover
2222 """Check if anyio is available.
2323
2424 Returns:
2525 bool: True if anyio is available
2626 """
2727 try :
28- import anyio # pragma: no cover
29- except ImportError : # pragma: no cover
28+ import anyio
29+ except ImportError :
3030 return False
3131 return True
3232
3333
34- def _is_trio_available () -> bool :
34+ def _is_trio_available () -> bool : # pragma: no cover
3535 """Check if trio is available.
3636
3737 Returns:
3838 bool: True if trio is available
3939 """
4040 if not _is_anyio_available ():
41- return False # pragma: no cover
41+ return False
4242
4343 try :
44- import trio # pragma: no cover
45- except ImportError : # pragma: no cover
44+ import trio
45+ except ImportError :
4646 return False
4747 return True
4848
@@ -58,8 +58,8 @@ def _is_in_trio_context() -> bool:
5858 Returns:
5959 bool: True if we're in a trio context
6060 """
61- if not has_trio :
62- return False # pragma: no cover
61+ if not has_trio : # pragma: no cover
62+ return False
6363
6464 # Import trio here since we already checked it's available
6565 import trio
@@ -79,14 +79,11 @@ def detect_async_context() -> AsyncContext:
7979 Returns:
8080 AsyncContext: The current async context type
8181 """
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
8384 return "asyncio"
8485
85- if _is_in_trio_context ():
86- return "trio"
87-
88- # Default to asyncio
89- return "asyncio"
86+ return "trio"
9087
9188
9289_ValueType = TypeVar ('_ValueType' )
@@ -195,13 +192,13 @@ def _create_lock(self) -> AsyncLock:
195192 """Create the appropriate lock based on the current async context."""
196193 context = detect_async_context ()
197194
198- if context == "trio" and has_anyio :
195+ if context == "trio" and has_anyio : # pragma: no cover
199196 try :
200197 import anyio
201- except Exception : # pragma: no cover
198+ except Exception :
202199 # 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 ()
205202
206203 # For asyncio or unknown contexts
207204 return asyncio .Lock ()
@@ -217,12 +214,11 @@ async def _awaitable(self) -> _ValueType:
217214 if self ._cache is _sentinel :
218215 self ._cache = await self ._coro
219216 return self ._cache # type: ignore
220- except RuntimeError :
217+ except RuntimeError : # pragma: no cover
221218 # Fallback for when running in asyncio context with trio detection
222219 if self ._cache is _sentinel :
223220 self ._cache = await self ._coro
224221 return self ._cache # type: ignore
225- # pragma: no cover
226222
227223
228224def reawaitable (
0 commit comments