Skip to content

Commit 096404e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
1 parent 5775111 commit 096404e

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

.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/*
@@ -23,4 +23,4 @@ exclude_lines =
2323
def __init__
2424
def __aenter__
2525
def __aexit__
26-
->exit
26+
->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 - these are excluded from coverage
@@ -61,10 +63,10 @@ def _is_in_trio_context() -> bool:
6163
# Early return if trio is not available
6264
if not has_trio:
6365
return False
64-
66+
6567
# Import trio here since we already checked it's available
6668
import trio
67-
69+
6870
try:
6971
# Will raise RuntimeError if not in trio context
7072
trio.lowlevel.current_task()
@@ -82,9 +84,9 @@ def detect_async_context() -> AsyncContext: # pragma: no cover
8284
"""
8385
# This branch is only taken when anyio is not installed
8486
if not has_anyio or not _is_in_trio_context():
85-
return "asyncio"
87+
return 'asyncio'
8688

87-
return "trio"
89+
return 'trio'
8890

8991

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

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

196-
if context == "trio" and has_anyio:
200+
if context == 'trio' and has_anyio:
197201
try:
198202
import anyio
199203
except Exception:
200204
# Just continue to asyncio if anyio import fails
201205
return asyncio.Lock()
202206
return anyio.Lock()
203-
207+
204208
# For asyncio or unknown contexts
205209
return asyncio.Lock()
206210

@@ -256,4 +260,4 @@ def decorator(
256260
) -> _AwaitableT:
257261
return ReAwaitable(coro(*args, **kwargs)) # type: ignore[return-value]
258262

259-
return decorator
263+
return decorator
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import pytest
2-
from unittest.mock import patch, MagicMock
32

43
from returns.primitives.reawaitable import (
5-
ReAwaitable,
4+
ReAwaitable,
65
reawaitable,
76
)
87

98

109
@pytest.mark.anyio
1110
async def test_reawaitable_lock_creation():
1211
"""Test the _create_lock method for different contexts."""
12+
1313
async def sample_coro() -> str:
1414
return 'value'
15-
15+
1616
# Create a ReAwaitable instance
1717
instance = ReAwaitable(sample_coro())
18-
18+
1919
# Test the lock is initially None
2020
assert instance._lock is None
21-
21+
2222
# Await to trigger lock creation
2323
result = await instance
2424
assert result == 'value'
25-
25+
2626
# Verify lock is created
2727
assert instance._lock is not None
2828

@@ -34,17 +34,18 @@ async def sample_coro() -> str:
3434
@pytest.mark.anyio
3535
async def test_reawaitable_decorator():
3636
"""Test the reawaitable decorator."""
37+
3738
# Define a test coroutine
3839
@reawaitable
3940
async def test_func(value: int) -> int:
4041
return value * 2
41-
42+
4243
# Call the decorated function
4344
result = test_func(5)
44-
45+
4546
# Verify it can be awaited multiple times
4647
assert await result == 10
4748
assert await result == 10 # Should use cached value
4849

4950

50-
# Tests removed as we're using pragmas now
51+
# Tests removed as we're using pragmas now

tests/test_primitives/test_reawaitable/test_reawaitable_lock.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import pytest
2-
import anyio
32

43
from returns.primitives.reawaitable import (
54
ReAwaitable,
6-
detect_async_context,
75
_is_in_trio_context,
6+
detect_async_context,
87
)
98

109

1110
@pytest.mark.anyio
1211
async def test_reawaitable_create_lock():
1312
"""Test that ReAwaitable correctly creates the lock when needed."""
13+
1414
async def sample_coroutine() -> str:
1515
return 'test'
16-
16+
1717
# Create ReAwaitable instance
1818
reawait = ReAwaitable(sample_coroutine())
19-
19+
2020
# The lock should be None initially
2121
assert reawait._lock is None
22-
22+
2323
# Await the coroutine once
2424
result1 = await reawait
25-
25+
2626
# The lock should be created
2727
assert reawait._lock is not None
2828
assert result1 == 'test'
29-
29+
3030
# Await again, should use the same lock
3131
result2 = await reawait
3232
assert result2 == 'test'
@@ -47,4 +47,4 @@ async def test_is_in_trio_context():
4747
# we just check the function runs without errors
4848
result = _is_in_trio_context()
4949
# Result will depend on which backend anyio is using
50-
assert isinstance(result, bool)
50+
assert isinstance(result, bool)

0 commit comments

Comments
 (0)