Skip to content
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

Bind composite errors #1975

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions returns/context/requires_context_ioresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,13 @@ def bind(
Kind3[
RequiresContextIOResult,
_NewValueType,
_ErrorType,
_ErrorType | _NewErrorType,
_EnvType,
],
],
) -> RequiresContextIOResult[_NewValueType, _ErrorType, _EnvType]:
) -> RequiresContextIOResult[
_NewValueType, _ErrorType | _NewErrorType, _EnvType,
]:
"""
Composes this container with a function returning the same type.

Expand Down Expand Up @@ -293,8 +295,12 @@ def bind(

def bind_result(
self,
function: Callable[[_ValueType], Result[_NewValueType, _ErrorType]],
) -> RequiresContextIOResult[_NewValueType, _ErrorType, _EnvType]:
function: Callable[
[_ValueType], Result[_NewValueType, _ErrorType | _NewErrorType],
],
) -> RequiresContextIOResult[
_NewValueType, _ErrorType | _NewErrorType, _EnvType,
]:
"""
Binds ``Result`` returning function to the current container.

Expand Down
50 changes: 35 additions & 15 deletions returns/context/requires_context_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def swap(self) -> RequiresContextResult[_ErrorType, _ValueType, _EnvType]:
return RequiresContextResult(lambda deps: self(deps).swap())

def map(
self, function: Callable[[_ValueType], _NewValueType],
self,
function: Callable[[_ValueType], _NewValueType],
) -> RequiresContextResult[_NewValueType, _ErrorType, _EnvType]:
"""
Composes successful container with a pure function.
Expand Down Expand Up @@ -232,11 +233,13 @@ def bind(
Kind3[
RequiresContextResult,
_NewValueType,
_ErrorType,
_ErrorType | _NewErrorType,
_EnvType,
],
],
) -> RequiresContextResult[_NewValueType, _ErrorType, _EnvType]:
) -> RequiresContextResult[
_NewValueType, _ErrorType | _NewErrorType, _EnvType,
]:
"""
Composes this container with a function returning the same type.

Expand Down Expand Up @@ -274,8 +277,12 @@ def bind(

def bind_result(
self,
function: Callable[[_ValueType], Result[_NewValueType, _ErrorType]],
) -> RequiresContextResult[_NewValueType, _ErrorType, _EnvType]:
function: Callable[
[_ValueType], Result[_NewValueType, _ErrorType | _NewErrorType],
],
) -> RequiresContextResult[
_NewValueType, _ErrorType | _NewErrorType, _EnvType,
]:
"""
Binds ``Result`` returning function to current container.

Expand Down Expand Up @@ -338,7 +345,8 @@ def bind_context(
)

def alt(
self, function: Callable[[_ErrorType], _NewErrorType],
self,
function: Callable[[_ErrorType], _NewErrorType],
) -> RequiresContextResult[_ValueType, _NewErrorType, _EnvType]:
"""
Composes failed container with a pure function.
Expand Down Expand Up @@ -455,7 +463,8 @@ def ask(cls) -> RequiresContextResult[_EnvType, _ErrorType, _EnvType]:

