This repository was archived by the owner on Nov 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add docstring for split function and catch function, also try to make ExceptionGroup can work for now :) #3
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
501a0d6
add docstring for split, catch.
WindSoilder 2a22cd5
add __copy__ for ExceptionGroup class, to make it can be splitted.
WindSoilder 96b7da2
fix catch call.
WindSoilder a45781c
add unit tests for split functions, and fix a little issue when Excep…
WindSoilder e0ab8de
fix docstring, and rename test file.
WindSoilder ee3e826
assign __traceback__ and __context__ of exception during copy.
WindSoilder eef4d66
1. The __copy__ method should also copy attribute. 2. Update test c…
WindSoilder 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 was deleted.
Oops, something went wrong.
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,117 @@ | ||
from exceptiongroup import ExceptionGroup, split | ||
|
||
|
||
def raise_error(err): | ||
raise err | ||
|
||
|
||
def raise_error_from_another(out_err, another_err): | ||
# use try..except approache so out_error have meaningful | ||
# __context__, __cause__ attribute. | ||
try: | ||
raise another_err | ||
except Exception as e: | ||
raise out_err from e | ||
|
||
|
||
def test_split_for_none_exception(): | ||
matched, unmatched = split(RuntimeError, None) | ||
assert matched is None | ||
assert unmatched is None | ||
|
||
|
||
def test_split_when_all_exception_matched(): | ||
group = ExceptionGroup( | ||
"Many Errors", | ||
[RuntimeError("Runtime Error1"), RuntimeError("Runtime Error2")], | ||
["Runtime Error1", "Runtime Error2"] | ||
) | ||
matched, unmatched = split(RuntimeError, group) | ||
assert matched is group | ||
assert unmatched is None | ||
|
||
|
||
def test_split_when_all_exception_unmatched(): | ||
group = ExceptionGroup( | ||
"Many Errors", | ||
[RuntimeError("Runtime Error1"), RuntimeError("Runtime Error2")], | ||
["Runtime Error1", "Runtime Error2"] | ||
) | ||
matched, unmatched = split(ValueError, group) | ||
assert matched is None | ||
assert unmatched is group | ||
|
||
|
||
def test_split_when_contains_matched_and_unmatched(): | ||
error1 = RuntimeError("Runtime Error1") | ||
error2 = ValueError("Value Error2") | ||
group = ExceptionGroup( | ||
"Many Errors", | ||
[error1, error2], | ||
['Runtime Error1', 'Value Error2'] | ||
) | ||
matched, unmatched = split(RuntimeError, group) | ||
assert isinstance(matched, ExceptionGroup) | ||
assert isinstance(unmatched, ExceptionGroup) | ||
assert matched.exceptions == [error1] | ||
assert matched.message == "Many Errors" | ||
assert matched.sources == ['Runtime Error1'] | ||
assert unmatched.exceptions == [error2] | ||
assert unmatched.message == "Many Errors" | ||
assert unmatched.sources == ['Value Error2'] | ||
WindSoilder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_split_with_predicate(): | ||
def _match(err): | ||
return str(err) != 'skip' | ||
|
||
error1 = RuntimeError("skip") | ||
error2 = RuntimeError("Runtime Error") | ||
group = ExceptionGroup( | ||
"Many Errors", | ||
[error1, error2], | ||
['skip', 'Runtime Error'] | ||
) | ||
matched, unmatched = split(RuntimeError, group, match=_match) | ||
assert matched.exceptions == [error2] | ||
assert unmatched.exceptions == [error1] | ||
|
||
|
||
def test_split_with_single_exception(): | ||
err = RuntimeError("Error") | ||
matched, unmatched = split(RuntimeError, err) | ||
assert matched is err | ||
assert unmatched is None | ||
|
||
matched, unmatched = split(ValueError, err) | ||
assert matched is None | ||
assert unmatched is err | ||
|
||
|
||
def test_split_and_check_attributes_same(): | ||
try: | ||
raise_error(RuntimeError("RuntimeError")) | ||
except Exception as e: | ||
run_error = e | ||
|
||
try: | ||
raise_error(ValueError("ValueError")) | ||
except Exception as e: | ||
val_error = e | ||
|
||
group = ExceptionGroup( | ||
"ErrorGroup", [run_error, val_error], ["RuntimeError", "ValueError"] | ||
) | ||
# go and check __traceback__, __cause__ attributes | ||
try: | ||
raise_error_from_another(group, RuntimeError("Cause")) | ||
except BaseException as e: | ||
new_group = e | ||
|
||
matched, unmatched = split(RuntimeError, group) | ||
assert matched.__traceback__ is new_group.__traceback__ | ||
assert matched.__cause__ is new_group.__cause__ | ||
assert matched.__context__ is new_group.__context__ | ||
assert unmatched.__traceback__ is new_group.__traceback__ | ||
assert unmatched.__cause__ is new_group.__cause__ | ||
assert unmatched.__context__ is new_group.__context__ |
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
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.