generated from amazon-archives/__template_Apache-2.0
-
Couldn't load subscription status.
- Fork 453
feat: Support adding exception notes for Python 3.10 #1034
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
zastrowm
merged 2 commits into
strands-agents:main
from
zastrowm:support_exception_info_for_310
Oct 16, 2025
+115
−23
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| """Exception note utilities for Python 3.10+ compatibility.""" | ||
|
|
||
| # add_note was added in 3.11 - we hoist to a constant to facilitate testing | ||
| supports_add_note = hasattr(Exception, "add_note") | ||
|
|
||
|
|
||
| def add_exception_note(exception: Exception, note: str) -> None: | ||
| """Add a note to an exception, compatible with Python 3.10+. | ||
| Uses add_note() if it's available (Python 3.11+) or modifies the exception message if it is not. | ||
| """ | ||
| if supports_add_note: | ||
zastrowm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # we ignore the mypy error because the version-check for add_note is extracted into a constant up above and | ||
| # mypy doesn't detect that | ||
| exception.add_note(note) # type: ignore | ||
| else: | ||
| # For Python 3.10, append note to the exception message | ||
| if hasattr(exception, "args") and exception.args: | ||
| exception.args = (f"{exception.args[0]}\n{note}",) + exception.args[1:] | ||
| else: | ||
| exception.args = (note,) | ||
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,51 @@ | ||
| """Tests for exception note utilities.""" | ||
|
|
||
| import sys | ||
| import traceback | ||
| import unittest.mock | ||
|
|
||
| import pytest | ||
|
|
||
| from strands import _exception_notes | ||
| from strands._exception_notes import add_exception_note | ||
|
|
||
|
|
||
| @pytest.mark.skipif(sys.version_info < (3, 11), reason="This test requires Python 3.11 or higher (need add_note)") | ||
| def test_add_exception_note_python_311_plus(): | ||
| """Test add_exception_note uses add_note in Python 3.11+.""" | ||
| exception = ValueError("original message") | ||
|
|
||
| add_exception_note(exception, "test note") | ||
|
|
||
| assert traceback.format_exception(exception) == ["ValueError: original message\n", "test note\n"] | ||
|
|
||
|
|
||
| def test_add_exception_note_python_310(): | ||
| """Test add_exception_note modifies args in Python 3.10.""" | ||
| with unittest.mock.patch.object(_exception_notes, "supports_add_note", False): | ||
| exception = ValueError("original message") | ||
|
|
||
| add_exception_note(exception, "test note") | ||
|
|
||
| assert traceback.format_exception(exception) == ["ValueError: original message\ntest note\n"] | ||
|
|
||
|
|
||
| def test_add_exception_note_python_310_no_args(): | ||
| """Test add_exception_note handles exception with no args in Python 3.10.""" | ||
| with unittest.mock.patch.object(_exception_notes, "supports_add_note", False): | ||
| exception = ValueError() | ||
| exception.args = () | ||
|
|
||
| add_exception_note(exception, "test note") | ||
|
|
||
| assert traceback.format_exception(exception) == ["ValueError: test note\n"] | ||
|
|
||
|
|
||
| def test_add_exception_note_python_310_multiple_args(): | ||
| """Test add_exception_note preserves additional args in Python 3.10.""" | ||
| with unittest.mock.patch.object(_exception_notes, "supports_add_note", False): | ||
| exception = ValueError("original message", "second arg") | ||
|
|
||
| add_exception_note(exception, "test note") | ||
|
|
||
| assert traceback.format_exception(exception) == ["ValueError: ('original message\\ntest note', 'second arg')\n"] |
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.