@classmethod
def from_result(
cls, inner_value: Result[_NewValueType, _NewErrorType],
cls,
inner_value: Result[_NewValueType, _NewErrorType],
) -> RequiresContextResult[_NewValueType, _NewErrorType, NoDeps]:
"""
Creates new container with ``Result`` as a unit value.
Expand All @@ -481,7 +490,8 @@ def from_result(
def from_typecast(
cls,
inner_value: RequiresContext[
Result[_NewValueType, _NewErrorType], _EnvType,
Result[_NewValueType, _NewErrorType],
_EnvType,
],
) -> RequiresContextResult[_NewValueType, _NewErrorType, _EnvType]:
"""
Expand Down Expand Up @@ -510,7 +520,8 @@ def from_typecast(

@classmethod
def from_context(
cls, inner_value: RequiresContext[_NewValueType, _NewEnvType],
cls,
inner_value: RequiresContext[_NewValueType, _NewEnvType],
) -> RequiresContextResult[_NewValueType, Any, _NewEnvType]:
"""
Creates new container from ``RequiresContext`` as a success unit.
Expand All @@ -528,7 +539,8 @@ def from_context(

@classmethod
def from_failed_context(
cls, inner_value: RequiresContext[_NewValueType, _NewEnvType],
cls,
inner_value: RequiresContext[_NewValueType, _NewEnvType],
) -> RequiresContextResult[Any, _NewValueType, _NewEnvType]:
"""
Creates new container from ``RequiresContext`` as a failure unit.
Expand All @@ -548,7 +560,9 @@ def from_failed_context(
def from_result_context(
cls,
inner_value: RequiresContextResult[
_NewValueType, _NewErrorType, _NewEnvType,
_NewValueType,
_NewErrorType,
_NewEnvType,
],
) -> RequiresContextResult[_NewValueType, _NewErrorType, _NewEnvType]:
"""
Expand All @@ -572,7 +586,8 @@ def from_result_context(

@classmethod
def from_value(
cls, inner_value: _FirstType,
cls,
inner_value: _FirstType,
) -> RequiresContextResult[_FirstType, Any, NoDeps]:
"""
Creates new container with ``Success(inner_value)`` as a unit value.
Expand All @@ -588,7 +603,8 @@ def from_value(

@classmethod
def from_failure(
cls, inner_value: _FirstType,
cls,
inner_value: _FirstType,
) -> RequiresContextResult[Any, _FirstType, NoDeps]:
"""
Creates new container with ``Failure(inner_value)`` as a unit value.
Expand All @@ -607,13 +623,17 @@ def from_failure(

#: Alias for a popular case when ``Result`` has ``Exception`` as error type.
RequiresContextResultE: TypeAlias = RequiresContextResult[
_ValueType, Exception, _EnvType,
_ValueType,
Exception,
_EnvType,
]

#: Alias to save you some typing. Uses original name from Haskell.
ReaderResult: TypeAlias = RequiresContextResult

#: Alias to save you some typing. Has ``Exception`` as error type.
ReaderResultE: TypeAlias = RequiresContextResult[
_ValueType, Exception, _EnvType,
_ValueType,
Exception,
_EnvType,
]
10 changes: 6 additions & 4 deletions returns/future.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,9 @@ def bind(
self,
function: Callable[
[_ValueType],
Kind2['FutureResult', _NewValueType, _ErrorType],
Kind2['FutureResult', _NewValueType, _ErrorType | _NewErrorType],
],
) -> 'FutureResult[_NewValueType, _ErrorType]':
) -> 'FutureResult[_NewValueType, _ErrorType | _NewErrorType]':
"""
Applies 'function' to the result of a previous calculation.

Expand Down Expand Up @@ -795,9 +795,11 @@ def bind_async(
self,
function: Callable[
[_ValueType],
Awaitable[Kind2['FutureResult', _NewValueType, _ErrorType]],
Awaitable[
Kind2['FutureResult', _NewValueType, _ErrorType | _NewErrorType]
],
],
) -> 'FutureResult[_NewValueType, _ErrorType]':
) -> 'FutureResult[_NewValueType, _ErrorType | _NewErrorType]':
"""
Composes a container and ``async`` function returning container.

Expand Down
8 changes: 4 additions & 4 deletions returns/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,9 @@ def bind(
self,
function: Callable[
[_ValueType],
Kind2['IOResult', _NewValueType, _ErrorType],
Kind2['IOResult', _NewValueType, _ErrorType | _NewErrorType],
],
) -> 'IOResult[_NewValueType, _ErrorType]':
) -> 'IOResult[_NewValueType, _ErrorType | _NewErrorType]':
"""
Composes successful container with a function that returns a container.

Expand All @@ -464,9 +464,9 @@ def bind_result(
self,
function: Callable[
[_ValueType],
Result[_NewValueType, _ErrorType],
Result[_NewValueType, _ErrorType | _NewErrorType],
],
) -> 'IOResult[_NewValueType, _ErrorType]':
) -> 'IOResult[_NewValueType, _ErrorType | _NewErrorType]':
"""
Composes successful container with a function that returns a container.

Expand Down
4 changes: 2 additions & 2 deletions returns/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def bind(
self,
function: Callable[
[_ValueType],
Kind2['Result', _NewValueType, _ErrorType],
Kind2['Result', _NewValueType, _ErrorType | _NewErrorType],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that it works with Exception subtypes, but it does not work if you have Result[int, int] and Result[int, str]

How would you compose these two objects? Result[int, int | str] might not make any sense.

Copy link
Author

@edodo1337 edodo1337 Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you compose these two objects? Result[int, int | str] might not make any sense.

Thank you for the review! I see your point, but isn’t int | str better than dealing with Any? At least with int | str, we can use pattern matching to handle each case explicitly.

],
) -> 'Result[_NewValueType, _ErrorType]':
) -> 'Result[_NewValueType, _ErrorType | _NewErrorType]':
"""
Composes successful container with a function that returns a container.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,19 @@

first: RequiresContextIOResult[float, bool, int]
reveal_type(first.modify_env(int)('1')) # N: Revealed type is "returns.io.IOResult[builtins.float, builtins.bool]"


- case: requires_context_ioresult_bind_composite_error
disable_cache: false
main: |
from returns.context import RequiresContextIOResult

x: RequiresContextIOResult[int, float, str]

def first_function(param: int) -> RequiresContextIOResult[bool, ValueError, str]:
...

def second_function(param: bool) -> RequiresContextIOResult[bool, KeyError, str]:
...

reveal_type(x.bind(first_function).bind(second_function)) # N: Revealed type is "returns.context.requires_context_ioresult.RequiresContextIOResult[builtins.bool, Union[builtins.float, builtins.ValueError, builtins.KeyError], builtins.str]"
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,19 @@

first: RequiresContextResult[float, bool, int]
reveal_type(first.modify_env(int)('1')) # N: Revealed type is "returns.result.Result[builtins.float, builtins.bool]"


- case: requires_context_result_bind_composite_error
disable_cache: false
main: |
from returns.context import RequiresContextResult

x: RequiresContextResult[int, float, str]

def first_function(param: int) -> RequiresContextResult[bool, ValueError, str]:
...

def second_function(param: bool) -> RequiresContextResult[bool, KeyError, str]:
...

reveal_type(x.bind(first_function).bind(second_function)) # N: Revealed type is "returns.context.requires_context_result.RequiresContextResult[builtins.bool, Union[builtins.float, builtins.ValueError, builtins.KeyError], builtins.str]"
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,59 @@
first: FutureResult[int, str]

reveal_type(first.lash(bind)) # N: Revealed type is "returns.future.FutureResult[builtins.int, builtins.float]"


- case: future_success_result_async_composite_error
disable_cache: false
main: |
from returns.future import FutureResult, FutureSuccess

def returns_value_error(value: int) -> FutureResult[str, ValueError]:
...
def returns_key_error(value: str) -> FutureResult[bool, KeyError]:
...

result = FutureSuccess(0).bind(returns_value_error).bind(returns_key_error)
reveal_type(result) # N: Revealed type is "returns.future.FutureResult[builtins.bool, Union[Any, builtins.ValueError, builtins.KeyError]]"


- case: future_failure_result_async_composite_error
disable_cache: false
main: |
from returns.future import FutureResult, FutureFailure

def returns_value_error(value: int) -> FutureResult[str, ValueError]:
...
def returns_key_error(value: str) -> FutureResult[bool, KeyError]:
...

result = FutureFailure(0).bind(returns_value_error).bind(returns_key_error)
reveal_type(result) # N: Revealed type is "returns.future.FutureResult[builtins.bool, Union[builtins.int, builtins.ValueError, builtins.KeyError]]"


- case: future_success_result_bind_async_composite_error
disable_cache: false
main: |
from returns.future import FutureResult, FutureSuccess

async def returns_value_error(value: int) -> FutureResult[str, ValueError]:
...
async def returns_key_error(value: str) -> FutureResult[bool, KeyError]:
...

result = FutureSuccess(0).bind_async(returns_value_error).bind_async(returns_key_error)
reveal_type(result) # N: Revealed type is "returns.future.FutureResult[builtins.bool, Union[Any, builtins.ValueError, builtins.KeyError]]"


- case: future_failure_result_bind_async_composite_error
disable_cache: false
main: |
from returns.future import FutureResult, FutureFailure

async def returns_value_error(value: int) -> FutureResult[str, ValueError]:
...
async def returns_key_error(value: str) -> FutureResult[bool, KeyError]:
...

result = FutureFailure(0).bind_async(returns_value_error).bind_async(returns_key_error)
reveal_type(result) # N: Revealed type is "returns.future.FutureResult[builtins.bool, Union[builtins.int, builtins.ValueError, builtins.KeyError]]"
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@
from returns.io import IOFailure

reveal_type(IOFailure(1).failure()) # N: Revealed type is "returns.io.IO[builtins.int]"


- case: iofailure_bind_composite_error
disable_cache: false
main: |
from returns.io import IOFailure, IOResult

def returns_value_error(value: int) -> IOResult[str, ValueError]:
...
def returns_key_error(value: str) -> IOResult[bool, KeyError]:
...

result = IOFailure(0).bind(returns_value_error).bind(returns_key_error)
reveal_type(result) # N: Revealed type is "returns.io.IOResult[builtins.bool, Union[builtins.int, builtins.ValueError, builtins.KeyError]]"
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,18 @@
from returns.io import IOSuccess

reveal_type(IOSuccess(1).unwrap()) # N: Revealed type is "returns.io.IO[builtins.int]"


- case: iosuccess_bind_composite_error
disable_cache: false
main: |
from returns.io import IOSuccess, IOResult

def returns_value_error(param: int) -> IOResult[str, ValueError]:
...
def returns_key_error(param: str) -> IOResult[str, KeyError]:
...

first: IOResult[int, Exception] = IOSuccess(1)
result = first.bind(returns_value_error).bind(returns_key_error)
reveal_type(result) # N: Revealed type is "returns.io.IOResult[builtins.str, Union[builtins.Exception, builtins.ValueError, builtins.KeyError]]"
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@

first: IOResult[int, Exception]
second = first.bind(test)
reveal_type(second) # N: Revealed type is "returns.io.IOResult[builtins.int, builtins.Exception]"
reveal_type(second) # N: Revealed type is "returns.io.IOResult[builtins.int, Union[builtins.Exception, builtins.ValueError]]"


- case: ioresult_correct_usage
Expand Down
Loading
Loading