-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added early return implementation, easy to use do-style notation * Added early return tests * Use old style typing for compatibility * Add latest python3 11 and 12 for tests * Fix comments * Add decorator test * Added missing pragmas no cover
- Loading branch information
1 parent
fa8d8e2
commit fcace3a
Showing
11 changed files
with
132 additions
and
6 deletions.
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