-
-
Notifications
You must be signed in to change notification settings - Fork 131
Add the ability to add debugging notes to the result.safe decorator. #2110
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
Open
madkopp
wants to merge
10
commits into
dry-python:master
Choose a base branch
from
madkopp:logging-safe
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.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
74cab2d
Add notes to result.safe
wit00 aedc2d1
add notes to changelog.md
wit00 c7aaef9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 736264e
Update to address changes needed to complete pull request
wit00 1c12ca6
Merge branch 'logging-safe' of https://github.com/madkopp/returns int…
wit00 6e5dd8e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ebfd7c2
Merge branch 'dry-python:master' into logging-safe
madkopp 087140e
Reduced identation for clearer code
wit00 6790f1f
moved add_note_to_exception, fixed comments
wit00 31e3961
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
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,79 @@ | ||
from returns.future import future_safe | ||
from returns.pipeline import is_successful | ||
|
||
|
||
@future_safe((Exception,), add_note_on_failure=True) | ||
async def error_throwing_function() -> None: | ||
"""Raises an exception.""" | ||
raise ValueError('This is an error!') | ||
|
||
|
||
@future_safe((Exception,), add_note_on_failure='A custom message') | ||
async def error_throwing_function_with_message() -> None: | ||
"""Raises an exception.""" | ||
raise ValueError('This is an error!') | ||
|
||
|
||
@future_safe((Exception,), add_note_on_failure='') | ||
async def error_throwing_function_with_empty_str() -> None: | ||
"""Raises an exception.""" | ||
raise ValueError('This is an error!') | ||
|
||
|
||
@future_safe | ||
async def error_throwing_function_without_note() -> None: | ||
"""Raises an exception.""" | ||
raise ValueError('This is an error!') | ||
|
||
|
||
async def test_add_note_safe() -> None: | ||
"""Tests the add_note decorator with safe.""" | ||
result = await error_throwing_function() | ||
|
||
print(result) | ||
print(result.failure()._inner_value.__notes__) | ||
print(result.failure()) | ||
assert not is_successful(result) | ||
assert ( | ||
'Exception occurred in error_throwing_function' | ||
in result.failure()._inner_value.__notes__[0] | ||
) | ||
|
||
|
||
async def test_add_note_safe_with_message() -> None: | ||
"""Tests the add_note decorator with safe.""" | ||
result = await error_throwing_function_with_message() | ||
|
||
print(result) | ||
print(result.failure()._inner_value.__notes__) | ||
print(result.failure()) | ||
assert not is_successful(result) | ||
assert 'A custom message' in result.failure()._inner_value.__notes__ | ||
assert ( | ||
'Exception occurred in error_throwing_function_with_message' | ||
in result.failure()._inner_value.__notes__[1] | ||
) | ||
|
||
|
||
async def test_add_note_safe_with_empty_str() -> None: | ||
"""Tests the add_note decorator with safe.""" | ||
result = await error_throwing_function_with_empty_str() | ||
|
||
print(result) | ||
|
||
# Passing an empty string to add_note_on_failure should be treated as | ||
# passing False, so no note should be added | ||
assert not hasattr(result.failure()._inner_value, '__notes__') | ||
assert not is_successful(result) | ||
|
||
|
||
async def test_add_note_safe_without_note() -> None: | ||
"""Tests the vanilla functionality of the safe decortaor.""" | ||
result = await error_throwing_function_without_note() | ||
|
||
print(result) | ||
|
||
# Make sure that the add_note_on_failure functionality does not break the | ||
# vanilla functionality of the safe decorator | ||
assert not hasattr(result.failure()._inner_value, '__notes__') | ||
assert not is_successful(result) |
Oops, something went wrong.
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.