From 77ff90ea7b0ff4fa288c8028d3a7827309ef4db5 Mon Sep 17 00:00:00 2001 From: danielSanchezQ Date: Mon, 1 Apr 2024 12:08:45 +0000 Subject: [PATCH] deploy: fcace3aeeab62c89e8c6ef71890f56946346ed0d --- exceptions.html | 79 ++++++++++- index.html | 2 +- prelude.html | 171 ++++++++++++++++++++++-- tests/exceptions/index.html | 65 +++++++++ tests/exceptions/test_early_return.html | 96 +++++++++++++ tests/index.html | 5 + tests/option/test_option_empty.html | 24 +++- tests/option/test_option_some.html | 22 ++- tests/result/test_result_err.html | 24 +++- tests/result/test_result_ok.html | 22 ++- 10 files changed, 490 insertions(+), 20 deletions(-) create mode 100644 tests/exceptions/index.html create mode 100644 tests/exceptions/test_early_return.html diff --git a/exceptions.html b/exceptions.html index 3969a41..17f485e 100644 --- a/exceptions.html +++ b/exceptions.html @@ -26,8 +26,31 @@

Module rusty_results.exceptions

Expand source code -
class UnwrapException(Exception):
-    ...
+
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
@@ -35,10 +58,54 @@

Module rusty_results.exceptions

+

Functions

+
+
+def early_return(f) +
+
+
+
+ +Expand source code + +
def early_return(f):
+    @wraps(f)
+    def wrapper(*args, **kwargs):
+        try:
+            f(*args, **kwargs)
+        except EarlyReturnException as e:
+            return e.value
+    return wrapper
+
+
+

Classes

+
+class EarlyReturnException +(value: ~T) +
+
+

Inappropriate argument value (of correct type).

+
+ +Expand source code + +
class EarlyReturnException(ValueError):
+    def __init__(self, value: T):
+        self.value = value
+        super().__init__(self.value)
+
+

Ancestors

+
    +
  • builtins.ValueError
  • +
  • builtins.Exception
  • +
  • builtins.BaseException
  • +
+
class UnwrapException (*args, **kwargs) @@ -72,9 +139,17 @@

