-
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
Changes from 6 commits
8fbeefc
36c024b
b5c3320
b162973
7d46044
b549aad
ce8e164
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()) |
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 |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from abc import abstractmethod | ||
from dataclasses import dataclass | ||
from typing import cast, TypeVar, Union, Callable, Generic, Iterator, Tuple, Dict, Any | ||
from rusty_results.exceptions import UnwrapException | ||
from rusty_results.exceptions import UnwrapException, EarlyReturnException | ||
|
||
try: | ||
from pydantic.fields import ModelField | ||
except ImportError: # pragma: no cover | ||
|
@@ -250,6 +251,15 @@ def transpose(self) -> "Result[Option[T], E]": | |
""" | ||
... # pragma: no cover | ||
|
||
@abstractmethod | ||
def early_return(self) -> T: | ||
""" | ||
Access hook for `early_return` wrapper style. | ||
:return: Self if self is Some(T) otherwise | ||
:raises: EarlyReturnException(Empty) | ||
""" | ||
... | ||
|
||
@abstractmethod | ||
def __bool__(self) -> bool: | ||
... # pragma: no cover | ||
|
@@ -260,6 +270,14 @@ def __contains__(self, item: T) -> bool: | |
def __iter__(self): | ||
return self.iter() | ||
|
||
def __invert__(self) -> T: | ||
""" | ||
Access hook for `early_return` wrapper style. | ||
:return: Self if self is Some(T) otherwise | ||
:raises: EarlyReturnException(Empty) | ||
""" | ||
return self.early_return() | ||
|
||
@classmethod | ||
def __get_validators__(cls): | ||
yield cls.__validate | ||
|
@@ -420,6 +438,10 @@ def transpose(self) -> "Result[Option[T], E]": | |
value: "ResultProtocol[T, E]" = self.Some | ||
return value.map(Some) | ||
|
||
def early_return(self) -> T: | ||
# it is safe to unwrap here as we know we are Some | ||
return self.unwrap() | ||
|
||
def __bool__(self) -> bool: | ||
return True | ||
|
||
|
@@ -504,6 +526,9 @@ def flatten(self) -> "Option[T]": | |
def transpose(self) -> "Result[Option[T], E]": | ||
return Ok(self) | ||
|
||
def early_return(self) -> T: | ||
raise EarlyReturnException(self) | ||
|
||
def __bool__(self) -> bool: | ||
return False | ||
|
||
|
@@ -726,6 +751,23 @@ def transpose(self) -> Option["Result[T, E]"]: | |
""" | ||
... # pragma: no cover | ||
|
||
@abstractmethod | ||
def early_return(self) -> T: | ||
""" | ||
Access hook for `early_return` wrapper style. | ||
:return: T if self is Ok(T) otherwise | ||
:raises: EarlyReturnException(Err(e)) | ||
""" | ||
... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add pragma no cover |
||
|
||
def __invert__(self) -> T: | ||
""" | ||
Access hook for `early_return` wrapper style. | ||
:return: T if self is Ok(T) otherwise | ||
:raises: EarlyReturnException(Err(e)) | ||
""" | ||
return self.early_return() | ||
|
||
@abstractmethod | ||
def __bool__(self) -> bool: | ||
... # pragma: no cover | ||
|
@@ -898,6 +940,10 @@ def transpose(self) -> Option["Result[T, E]"]: | |
raise TypeError("Inner value is not of type Option") | ||
return cast(Option, self.unwrap()).map(Ok) | ||
|
||
def early_return(self) -> T: | ||
# safe to unwrap here as we know it is Ok | ||
return self.unwrap() | ||
|
||
def __repr__(self): | ||
return f"Ok({self.Ok})" | ||
|
||
|
@@ -983,6 +1029,9 @@ def flatten(self) -> "Result[T, E]": | |
def transpose(self) -> Option["Result[T, E]"]: | ||
return Some(self) | ||
|
||
def early_return(self) -> T: | ||
raise EarlyReturnException(self) | ||
|
||
def __repr__(self): | ||
return f"Err({self.Error})" | ||
|
||
|
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]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Just added the pragma to the line that never reaches. |
||
foo: Option = Empty() | ||
_ = ~foo | ||
return Some(10) | ||
|
||
assert __test_it() == Empty() |
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.
add pragma no cover