From 3cc1f47a40d3622b03496b9011fb4263705a8c80 Mon Sep 17 00:00:00 2001 From: check69 Date: Wed, 11 May 2022 22:07:16 +0000 Subject: [PATCH] deploy: cc92f8618b33ef875d87acc724d166298cd47442 --- prelude.html | 248 +++++++++++++++++++++++++++- tests/option/test_option_empty.html | 36 +++- tests/option/test_option_some.html | 54 +++++- tests/result/test_result_err.html | 62 ++++++- tests/result/test_result_ok.html | 130 ++++++++++++++- 5 files changed, 514 insertions(+), 16 deletions(-) diff --git a/prelude.html b/prelude.html index 7ab2aa5..5fd909d 100644 --- a/prelude.html +++ b/prelude.html @@ -28,7 +28,7 @@

Module rusty_results.prelude

from abc import abstractmethod
 from dataclasses import dataclass
-from typing import TypeVar, Union, Callable, Generic, Iterator, Tuple, Dict, Any
+from typing import cast, TypeVar, Union, Callable, Generic, Iterator, Tuple, Dict, Any
 from rusty_results.exceptions import UnwrapException
 try:
     from pydantic.fields import ModelField
@@ -268,6 +268,16 @@ 

Module rusty_results.prelude

""" ... # pragma: no cover + @abstractmethod + def transpose(self) -> "Result[Option[T], E]": + """ + Transposes an Option of a Result into a Result of an Option. + Empty will be mapped to Ok(Empty). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_). + :return: `Result[Option[T], E]` + :raises TypeError if inner value is not a `Result` + """ + ... # pragma: no cover + @abstractmethod def __bool__(self) -> bool: ... # pragma: no cover @@ -406,7 +416,7 @@

Module rusty_results.prelude

if other.is_some: # function typing is correct, we really return an Option[Tuple] but mypy complains that # other may not have a Value attribute because it do not understand the previous line check. - return Some((self.Some, other.Some)) # type: ignore + return Some((self.Some, other.Some)) # type: ignore[union-attr] return Empty() @@ -422,7 +432,7 @@

Module rusty_results.prelude

def flatten_one(self) -> "Option[T]": inner: T = self.unwrap() if isinstance(inner, OptionProtocol): - return inner # type: ignore[return-value] + return cast(Option, inner) return self def flatten(self) -> "Option[T]": @@ -432,6 +442,12 @@

Module rusty_results.prelude

this, inner = (inner, inner.flatten_one()) return this + def transpose(self) -> "Result[Option[T], E]": + if not isinstance(self.Some, ResultProtocol): + raise TypeError("Inner value is not a Result") + value: "ResultProtocol[T, E]" = self.Some + return value.map(Some) + def __bool__(self) -> bool: return True @@ -513,6 +529,9 @@

Module rusty_results.prelude

def flatten(self) -> "Option[T]": return self + def transpose(self) -> "Result[Option[T], E]": + return Ok(self) + def __bool__(self) -> bool: return False @@ -709,6 +728,32 @@

Module rusty_results.prelude

""" ... # pragma: no cover + @abstractmethod + def flatten_one(self) -> "Result[T, E]": + """ + Converts from Result[Result[T, E], E] to Result<T, E>, one nested level. + :return: Flattened Result[T, E] + """ + ... # pragma: no cover + + @abstractmethod + def flatten(self) -> "Result[T, E]": + """ + Converts from Result[Result[T, E], E] to Result<T, E>, any nested level + :return: Flattened Result[T, E] + """ + ... # pragma: no cover + + @abstractmethod + def transpose(self) -> Option["Result[T, E]"]: + """ + Transposes a Result of an Option into an Option of a Result. + Ok(Empty) will be mapped to Empty. Ok(Some(_)) and Err(_) will be mapped to Some(Ok(_)) and Some(Err(_)) + :return: Option[Result[T, E]] as per the mapping above + :raises TypeError if inner value is not an `Option` + """ + ... # pragma: no cover + @abstractmethod def __bool__(self) -> bool: ... # pragma: no cover @@ -864,6 +909,23 @@