Index

  • rusty_results
  • +
  • Functions

    + +
  • Classes

    diff --git a/index.html b/index.html index caa942e..a3ec432 100644 --- a/index.html +++ b/index.html @@ -27,7 +27,7 @@

    Package rusty_results

    Expand source code
    from .prelude import Option, Some, Empty, Result, Ok, Err
    -from .exceptions import UnwrapException
    +from .exceptions import UnwrapException, early_return
  • diff --git a/prelude.html b/prelude.html index 5fd909d..a4e1810 100644 --- a/prelude.html +++ b/prelude.html @@ -29,7 +29,8 @@

    Module rusty_results.prelude

    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
    @@ -276,7 +277,16 @@ 

    Module rusty_results.prelude

    :return: `Result[Option[T], E]` :raises TypeError if inner value is not a `Result` """ - ... # pragma: no cover + ... # 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) + """ + ... # pragma: no cover @abstractmethod def __bool__(self) -> bool: @@ -288,6 +298,14 @@

    Module rusty_results.prelude

    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 @@ -448,6 +466,10 @@

    Module rusty_results.prelude

    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 @@ -532,6 +554,9 @@

    Module rusty_results.prelude

    def transpose(self) -> "Result[Option[T], E]": return Ok(self) + def early_return(self) -> T: + raise EarlyReturnException(self) + def __bool__(self) -> bool: return False @@ -734,7 +759,7 @@

    Module rusty_results.prelude

    Converts from Result[Result[T, E], E] to Result<T, E>, one nested level. :return: Flattened Result[T, E] """ - ... # pragma: no cover + ... # pragma: no cover @abstractmethod def flatten(self) -> "Result[T, E]": @@ -742,7 +767,7 @@

    Module rusty_results.prelude

    Converts from Result[Result[T, E], E] to Result<T, E>, any nested level :return: Flattened Result[T, E] """ - ... # pragma: no cover + ... # pragma: no cover @abstractmethod def transpose(self) -> Option["Result[T, E]"]: @@ -752,7 +777,24 @@

    Module rusty_results.prelude

    :return: Option[Result[T, E]] as per the mapping above :raises TypeError if inner value is not an `Option` """ - ... # pragma: no cover + ... # 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)) + """ + ... # 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: @@ -926,6 +968,10 @@

    Module rusty_results.prelude

    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})" @@ -1011,6 +1057,9 @@

    Module rusty_results.prelude

    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})" @@ -1119,6 +1168,9 @@

    Classes

    def transpose(self) -> "Result[Option[T], E]": return Ok(self) + def early_return(self) -> T: + raise EarlyReturnException(self) + def __bool__(self) -> bool: return False @@ -1137,6 +1189,7 @@

    Inherited members

    • and_then
    • contains
    • +
    • early_return
    • expect_empty
    • expects
    • filter
    • @@ -1247,6 +1300,9 @@

      Inherited members

      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})" @@ -1276,6 +1332,7 @@

      Inherited members

    • and_then
    • contains
    • contains_err
    • +
    • early_return
    • err
    • expect
    • expect_err
    • @@ -1395,6 +1452,10 @@

      Inherited members

      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})" @@ -1424,6 +1485,7 @@

      Inherited members

    • and_then
    • contains
    • contains_err
    • +
    • early_return
    • err
    • expect
    • expect_err
    • @@ -1701,7 +1763,16 @@

      Inherited members

      :return: `Result[Option[T], E]` :raises TypeError if inner value is not a `Result` """ - ... # pragma: no cover + ... # 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) + """ + ... # pragma: no cover @abstractmethod def __bool__(self) -> bool: @@ -1713,6 +1784,14 @@

      Inherited members

      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 @@ -1864,6 +1943,27 @@

      Methods

      ... # pragma: no cover
    +
    +def early_return(self) ‑> ~T +
    +
    +

    Access hook for early_return wrapper style. +:return: Self if self is Some(T) otherwise +:raises: EarlyReturnException(Empty)

    +
    + +Expand source code + +
    @abstractmethod
    +def early_return(self) -> T:
    +    """
    +    Access hook for `early_return` wrapper style.
    +    :return: Self if self is Some(T) otherwise
    +    :raises: EarlyReturnException(Empty)
    +    """
    +    ...  # pragma: no cover
    +
    +
    def expect_empty(self, msg: str)
    @@ -2154,7 +2254,7 @@

    Methods

    :return: `Result[Option[T], E]` :raises TypeError if inner value is not a `Result` """ - ... # pragma: no cover + ... # pragma: no cover
    @@ -2527,7 +2627,7 @@

    Methods

    Converts from Result[Result[T, E], E] to Result<T, E>, one nested level. :return: Flattened Result[T, E] """ - ... # pragma: no cover + ... # pragma: no cover @abstractmethod def flatten(self) -> "Result[T, E]": @@ -2535,7 +2635,7 @@

    Methods

    Converts from Result[Result[T, E], E] to Result<T, E>, any nested level :return: Flattened Result[T, E] """ - ... # pragma: no cover + ... # pragma: no cover @abstractmethod def transpose(self) -> Option["Result[T, E]"]: @@ -2545,7 +2645,24 @@

    Methods

    :return: Option[Result[T, E]] as per the mapping above :raises TypeError if inner value is not an `Option` """ - ... # pragma: no cover + ... # 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)) + """ + ... # 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: @@ -2740,6 +2857,27 @@

    Methods

    ... # pragma: no cover
    +
    +def early_return(self) ‑> ~T +
    +
    +

    Access hook for early_return wrapper style. +:return: T if self is Ok(T) otherwise +:raises: EarlyReturnException(Err(e))

    +
    + +Expand source code + +
    @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))
    +    """
    +    ...  # pragma: no cover
    +
    +
    def err(self) ‑> Union[Some[~E], Empty]
    @@ -2817,7 +2955,7 @@

    Methods

    Converts from Result[Result[T, E], E] to Result<T, E>, any nested level :return: Flattened Result[T, E] """ - ... # pragma: no cover + ... # pragma: no cover
    @@ -2836,7 +2974,7 @@

    Methods

    Converts from Result[Result[T, E], E] to Result<T, E>, one nested level. :return: Flattened Result[T, E] """ - ... # pragma: no cover
    + ... # pragma: no cover
    @@ -3025,7 +3163,7 @@

    Methods

    :return: Option[Result[T, E]] as per the mapping above :raises TypeError if inner value is not an `Option` """ - ... # pragma: no cover
    + ... # pragma: no cover
    @@ -3225,6 +3363,10 @@

    Methods

    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 @@ -3250,6 +3392,7 @@

    Inherited members

    • and_then
    • contains
    • +
    • early_return
    • expect_empty
    • expects
    • filter
    • @@ -3312,6 +3455,7 @@

    • and_then
    • contains
    • +
    • early_return
    • expect_empty
    • expects
    • filter
    • @@ -3342,6 +3486,7 @@

      and_then
    • contains
    • contains_err
    • +
    • early_return
    • err
    • expect
    • expect_err
    • diff --git a/tests/exceptions/index.html b/tests/exceptions/index.html new file mode 100644 index 0000000..668be05 --- /dev/null +++ b/tests/exceptions/index.html @@ -0,0 +1,65 @@ + + + + + + +rusty_results.tests.exceptions API documentation + + + + + + + + + + + +
      + + +
      + + + \ No newline at end of file diff --git a/tests/exceptions/test_early_return.html b/tests/exceptions/test_early_return.html new file mode 100644 index 0000000..12b399d --- /dev/null +++ b/tests/exceptions/test_early_return.html @@ -0,0 +1,96 @@ + + + + + + +rusty_results.tests.exceptions.test_early_return API documentation + + + + + + + + + + + +
      +
      +
      +

      Module rusty_results.tests.exceptions.test_early_return

      +
      +
      +
      + +Expand source code + +
      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()
      +
      +
      +
      +
      +
      +
      +
      +

      Functions

      +
      +
      +def test_early_return() +
      +
      +
      +
      + +Expand source code + +
      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()
      +
      +
      +
      +
      +
      +
      +
      + +
      + + + \ No newline at end of file diff --git a/tests/index.html b/tests/index.html index aec8045..736e510 100644 --- a/tests/index.html +++ b/tests/index.html @@ -26,6 +26,10 @@

      Module rusty_results.tests

      Sub-modules

      +
      rusty_results.tests.exceptions
      +
      +
      +
      rusty_results.tests.option
      @@ -60,6 +64,7 @@

      Index

    • Sub-modules

        +
      • rusty_results.tests.exceptions
      • rusty_results.tests.option
      • rusty_results.tests.result
      • rusty_results.tests.serde
      • diff --git a/tests/option/test_option_empty.html b/tests/option/test_option_empty.html index 081588d..6edf37e 100644 --- a/tests/option/test_option_empty.html +++ b/tests/option/test_option_empty.html @@ -132,7 +132,13 @@

        Module rusty_results.tests.option.test_option_empty + assert this.transpose() == Ok(Empty()) + + +def test_early_return(): + with pytest.raises(EarlyReturnException): + this: Empty = Empty() + _ = ~this

    • @@ -142,6 +148,21 @@

      Module rusty_results.tests.option.test_option_empty

      Functions

      +
      +def test_early_return() +
      +
      +
      +
      + +Expand source code + +
      def test_early_return():
      +    with pytest.raises(EarlyReturnException):
      +        this: Empty = Empty()
      +        _ = ~this
      +
      +
      def test_empty_and_then()
      @@ -480,6 +501,7 @@

      Index

    • Functions

        +
      • test_early_return
      • test_empty_and_then
      • test_empty_contains
      • test_empty_expect_none
      • diff --git a/tests/option/test_option_some.html b/tests/option/test_option_some.html index a8ec189..1b00c70 100644 --- a/tests/option/test_option_some.html +++ b/tests/option/test_option_some.html @@ -199,7 +199,12 @@

        Module rusty_results.tests.option.test_option_some + Some(10).transpose() + + +def test_early_return(): + value = ~Some(10) + assert value == 10

    • @@ -222,6 +227,20 @@

      Functions

      return Some(value), value
      +
      +def test_early_return() +
      +
      +
      +
      + +Expand source code + +
      def test_early_return():
      +    value = ~Some(10)
      +    assert value == 10
      +
      +
      def test_flatten(option: Union[Some[~T], Empty], expected_flatten: Union[Some[~T], Empty])
      @@ -640,6 +659,7 @@

      Index

    • Functions

      • create_some
      • +
      • test_early_return
      • test_flatten
      • test_flatten_one
      • test_some_and_then
      • diff --git a/tests/result/test_result_err.html b/tests/result/test_result_err.html index 2694faf..5aa2919 100644 --- a/tests/result/test_result_err.html +++ b/tests/result/test_result_err.html @@ -175,7 +175,13 @@

        Module rusty_results.tests.result.test_result_err def test_transpose(): this: Result = Err(None) - assert this.transpose() == Some(Err(None)) + assert this.transpose() == Some(Err(None)) + + +def test_early_return(): + err: Result[int, int] = Err(0) + with pytest.raises(EarlyReturnException): + _ = ~err

    • @@ -185,6 +191,21 @@

      Module rusty_results.tests.result.test_result_err

      Functions

      +
      +def test_early_return() +
      +
      +
      +
      + +Expand source code + +
      def test_early_return():
      +    err: Result[int, int] = Err(0)
      +    with pytest.raises(EarlyReturnException):
      +        _ = ~err
      +
      +
      def test_err_and_then()
      @@ -540,6 +561,7 @@

      Index

    • Functions

        +
      • test_early_return
      • test_err_and_then
      • test_err_builds
      • test_err_contains
      • diff --git a/tests/result/test_result_ok.html b/tests/result/test_result_ok.html index 3f06ef5..d5c9d58 100644 --- a/tests/result/test_result_ok.html +++ b/tests/result/test_result_ok.html @@ -206,7 +206,12 @@

        Module rusty_results.tests.result.test_result_ok< def test_transpose_type_error(): with pytest.raises(TypeError): - Ok(10).transpose() + Ok(10).transpose() + + +def test_early_return(): + err: Result[int, int] = Ok(0) + assert ~err == 0

    • @@ -216,6 +221,20 @@

      Module rusty_results.tests.result.test_result_ok<

      Functions

      +
      +def test_early_return() +
      +
      +
      +
      + +Expand source code + +
      def test_early_return():
      +    err: Result[int, int] = Ok(0)
      +    assert ~err == 0
      +
      +
      def test_flatten(result: Union[Ok[~T, ~E], Err[~T, ~E]], expected_flatten: Union[Ok[~T, ~E], Err[~T, ~E]])
      @@ -611,6 +630,7 @@

      Index

    • Functions