Skip to content

Commit aec3bbe

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
1 parent 2d3a0cb commit aec3bbe

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

.coverage_skip.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
"""Coverage configuration for skipping files."""
22

33
from pathlib import Path
4-
import re
4+
55

66
def setup_coverage(cov):
77
"""Setup the coverage configuration."""
88
# Get all skipped files
99
cov.exclude('pragma: no cover')
10-
10+
1111
# Skip any-related code
1212
cov.exclude('if not has_anyio')
1313
cov.exclude('if not has_trio')
1414
cov.exclude('if context == "trio" and has_anyio')
1515
cov.exclude('except RuntimeError:')
1616
cov.exclude('except Exception:')
17-
17+
1818
# Skip protocol definitions
1919
cov.exclude('def __init__')
2020
cov.exclude('def __aenter__')
2121
cov.exclude('def __aexit__')
22-
22+
2323
# Skip branch execution patterns
2424
cov.exclude('->exit')
25-
25+
2626
# Skip specific issues in reawaitable.py
2727
reawaitable_path = Path('returns/primitives/reawaitable.py')
2828
if reawaitable_path.exists():
2929
source = reawaitable_path.read_text()
3030
for i, line in enumerate(source.splitlines(), 1):
31-
if any(x in line for x in [
32-
'import trio',
33-
'import anyio',
34-
'return False',
35-
'return True',
36-
'_is_anyio_available',
37-
'_is_trio_available',
38-
]):
39-
cov.exclude_line(reawaitable_path, i)
31+
if any(
32+
x in line
33+
for x in [
34+
'import trio',
35+
'import anyio',
36+
'return False',
37+
'return True',
38+
'_is_anyio_available',
39+
'_is_trio_available',
40+
]
41+
):
42+
cov.exclude_line(reawaitable_path, i)

.coveragerc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[run]
2-
omit =
2+
omit =
33
returns/contrib/mypy/*
44
returns/contrib/pytest/*.py
55
returns/contrib/hypothesis/*
@@ -36,4 +36,4 @@ exclude_lines =
3636
def __aenter__
3737
def __aexit__
3838
# Skip branch execution patterns
39-
->exit
39+
->exit

returns/primitives/reawaitable.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
# Always import asyncio
2+
import asyncio
13
from collections.abc import Awaitable, Callable, Generator
24
from functools import wraps
35
from typing import Literal, NewType, ParamSpec, Protocol, TypeVar, cast, final
4-
# Always import asyncio
5-
import asyncio
6+
67

78
# pragma: no cover
89
class AsyncLock(Protocol):
910
"""A protocol for an asynchronous lock."""
11+
1012
def __init__(self) -> None: ...
1113
async def __aenter__(self) -> None: ...
1214
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: ...
1315

1416

1517
# Define context types as literals
16-
AsyncContext = Literal["asyncio", "trio", "unknown"]
18+
AsyncContext = Literal['asyncio', 'trio', 'unknown']
1719

1820

1921
# Functions for detecting async context
@@ -60,10 +62,10 @@ def _is_in_trio_context() -> bool:
6062
# Early return if trio is not available
6163
if not has_trio:
6264
return False
63-
65+
6466
# Import trio here since we already checked it's available
6567
import trio
66-
68+
6769
try:
6870
# Will raise RuntimeError if not in trio context
6971
trio.lowlevel.current_task()
@@ -81,9 +83,9 @@ def detect_async_context() -> AsyncContext:
8183
"""
8284
# This branch is only taken when anyio is not installed
8385
if not has_anyio or not _is_in_trio_context():
84-
return "asyncio"
86+
return 'asyncio'
8587

86-
return "trio"
88+
return 'trio'
8789

8890

8991
_ValueType = TypeVar('_ValueType')
@@ -142,7 +144,9 @@ def __init__(self, coro: Awaitable[_ValueType]) -> None:
142144
"""We need just an awaitable to work with."""
143145
self._coro = coro
144146
self._cache: _ValueType | _Sentinel = _sentinel
145-
self._lock: AsyncLock | None = None # Will be created lazily based on the backend
147+
self._lock: AsyncLock | None = (
148+
None # Will be created lazily based on the backend
149+
)
146150

147151
def __await__(self) -> Generator[None, None, _ValueType]:
148152
"""
@@ -192,14 +196,14 @@ def _create_lock(self) -> AsyncLock:
192196
"""Create the appropriate lock based on the current async context."""
193197
context = detect_async_context()
194198

195-
if context == "trio" and has_anyio:
199+
if context == 'trio' and has_anyio:
196200
try:
197201
import anyio
198202
except Exception:
199203
# Just continue to asyncio if anyio import fails
200204
return asyncio.Lock()
201205
return anyio.Lock()
202-
206+
203207
# For asyncio or unknown contexts
204208
return asyncio.Lock()
205209

@@ -254,4 +258,4 @@ def decorator(
254258
) -> _AwaitableT:
255259
return ReAwaitable(coro(*args, **kwargs)) # type: ignore[return-value]
256260

257-
return decorator
261+
return decorator

0 commit comments

Comments
 (0)