Module rusty_results.prelude

def expect_err(self, msg: str) -> E: raise UnwrapException(msg) + def flatten_one(self) -> "Result[T, E]": + if isinstance(self.Ok, ResultProtocol): + return cast(Result, self.unwrap()) + return self + + def flatten(self) -> "Result[T, E]": + this: Result[T, E] = self + inner: Result[T, E] = self.flatten_one() + while inner is not this: + this, inner = (inner, inner.flatten_one()) + return this + + def transpose(self) -> Option["Result[T, E]"]: + if not isinstance(self.Ok, OptionProtocol): + raise TypeError("Inner value is not of type Option") + return cast(Option, self.unwrap()).map(Ok) + def __repr__(self): return f"Ok({self.Ok})" @@ -940,6 +1002,15 @@

Module rusty_results.prelude

def expect_err(self, msg: str) -> E: return self.Error + def flatten_one(self) -> "Result[T, E]": + return self + + def flatten(self) -> "Result[T, E]": + return self + + def transpose(self) -> Option["Result[T, E]"]: + return Some(self) + def __repr__(self): return f"Err({self.Error})" @@ -1045,6 +1116,9 @@

Classes

def flatten(self) -> "Option[T]": return self + def transpose(self) -> "Result[Option[T], E]": + return Ok(self) + def __bool__(self) -> bool: return False @@ -1077,6 +1151,7 @@

