generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 535
bidi - remove python 3.11+ features #1302
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5421b56
bidi - remove python 3.11+ features
pgrayy b693903
Merge branch 'main' of https://github.com/strands-agents/sdk-python i…
pgrayy 5b449b3
run - raise on exception only
pgrayy db934ac
reraise external cancellations
pgrayy 75cc8af
task group
pgrayy be3efbf
Merge branch 'main' of https://github.com/strands-agents/sdk-python i…
pgrayy bfc32cc
wording
pgrayy 93f1fa1
test regex
pgrayy 2ecbd22
bidi exception chain
pgrayy 0279685
docs
pgrayy f9a093a
Merge branch 'main' of https://github.com/strands-agents/sdk-python i…
pgrayy 0aaf10d
remove exception chain
pgrayy e79b925
reduce changes
pgrayy f95cf3c
Merge branch 'main' of https://github.com/strands-agents/sdk-python i…
pgrayy 70b2feb
remove extra change
pgrayy 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Manage a group of async tasks. | ||
| This is intended to mimic the behaviors of asyncio.TaskGroup released in Python 3.11. | ||
| - Docs: https://docs.python.org/3/library/asyncio-task.html#task-groups | ||
| """ | ||
|
|
||
| import asyncio | ||
| from typing import Any, Coroutine | ||
|
|
||
|
|
||
| class _TaskGroup: | ||
pgrayy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Shim of asyncio.TaskGroup for use in Python 3.10. | ||
| Attributes: | ||
| _tasks: List of tasks in group. | ||
| """ | ||
|
|
||
| _tasks: list[asyncio.Task] | ||
|
|
||
| def create_task(self, coro: Coroutine[Any, Any, Any]) -> asyncio.Task: | ||
| """Create an async task and add to group. | ||
| Returns: | ||
| The created task. | ||
| """ | ||
| task = asyncio.create_task(coro) | ||
| self._tasks.append(task) | ||
| return task | ||
|
|
||
| async def __aenter__(self) -> "_TaskGroup": | ||
| """Setup self managed task group context.""" | ||
| self._tasks = [] | ||
| return self | ||
|
|
||
| async def __aexit__(self, *_: Any) -> None: | ||
| """Execute tasks in group. | ||
| The following execution rules are enforced: | ||
| - The context stops executing all tasks if at least one task raises an Exception or the context is cancelled. | ||
| - The context re-raises Exceptions to the caller. | ||
| - The context re-raises CancelledErrors to the caller only if the context itself was cancelled. | ||
| """ | ||
| try: | ||
| await asyncio.gather(*self._tasks) | ||
|
|
||
| except (Exception, asyncio.CancelledError) as error: | ||
| for task in self._tasks: | ||
| task.cancel() | ||
|
|
||
| await asyncio.gather(*self._tasks, return_exceptions=True) | ||
|
|
||
| if not isinstance(error, asyncio.CancelledError): | ||
| raise | ||
|
|
||
| context_task = asyncio.current_task() | ||
| if context_task and context_task.cancelling() > 0: # context itself was cancelled | ||
| raise | ||
|
|
||
| finally: | ||
| self._tasks = [] | ||
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
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,59 @@ | ||
| import asyncio | ||
| import unittest.mock | ||
|
|
||
| import pytest | ||
|
|
||
| from strands.experimental.bidi._async._task_group import _TaskGroup | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_task_group__aexit__(): | ||
| coro = unittest.mock.AsyncMock() | ||
|
|
||
| async with _TaskGroup() as task_group: | ||
| task_group.create_task(coro()) | ||
|
|
||
| coro.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_task_group__aexit__exception(): | ||
| wait_event = asyncio.Event() | ||
| async def wait(): | ||
| await wait_event.wait() | ||
|
|
||
| async def fail(): | ||
| raise ValueError("test error") | ||
|
|
||
| with pytest.raises(ValueError, match=r"test error"): | ||
| async with _TaskGroup() as task_group: | ||
| wait_task = task_group.create_task(wait()) | ||
| fail_task = task_group.create_task(fail()) | ||
|
|
||
| assert wait_task.cancelled() | ||
| assert not fail_task.cancelled() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_task_group__aexit__cancelled(): | ||
| wait_event = asyncio.Event() | ||
| async def wait(): | ||
| await wait_event.wait() | ||
|
|
||
| tasks = [] | ||
|
|
||
| run_event = asyncio.Event() | ||
| async def run(): | ||
| async with _TaskGroup() as task_group: | ||
| tasks.append(task_group.create_task(wait())) | ||
| run_event.set() | ||
|
|
||
| run_task = asyncio.create_task(run()) | ||
| await run_event.wait() | ||
| run_task.cancel() | ||
|
|
||
| with pytest.raises(asyncio.CancelledError): | ||
| await run_task | ||
|
|
||
| wait_task = tasks[0] | ||
| assert wait_task.cancelled() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.