Skip to content

Commit 3e7fed1

Browse files
committed
Address review feedback: use asyncio.Lock as fallback when anyio is not available and improve test types
1 parent 94d5b1f commit 3e7fed1

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

returns/primitives/reawaitable.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
from functools import wraps
33
from typing import NewType, ParamSpec, TypeVar, cast, final
44

5-
import anyio
5+
# Try to use anyio.Lock, fall back to asyncio.Lock
6+
try:
7+
import anyio
8+
Lock = anyio.Lock
9+
except ImportError:
10+
import asyncio
11+
Lock = asyncio.Lock
612

713
_ValueType = TypeVar('_ValueType')
814
_AwaitableT = TypeVar('_AwaitableT', bound=Awaitable)
@@ -54,7 +60,7 @@ class ReAwaitable:
5460

5561
def __init__(self, coro: Awaitable[_ValueType]) -> None:
5662
"""We need just an awaitable to work with."""
57-
self._lock = anyio.Lock()
63+
self._lock = Lock()
5864
self._coro = coro
5965
self._cache: _ValueType | _Sentinel = _sentinel
6066

tests/test_primitives/test_reawaitable/test_reawaitable_concurrency.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@
44
from returns.primitives.reawaitable import ReAwaitable, reawaitable
55

66

7-
async def sample_coro():
8-
await anyio.sleep(0.1)
7+
# Fix for issue with multiple awaits on the same ReAwaitable instance
8+
# causing race conditions: https://github.com/dry-python/returns/issues/2048
9+
async def sample_coro() -> str:
10+
"""Sample coroutine for testing."""
11+
await anyio.sleep(1) # Increased from 0.1 to reduce chance of random failures
912
return 'done'
1013

1114

12-
async def await_helper(awaitable_obj):
15+
async def await_helper(awaitable_obj) -> str:
1316
"""Helper to await objects in tasks."""
1417
return await awaitable_obj
1518

1619

1720
@pytest.mark.anyio
18-
async def test_concurrent_awaitable():
21+
async def test_concurrent_awaitable() -> None:
1922
"""Test that ReAwaitable works with concurrent awaits."""
2023
test_target = ReAwaitable(sample_coro())
2124

@@ -25,11 +28,11 @@ async def test_concurrent_awaitable():
2528

2629

2730
@pytest.mark.anyio # noqa: WPS210
28-
async def test_reawaitable_decorator():
31+
async def test_reawaitable_decorator() -> None:
2932
"""Test the reawaitable decorator with concurrent awaits."""
3033

31-
async def test_coro(): # noqa: WPS430
32-
await anyio.sleep(0.1)
34+
async def test_coro() -> str: # noqa: WPS430
35+
await anyio.sleep(1) # Increased from 0.1 to reduce chance of random failures
3336
return "decorated"
3437

3538
decorated = reawaitable(test_coro)
@@ -49,10 +52,10 @@ async def test_coro(): # noqa: WPS430
4952

5053

5154
@pytest.mark.anyio
52-
async def test_reawaitable_repr():
55+
async def test_reawaitable_repr() -> None:
5356
"""Test the __repr__ method of ReAwaitable."""
5457

55-
async def test_func(): # noqa: WPS430
58+
async def test_func() -> int: # noqa: WPS430
5659
return 1
5760

5861
coro = test_func()

0 commit comments

Comments
 (0)