Inherited members

  • ok_or
  • ok_or_else
  • or_else
  • +
  • transpose
  • unwrap
  • unwrap_empty
  • unwrap_or
  • @@ -1163,6 +1238,15 @@

    Inherited members

    def expect_err(self, msg: str) -> E: return self.Error + def flatten_one(self) -> "Result[T, E]": + return self + + def flatten(self) -> "Result[T, E]": + return self + + def transpose(self) -> Option["Result[T, E]"]: + return Some(self) + def __repr__(self): return f"Err({self.Error})" @@ -1195,6 +1279,8 @@

    Inherited members

  • err
  • expect
  • expect_err
  • +
  • flatten
  • +
  • flatten_one
  • is_err
  • is_ok
  • iter
  • @@ -1204,6 +1290,7 @@

    Inherited members

  • map_or_else
  • ok
  • or_else
  • +
  • transpose
  • unwrap
  • unwrap_err
  • unwrap_or
  • @@ -1291,6 +1378,23 @@

    Inherited members

    def expect_err(self, msg: str) -> E: raise UnwrapException(msg) + def flatten_one(self) -> "Result[T, E]": + if isinstance(self.Ok, ResultProtocol): + return cast(Result, self.unwrap()) + return self + + def flatten(self) -> "Result[T, E]": + this: Result[T, E] = self + inner: Result[T, E] = self.flatten_one() + while inner is not this: + this, inner = (inner, inner.flatten_one()) + return this + + def transpose(self) -> Option["Result[T, E]"]: + if not isinstance(self.Ok, OptionProtocol): + raise TypeError("Inner value is not of type Option") + return cast(Option, self.unwrap()).map(Ok) + def __repr__(self): return f"Ok({self.Ok})" @@ -1323,6 +1427,8 @@

    Inherited members

  • err
  • expect
  • expect_err
  • +
  • flatten
  • +
  • flatten_one
  • is_err
  • is_ok
  • iter
  • @@ -1332,6 +1438,7 @@

    Inherited members

  • map_or_else
  • ok
  • or_else
  • +
  • transpose
  • unwrap
  • unwrap_err
  • unwrap_or
  • @@ -1586,6 +1693,16 @@

    Inherited members

    """ ... # pragma: no cover + @abstractmethod + def transpose(self) -> "Result[Option[T], E]": + """ + Transposes an Option of a Result into a Result of an Option. + Empty will be mapped to Ok(Empty). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_). + :return: `Result[Option[T], E]` + :raises TypeError if inner value is not a `Result` + """ + ... # pragma: no cover + @abstractmethod def __bool__(self) -> bool: ... # pragma: no cover @@ -2017,6 +2134,29 @@

    Methods

    ... # pragma: no cover
    +
    +def transpose(self) ‑> Union[Ok[Union[Some[~T], Empty], ~E], Err[Union[Some[~T], Empty], ~E]] +
    +
    +

    Transposes an Option of a Result into a Result of an Option. +Empty will be mapped to Ok(Empty). Some(Ok()) and Some(Err()) will be mapped to Ok(Some()) and Err(). +:return: Result[Option[T], E] +:raises TypeError if inner value is not a Result

    +
    + +Expand source code + +
    @abstractmethod
    +def transpose(self) -> "Result[Option[T], E]":
    +    """
    +    Transposes an Option of a Result into a Result of an Option.
    +    Empty will be mapped to Ok(Empty). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_).
    +    :return: `Result[Option[T], E]`
    +    :raises TypeError if inner value is not a `Result`
    +    """
    +    ... # pragma: no cover
    +
    +
    def unwrap(self) ‑> ~T
    @@ -2381,6 +2521,32 @@

    Methods

    """ ... # pragma: no cover + @abstractmethod + def flatten_one(self) -> "Result[T, E]": + """ + Converts from Result[Result[T, E], E] to Result<T, E>, one nested level. + :return: Flattened Result[T, E] + """ + ... # pragma: no cover + + @abstractmethod + def flatten(self) -> "Result[T, E]": + """ + Converts from Result[Result[T, E], E] to Result<T, E>, any nested level + :return: Flattened Result[T, E] + """ + ... # pragma: no cover + + @abstractmethod + def transpose(self) -> Option["Result[T, E]"]: + """ + Transposes a Result of an Option into an Option of a Result. + Ok(Empty) will be mapped to Empty. Ok(Some(_)) and Err(_) will be mapped to Some(Ok(_)) and Some(Err(_)) + :return: Option[Result[T, E]] as per the mapping above + :raises TypeError if inner value is not an `Option` + """ + ... # pragma: no cover + @abstractmethod def __bool__(self) -> bool: ... # pragma: no cover @@ -2635,6 +2801,44 @@

    Methods

    ... # pragma: no cover +
    +def flatten(self) ‑> Union[Ok[~T, ~E], Err[~T, ~E]] +
    +
    +

    Converts from Result[Result[T, E], E] to Result, any nested level +:return: Flattened Result[T, E]

    +
    + +Expand source code + +
    @abstractmethod
    +def flatten(self) -> "Result[T, E]":
    +    """
    +    Converts from Result[Result[T, E], E] to Result<T, E>, any nested level
    +    :return: Flattened Result[T, E]
    +    """
    +    ... # pragma: no cover
    +
    +
    +
    +def flatten_one(self) ‑> Union[Ok[~T, ~E], Err[~T, ~E]] +
    +
    +

    Converts from Result[Result[T, E], E] to Result, one nested level. +:return: Flattened Result[T, E]

    +
    + +Expand source code + +
    @abstractmethod
    +def flatten_one(self) -> "Result[T, E]":
    +    """
    +    Converts from Result[Result[T, E], E] to Result<T, E>, one nested level.
    +    :return: Flattened Result[T, E]
    +    """
    +    ... # pragma: no cover
    +
    +
    def iter(self) ‑> Iterator[~T]
    @@ -2801,6 +3005,29 @@

    Methods

    ... # pragma: no cover +
    +def transpose(self) ‑> Union[Some[Union[Ok[~T, ~E], Err[~T, ~E]]], Empty] +
    +
    +

    Transposes a Result of an Option into an Option of a Result. +Ok(Empty) will be mapped to Empty. Ok(Some()) and Err() will be mapped to Some(Ok()) and Some(Err()) +:return: Option[Result[T, E]] as per the mapping above +:raises TypeError if inner value is not an Option

    +
    + +Expand source code + +
    @abstractmethod
    +def transpose(self) -> Option["Result[T, E]"]:
    +    """
    +    Transposes a Result of an Option into an Option of a Result.
    +    Ok(Empty) will be mapped to Empty. Ok(Some(_)) and Err(_) will be mapped to Some(Ok(_)) and Some(Err(_))
    +    :return: Option[Result[T, E]] as per the mapping above
    +    :raises TypeError if inner value is not an `Option`
    +    """
    +    ... # pragma: no cover
    +
    +
    def unwrap(self) ‑> ~T
    @@ -2966,7 +3193,7 @@

    Methods

    if other.is_some: # function typing is correct, we really return an Option[Tuple] but mypy complains that # other may not have a Value attribute because it do not understand the previous line check. - return Some((self.Some, other.Some)) # type: ignore + return Some((self.Some, other.Some)) # type: ignore[union-attr] return Empty() @@ -2982,7 +3209,7 @@

    Methods

    def flatten_one(self) -> "Option[T]": inner: T = self.unwrap() if isinstance(inner, OptionProtocol): - return inner # type: ignore[return-value] + return cast(Option, inner) return self def flatten(self) -> "Option[T]": @@ -2992,6 +3219,12 @@

    Methods

    this, inner = (inner, inner.flatten_one()) return this + def transpose(self) -> "Result[Option[T], E]": + if not isinstance(self.Some, ResultProtocol): + raise TypeError("Inner value is not a Result") + value: "ResultProtocol[T, E]" = self.Some + return value.map(Some) + def __bool__(self) -> bool: return True @@ -3031,6 +3264,7 @@

    Inherited members

  • ok_or
  • ok_or_else
  • or_else
  • +
  • transpose
  • unwrap
  • unwrap_empty
  • unwrap_or
  • @@ -3092,6 +3326,7 @@

    ok_or
  • ok_or_else
  • or_else
  • +
  • transpose
  • unwrap
  • unwrap_empty
  • unwrap_or
  • @@ -3110,6 +3345,8 @@

    err
  • expect
  • expect_err
  • +
  • flatten
  • +
  • flatten_one
  • is_err
  • is_ok
  • iter
  • @@ -3119,6 +3356,7 @@

    map_or_else
  • ok
  • or_else
  • +
  • transpose
  • unwrap
  • unwrap_err
  • unwrap_or
  • diff --git a/tests/option/test_option_empty.html b/tests/option/test_option_empty.html index 4c51e5d..081588d 100644 --- a/tests/option/test_option_empty.html +++ b/tests/option/test_option_empty.html @@ -120,14 +120,19 @@

    Module rusty_results.tests.option.test_option_empty + assert this.flatten() == this + + +def test_transpose(): + this: Empty = Empty() + assert this.transpose() == Ok(Empty())
    @@ -425,12 +430,26 @@

    Functions

    Expand source code
    def test_flatten():
    +    this: Empty = Empty()
    +    assert this.flatten() == this
    + + +
    +def test_flatten_one() +
    +
    +
    +
    + +Expand source code + +
    def test_flatten_one():
         this: Empty = Empty()
         assert this.flatten_one() == this
    -
    -def test_flatten_all() +
    +def test_transpose()
    @@ -438,9 +457,9 @@

    Functions

    Expand source code -
    def test_flatten_all():
    +
    def test_transpose():
         this: Empty = Empty()
    -    assert this.flatten() == this
    + assert this.transpose() == Ok(Empty())
    @@ -483,7 +502,8 @@

    Index

  • test_empty_zip
  • test_empty_zip_with
  • test_flatten
  • -
  • test_flatten_all
  • +
  • test_flatten_one
  • +
  • test_transpose
  • diff --git a/tests/option/test_option_some.html b/tests/option/test_option_some.html index 2748f57..a8ec189 100644 --- a/tests/option/test_option_some.html +++ b/tests/option/test_option_some.html @@ -183,7 +183,23 @@

    Module rusty_results.tests.option.test_option_some + assert option.flatten() == expected_flatten + + +@pytest.mark.parametrize( + "option, expected_transpose", + [ + (Some(Ok(1)), Ok(Some(1))), + (Some(Err(2)), Err(2)), + ] +) +def test_transpose(option, expected_transpose): + assert option.transpose() == expected_transpose + + +def test_transpose_type_error(): + with pytest.raises(TypeError): + Some(10).transpose()

    @@ -571,6 +587,40 @@

    Functions

    assert some.zip_with(Some(5), lambda x: sum(x)) == Some(value + 5) +
    +def test_transpose(option, expected_transpose) +
    +
    +
    +
    + +Expand source code + +
    @pytest.mark.parametrize(
    +    "option, expected_transpose",
    +    [
    +        (Some(Ok(1)), Ok(Some(1))),
    +        (Some(Err(2)), Err(2)),
    +    ]
    +)
    +def test_transpose(option, expected_transpose):
    +    assert option.transpose() == expected_transpose
    +
    +
    +
    +def test_transpose_type_error() +
    +
    +
    +
    + +Expand source code + +
    def test_transpose_type_error():
    +    with pytest.raises(TypeError):
    +        Some(10).transpose()
    +
    +
    @@ -614,6 +664,8 @@

    Index

  • test_some_xor
  • test_some_zip
  • test_some_zip_with
  • +
  • test_transpose
  • +
  • test_transpose_type_error
  • diff --git a/tests/result/test_result_err.html b/tests/result/test_result_err.html index fefd39b..2694faf 100644 --- a/tests/result/test_result_err.html +++ b/tests/result/test_result_err.html @@ -160,7 +160,22 @@

    Module rusty_results.tests.result.test_result_err def test_err_expect_err(): err: Result[int, int] = Err(0) - assert err.expect_err("foo") == 0 + assert err.expect_err("foo") == 0 + + +def test_flatten_one(): + this: Result = Err(None) + assert this.flatten_one() == this + + +def test_flatten(): + this: Result = Err(None) + assert this.flatten() == this + + +def test_transpose(): + this: Result = Err(None) + assert this.transpose() == Some(Err(None))

    @@ -465,6 +480,48 @@

    Functions

    assert err.unwrap_or_else(lambda: 1) == 1 +
    +def test_flatten() +
    +
    +
    +
    + +Expand source code + +
    def test_flatten():
    +    this: Result = Err(None)
    +    assert this.flatten() == this
    +
    +
    +
    +def test_flatten_one() +
    +
    +
    +
    + +Expand source code + +
    def test_flatten_one():
    +    this: Result = Err(None)
    +    assert this.flatten_one() == this
    +
    +
    +
    +def test_transpose() +
    +
    +
    +
    + +Expand source code + +
    def test_transpose():
    +    this: Result = Err(None)
    +    assert this.transpose() == Some(Err(None))
    +
    +
    @@ -501,6 +558,9 @@

    Index

  • test_err_unwrap_err
  • test_err_unwrap_or
  • test_err_unwrap_or_else
  • +
  • test_flatten
  • +
  • test_flatten_one
  • +
  • test_transpose
  • diff --git a/tests/result/test_result_ok.html b/tests/result/test_result_ok.html index 8fe278b..3f06ef5 100644 --- a/tests/result/test_result_ok.html +++ b/tests/result/test_result_ok.html @@ -162,7 +162,51 @@

    Module rusty_results.tests.result.test_result_ok< ok: Result[int, int] = Ok(0) with pytest.raises(UnwrapException) as e: ok.expect_err(exception_msg) - assert str(e.value) == exception_msg + assert str(e.value) == exception_msg + + +@pytest.mark.parametrize( + "result, expected_flatten", + [ + (Ok(1), Ok(1)), + (Ok(Ok(2)), Ok(2)), + (Ok(Ok(Ok(3))), Ok(Ok(3))), + (Ok(Err(None)), Err(None)), + (Ok(Ok(Ok(Ok(Ok(Ok(Err(None))))))), Ok(Ok(Ok(Ok(Ok(Err(None))))))), + ] +) +def test_flatten_one(result: Result, expected_flatten: Result): + assert result.flatten_one() == expected_flatten + + +@pytest.mark.parametrize( + "result, expected_flatten", + [ + (Ok(1), Ok(1)), + (Ok(Ok(2)), Ok(2)), + (Ok(Ok(Ok(3))), Ok(3)), + (Ok(Err(None)), Err(None)), + (Ok(Ok(Ok(Ok(Ok(Ok(Err(None))))))), Err(None)), + ] +) +def test_flatten(result: Result, expected_flatten: Result): + assert result.flatten() == expected_flatten + + +@pytest.mark.parametrize( + "result, expected_transpose", + [ + (Ok(Some(1)), Some(Ok(1))), + (Ok(Empty()), Empty()), + ] +) +def test_transpose(result, expected_transpose): + assert result.transpose() == expected_transpose + + +def test_transpose_type_error(): + with pytest.raises(TypeError): + Ok(10).transpose()

    @@ -172,6 +216,52 @@

    Module rusty_results.tests.result.test_result_ok<

    Functions

    +
    +def test_flatten(result: Union[Ok[~T, ~E], Err[~T, ~E]], expected_flatten: Union[Ok[~T, ~E], Err[~T, ~E]]) +
    +
    +
    +
    + +Expand source code + +
    @pytest.mark.parametrize(
    +    "result, expected_flatten",
    +    [
    +        (Ok(1), Ok(1)),
    +        (Ok(Ok(2)), Ok(2)),
    +        (Ok(Ok(Ok(3))), Ok(3)),
    +        (Ok(Err(None)), Err(None)),
    +        (Ok(Ok(Ok(Ok(Ok(Ok(Err(None))))))), Err(None)),
    +    ]
    +)
    +def test_flatten(result: Result, expected_flatten: Result):
    +    assert result.flatten() == expected_flatten
    +
    +
    +
    +def test_flatten_one(result: Union[Ok[~T, ~E], Err[~T, ~E]], expected_flatten: Union[Ok[~T, ~E], Err[~T, ~E]]) +
    +
    +
    +
    + +Expand source code + +
    @pytest.mark.parametrize(
    +    "result, expected_flatten",
    +    [
    +        (Ok(1), Ok(1)),
    +        (Ok(Ok(2)), Ok(2)),
    +        (Ok(Ok(Ok(3))), Ok(Ok(3))),
    +        (Ok(Err(None)), Err(None)),
    +        (Ok(Ok(Ok(Ok(Ok(Ok(Err(None))))))), Ok(Ok(Ok(Ok(Ok(Err(None))))))),
    +    ]
    +)
    +def test_flatten_one(result: Result, expected_flatten: Result):
    +    assert result.flatten_one() == expected_flatten
    +
    +
    def test_ok_and_then()
    @@ -469,6 +559,40 @@

    Functions

    assert ok.unwrap_or_else(lambda: 1) == 0 +
    +def test_transpose(result, expected_transpose) +
    +
    +
    +
    + +Expand source code + +
    @pytest.mark.parametrize(
    +    "result, expected_transpose",
    +    [
    +        (Ok(Some(1)), Some(Ok(1))),
    +        (Ok(Empty()), Empty()),
    +    ]
    +)
    +def test_transpose(result, expected_transpose):
    +    assert result.transpose() == expected_transpose
    +
    +
    +
    +def test_transpose_type_error() +
    +
    +
    +
    + +Expand source code + +
    def test_transpose_type_error():
    +    with pytest.raises(TypeError):
    +        Ok(10).transpose()
    +
    +
    @@ -487,6 +611,8 @@

    Index

  • Functions