-
Notifications
You must be signed in to change notification settings - Fork 3
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
Do notation style #21
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8fbeefc
Added early return implementation, easy to use do-style notation
danielSanchezQ 36c024b
Added early return tests
danielSanchezQ b5c3320
Use old style typing for compatibility
danielSanchezQ b162973
Add latest python3 11 and 12 for tests
danielSanchezQ 7d46044
Fix comments
danielSanchezQ b549aad
Add decorator test
danielSanchezQ ce8e164
Added missing pragmas no cover
danielSanchezQ 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 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 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,20 @@ | ||
from rusty_results import Option, Some, Empty | ||
from rusty_results import early_return | ||
|
||
|
||
@early_return | ||
def fail_on_operation() -> Option[int]: | ||
value1 = Some(10) | ||
value2 = Empty() | ||
return Some(~value1 + ~value2) | ||
|
||
|
||
def success_on_operation() -> Option[int]: | ||
value1 = Some(10) | ||
value2 = Some(10) | ||
return Some(~value1 + ~value2) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Success so it return value: ", success_on_operation()) | ||
print("Fail so it return Empty: ", fail_on_operation()) |
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
from .prelude import Option, Some, Empty, Result, Ok, Err | ||
from .exceptions import UnwrapException | ||
from .exceptions import UnwrapException, early_return |
This file contains 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 |
---|---|---|
@@ -1,2 +1,25 @@ | ||
from functools import wraps | ||
from typing import TypeVar | ||
|
||
|
||
class UnwrapException(Exception): | ||
... | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class EarlyReturnException(ValueError): | ||
def __init__(self, value: T): | ||
self.value = value | ||
super().__init__(self.value) | ||
|
||
|
||
def early_return(f): | ||
@wraps(f) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
f(*args, **kwargs) | ||
except EarlyReturnException as e: | ||
return e.value | ||
return wrapper |
This file contains 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
Empty file.
This file contains 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,11 @@ | ||
from rusty_results import early_return, Option, Some, Empty | ||
|
||
|
||
def test_early_return(): | ||
@early_return | ||
def __test_it() -> Option[str]: | ||
foo: Option = Empty() | ||
_ = ~foo | ||
return Some(10) # pragma: no cover | ||
|
||
assert __test_it() == Empty() |
This file contains 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 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 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 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.
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.
I would change the __test_it function to accept a parameter and just use the ~parameter and check if it is empty or Some(10) and we get the 100% coverage back again
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.
Just added the pragma to the line that never reaches.