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
Typing issue with chained Result #124
Comments
Thanks for reporting. I will look into this. |
I think I found the source of this issue and a fix as well. Currently the return type for Result[_NewValueType ,_NewErrorType] it should be Result[_NewValueType, Union[_ErrorType ,_NewErrorType]] to account for the accumulation of the various failure cases. I tried this out on my machine for the examples from above and it seems to work just as intended; excuse me for being too lazy to author a proper pull request ;) |
@sobolevn, if you want to keep types sane and sound, |
Ok, so the plan is:
@astynax what about the value type? Thanks for your suggestions, guys! New release will be available before the 20th of August. |
I agree with @astynax that |
Another issue I just found is that the type of Success(0).bind(fraction).bind(log_of_arg_minus_one)
# reveal_type is 'Result[builtins.float*, Union[Any, builtins.ZeroDivisionError, builtins.ValueError*]]' |
@SaschaSchlemmer it can be |
Excuse me if this is bikeshedding, but I don't think it should be class NoType: pass |
Seems legit. Thanks! |
Another general issue with the typing of z: Result[float, Exception] = Success(1).bind(fraction).bind(log_of_arg_minus_one) As it currently is (regardless of the original or the proposed behavior) this doesn't typecheck. I think it should? |
Yes, I think so too. I will check either |
as far as I am aware mypy handles this just fine (when you set the |
@sobolevn scratch the z: Result[float, Union[ZeroDevisionError, ValueError] = Success(1).bind(fraction).bind(log_of_arg_minus_one) because the resulting Something curious happens with res: Result[float, Union[ZeroDivisionError, ValueError]] = Success(0).bind(fraction).bind(log_of_arg_minus_one)
reveal_type(res)
# Revealed type is
# 'returns.result.Result[builtins.float, Union[builtins.ZeroDivisionError, builtins.ValueError]]' vs reveal_type(Success(0).bind(fraction).bind(log_of_arg_minus_one))
# Revealed type is
# 'returns.result.Result[builtins.float*, Union[Any, builtins.ZeroDivisionError, builtins.ValueError*]]' |
According to the powers that be I am unfortunately still struggling a bit with setting up the git repo on my end but what I found out is:
|
@SaschaSchlemmer awesome work! Thanks a lot for your time and effort. What problems with |
I've actually joined the gitter chat, maybe that is a more suitable channel for my issues around the setup? |
@SaschaSchlemmer sorry, I don't use gitter. You can create a new github issue with your specific problem. And I will update the |
Sry for being silent the whole time; I was quite busy. I see from your commit that
So that means that the use case of chained |
I will consider adding |
I also had an interesting discussion on the typing/gitter channel about a dual function to |
@SaschaSchlemmer thanks a lot for sharing this! 👍 Are you interested in giving this a try? |
You mean the an implementation for |
@sobolevn I think a better name for the type aggregating |
I made some process with the plugin implementation for the proposed |
Take the following two functions as an example of two (or more) functions with unique failure modes (each function may fail in their own way)
then a computation like
fraction(x).bind(log_of_arg_minus_one)
has potentially three different outcomes:Success
, i.e.fraction(x)
returnsSuccess(y)
wherey
is afloat
andlog_of_arg_minus_one(y)
returnsSuccess(z)
wherez
is also a float; examplefraction(2).bind(log_of_arg_minus_one)
fraction
, i.e.Failure(ZeroDivisionError)
; example:fraction(0).bind(log_of_arg_minus_one)
log_of_arg_minus_one
, i.e.Failure(ValueError)
; example:fraction(1).bind(log_of_arg_minus_one)
Sadly this seems not to type correctly:
The following also doesn't work:
I don't know it this is an issue with
bind
(that might be fixable with a plugin) or just thatResult[S, F]
is not properly covariant with respect toF
i.e.Result[X, A]
is considered a subtype ofResult[X, B]
whenA
is a subtype ofB
The text was updated successfully, but these errors were encountered: