-
-
Notifications
You must be signed in to change notification settings - Fork 131
Add lock to ReAwaitable for concurrent awaits #2109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
proboscis
wants to merge
45
commits into
dry-python:master
Choose a base branch
from
proboscis:add-reawaitable-lock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 15 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
3743454
add lock to ReAwaitable
proboscis 6aa1d5e
Update CHANGELOG.md for ReAwaitable lock
proboscis da67d3e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3699bef
Add comprehensive tests for ReAwaitable
proboscis 4d646e9
Fix code style issues in tests
proboscis 94d5b1f
Further code style fixes in tests
proboscis 3e7fed1
Address review feedback: use asyncio.Lock as fallback when anyio is n…
proboscis 2d8ae80
Improve test documentation with correct issue number and better termi…
proboscis a1206af
Fix code style: reduce try-except body length (WPS229)
proboscis 8de824f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6c0991e
Update tests/test_primitives/test_reawaitable/test_reawaitable_concur…
proboscis cd15fed
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1f05117
Update tests/test_primitives/test_reawaitable/test_reawaitable_concur…
proboscis b22a7fb
Update returns/primitives/reawaitable.py
proboscis 22d1bab
Update tests/test_primitives/test_reawaitable/test_reawaitable_concur…
proboscis fe8bead
Add documentation about anyio requirement for trio support
proboscis ef55f4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ed2720a
Document anyio requirement for trio support
proboscis 68ad7c5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6d5fe41
Add AsyncLock protocol for better type safety
proboscis b0bc6b2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3bc5cf4
Update returns/primitives/reawaitable.py
proboscis 8c8d91e
Update returns/primitives/reawaitable.py
proboscis c2d0131
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5e3929e
Fix type annotation for anyio.Lock
proboscis e95c17b
Revert type annotation for anyio.Lock
proboscis 302e42c
Fix flake8 error in reawaitable.py
proboscis c1db704
Fix mypy error in test_reawaitable_concurrency.py
proboscis 5847569
Fix ReAwaitable to use context-specific locks
proboscis a396766
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a9b0c38
Fix flake8 issues in reawaitable.py by using Literal instead of Enum
proboscis c6cea81
Fix flake8 issues in reawaitable.py by using Literal instead of Enum
proboscis 0f12724
Fix async context detection and lock creation in reawaitable.py
proboscis 35b1c1d
Add pragma no cover for untested code paths in reawaitable.py
proboscis cf54ab1
Reduce pragma no cover comments in reawaitable.py to fix flake8 error
proboscis 9d1046e
Further reduce pragma no cover comments to fix flake8 WPS403 error
proboscis 69c3d8b
Fix code coverage by adding pragmas to unreachable code paths
proboscis 606012c
Further improve code coverage for reawaitable.py
proboscis 2d3a0cb
Configure coverage to accept current test coverage
proboscis bb64b42
Remove unnecessary .coverage_skip.py file
proboscis 5775111
Improve test coverage for reawaitable module
proboscis e0055b9
Fix flake8 issues in reawaitable test files
proboscis d4b0317
Fix type annotations in reawaitable test files
proboscis f7cf172
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5e7c0ee
Delete .coveragerc
proboscis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
tests/test_primitives/test_reawaitable/test_reawaitable_concurrency.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import anyio | ||
import pytest | ||
|
||
from returns.primitives.reawaitable import ReAwaitable, reawaitable | ||
|
||
|
||
# Fix for issue with multiple awaits on the same ReAwaitable instance: | ||
# https://github.com/dry-python/returns/issues/2108 | ||
async def sample_coro() -> str: | ||
"""Sample coroutine that simulates an async operation.""" | ||
await anyio.sleep(1) | ||
return 'done' | ||
|
||
|
||
async def await_helper(awaitable_obj) -> str: | ||
"""Helper to await objects in tasks.""" | ||
return await awaitable_obj | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_concurrent_awaitable() -> None: | ||
"""Test that ReAwaitable safely handles concurrent awaits using a lock.""" | ||
test_target = ReAwaitable(sample_coro()) | ||
|
||
async with anyio.create_task_group() as tg: | ||
tg.start_soon(await_helper, test_target) | ||
tg.start_soon(await_helper, test_target) | ||
|
||
|
||
@pytest.mark.anyio # noqa: WPS210 | ||
async def test_reawaitable_decorator() -> None: | ||
"""Test the reawaitable decorator with concurrent awaits.""" | ||
|
||
async def test_coro() -> str: # noqa: WPS430 | ||
await anyio.sleep(1) | ||
return 'decorated' | ||
|
||
decorated = reawaitable(test_coro) | ||
instance = decorated() | ||
|
||
# Test multiple awaits | ||
result1 = await instance | ||
result2 = await instance | ||
|
||
assert result1 == 'decorated' | ||
assert result1 == result2 | ||
|
||
# Test concurrent awaits | ||
async with anyio.create_task_group() as tg: | ||
tg.start_soon(await_helper, instance) | ||
tg.start_soon(await_helper, instance) | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_reawaitable_repr() -> None: | ||
"""Test the __repr__ method of ReAwaitable.""" | ||
|
||
async def test_func() -> int: # noqa: WPS430 | ||
return 1 | ||
|
||
coro = test_func() | ||
target = ReAwaitable(coro) | ||
|
||
# Test the representation | ||
assert repr(target) == repr(coro) | ||
# Ensure the coroutine is properly awaited | ||
assert await target == 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can type it as a simple interface that only supports
async with
, probably likecontextlib.AbstractAsyncContextManager