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 2 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
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
15 changes: 15 additions & 0 deletions typesafety/test_result/test_construct_failure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@
from returns.result import Failure

reveal_type(Failure(1).failure()) # N: Revealed type is "builtins.int"


- case: failure_bind_composite_error
disable_cache: false
main: |
from returns.result import Success, Failure, Result

def first_function(value: int) -> Result[str, ValueError]:
...

def second_function(value: str) -> Result[bool, KeyError]:
...

result = Failure(0).bind(first_function).bind(second_function)
reveal_type(result) # N: Revealed type is "returns.result.Result[builtins.bool, Union[builtins.int, builtins.ValueError, builtins.KeyError]]"
15 changes: 15 additions & 0 deletions typesafety/test_result/test_construct_success.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@
from returns.result import Success

reveal_type(Success(1).unwrap()) # N: Revealed type is "builtins.int"


- case: success_bind_composite_error
disable_cache: false
main: |
from returns.result import Success, Result

def returns_value_error(param: int) -> Result[str, ValueError]:
...

def returns_key_error(param: str) -> Result[str, KeyError]:
...

first: Result[int, Exception] = Success(1)
reveal_type(first.bind(returns_value_error).bind(returns_key_error)) # N: Revealed type is "returns.result.Result[builtins.str, Union[builtins.Exception, builtins.ValueError, builtins.KeyError]]"
2 changes: 1 addition & 1 deletion typesafety/test_result/test_result_type_cast.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

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


- case: result_correct_usage
Expand Down
Loading