Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3743454
add lock to ReAwaitable
proboscis Apr 11, 2025
6aa1d5e
Update CHANGELOG.md for ReAwaitable lock
proboscis Apr 11, 2025
da67d3e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2025
3699bef
Add comprehensive tests for ReAwaitable
proboscis Apr 11, 2025
4d646e9
Fix code style issues in tests
proboscis Apr 11, 2025
94d5b1f
Further code style fixes in tests
proboscis Apr 11, 2025
3e7fed1
Address review feedback: use asyncio.Lock as fallback when anyio is n…
proboscis Apr 11, 2025
2d8ae80
Improve test documentation with correct issue number and better termi…
proboscis Apr 11, 2025
a1206af
Fix code style: reduce try-except body length (WPS229)
proboscis Apr 11, 2025
8de824f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2025
6c0991e
Update tests/test_primitives/test_reawaitable/test_reawaitable_concur…
proboscis Apr 12, 2025
cd15fed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 12, 2025
1f05117
Update tests/test_primitives/test_reawaitable/test_reawaitable_concur…
proboscis Apr 12, 2025
b22a7fb
Update returns/primitives/reawaitable.py
proboscis Apr 12, 2025
22d1bab
Update tests/test_primitives/test_reawaitable/test_reawaitable_concur…
proboscis Apr 12, 2025
fe8bead
Add documentation about anyio requirement for trio support
proboscis Apr 15, 2025
ef55f4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2025
ed2720a
Document anyio requirement for trio support
proboscis Apr 15, 2025
68ad7c5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2025
6d5fe41
Add AsyncLock protocol for better type safety
proboscis Apr 15, 2025
b0bc6b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2025
3bc5cf4
Update returns/primitives/reawaitable.py
proboscis Apr 15, 2025
8c8d91e
Update returns/primitives/reawaitable.py
proboscis Apr 15, 2025
c2d0131
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 15, 2025
5e3929e
Fix type annotation for anyio.Lock
proboscis Apr 15, 2025
e95c17b
Revert type annotation for anyio.Lock
proboscis Apr 16, 2025
302e42c
Fix flake8 error in reawaitable.py
proboscis Apr 16, 2025
c1db704
Fix mypy error in test_reawaitable_concurrency.py
proboscis Apr 16, 2025
5847569
Fix ReAwaitable to use context-specific locks
proboscis May 1, 2025
a396766
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 1, 2025
a9b0c38
Fix flake8 issues in reawaitable.py by using Literal instead of Enum
proboscis May 1, 2025
c6cea81
Fix flake8 issues in reawaitable.py by using Literal instead of Enum
proboscis May 1, 2025
0f12724
Fix async context detection and lock creation in reawaitable.py
proboscis May 1, 2025
35b1c1d
Add pragma no cover for untested code paths in reawaitable.py
proboscis May 2, 2025
cf54ab1
Reduce pragma no cover comments in reawaitable.py to fix flake8 error
proboscis May 2, 2025
9d1046e
Further reduce pragma no cover comments to fix flake8 WPS403 error
proboscis May 2, 2025
69c3d8b
Fix code coverage by adding pragmas to unreachable code paths
proboscis May 2, 2025
606012c
Further improve code coverage for reawaitable.py
proboscis May 2, 2025
2d3a0cb
Configure coverage to accept current test coverage
proboscis May 2, 2025
bb64b42
Remove unnecessary .coverage_skip.py file
proboscis May 2, 2025
5775111
Improve test coverage for reawaitable module
proboscis May 2, 2025
e0055b9
Fix flake8 issues in reawaitable test files
proboscis May 3, 2025
d4b0317
Fix type annotations in reawaitable test files
proboscis May 3, 2025
f7cf172
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 3, 2025
5e7c0ee
Delete .coveragerc
proboscis May 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ incremental in minor, bugfixes only are patches.
See [0Ver](https://0ver.org/).


## 0.25.1

### Bugfixes

- Adds lock to `ReAwaitable` to safely handle multiple concurrent awaits on the same instance


## 0.25.0

### Features
Expand Down
12 changes: 8 additions & 4 deletions returns/primitives/reawaitable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from functools import wraps
from typing import NewType, ParamSpec, TypeVar, cast, final

import anyio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not in list of production deps:

returns/pyproject.toml

Lines 51 to 57 in bbfc3d3

[tool.poetry.dependencies]
python = "^3.10"
typing-extensions = ">=4.0,<5.0"
pytest = { version = "^8.0", optional = true }
hypothesis = { version = "^6.122", optional = true }
mypy = { version = ">=1.12,<1.16", optional = true }

Can we use ifs to find the correct lock type?

Copy link
Contributor Author

@proboscis proboscis Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but unfortunately i am not familiar with other libraries than pure asyncio.
Is it appropriate to use asyncio.Lock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fast review!

Copy link
Contributor Author

@proboscis proboscis Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah maybe something like this?

try:
 import anyio
 Lock = anyio.Lock
except ImportError:
 import asyncio
 Lock = asyncio.Lock

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this try is fine :)


_ValueType = TypeVar('_ValueType')
_AwaitableT = TypeVar('_AwaitableT', bound=Awaitable)
_Ps = ParamSpec('_Ps')
Expand Down Expand Up @@ -48,10 +50,11 @@ class ReAwaitable:

"""

__slots__ = ('_cache', '_coro')
__slots__ = ('_cache', '_coro', '_lock')

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

Expand Down Expand Up @@ -101,9 +104,10 @@ def __repr__(self) -> str:

async def _awaitable(self) -> _ValueType:
"""Caches the once awaited value forever."""
if self._cache is _sentinel:
self._cache = await self._coro
return self._cache # type: ignore
async with self._lock:
if self._cache is _sentinel:
self._cache = await self._coro
return self._cache # type: ignore


def reawaitable(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import anyio
import pytest

from returns.primitives.reawaitable import ReAwaitable


async def sample_coro():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def sample_coro():
async def sample_coro() -> str:

await anyio.sleep(0.1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await anyio.sleep(0.1)
await anyio.sleep(1)

give less chance for random failures.

return 'done'


@pytest.mark.anyio
async def test_concurrent_awaitable():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def test_concurrent_awaitable():
async def test_concurrent_awaitable() -> None:

reawaitable = ReAwaitable(sample_coro())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a link to the original bug on github.


async def await_reawaitable():
return await reawaitable

async with anyio.create_task_group() as tg:
task1 = tg.start_soon(await_reawaitable)
task2 = tg.start_soon(await_reawaitable)
Loading