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

Either.bind function doesn't support type hint as well #34

Open
fResult opened this issue Mar 14, 2024 · 0 comments
Open

Either.bind function doesn't support type hint as well #34

fResult opened this issue Mar 14, 2024 · 0 comments

Comments

@fResult
Copy link

fResult commented Mar 14, 2024

Either.bind function doesn't support type hint as well

Problem

The average functoion receives list[float], and returns Either[ErrorMessage, float].
image

So the avg_result type should be Either[ErrorMessage, float], not Ether[ErrorMessage, list[list[str]]].
image

But when I use bind function, it will return only the type of Either source.
image

Reproduce:

https://github.com/fResult/Just-Python/blob/fc32a08509741a5dff6a45d922175890e5a86478/worksheets/csv_use_either.py#L94-L100

Current implementation of the bind function

I found that the bind method signature is receive Callable[[S], 'Either[M, T]' which M, and T generic types are from the Either its class.
image

But I think it should be like this instead...

My Solution

I'm a new Python user, and I'm not an advanced functional programmer.
So, I'm not sure that my solution will break something, or not.
.
.
Let the M, and T generic types refer to generic M, and T of bind method itself.
image

-    def bind(
+    def bind[M, T](
             self: 'Either[M, S]', kleisli_function: Callable[[S], 'Either[M, T]']
     ) -> 'Either[M, T]':
         """ See Monad.bind """
         if self.is_left(): # pylint: disable=no-else-return
             return self
         else:
             return kleisli_function(self.value)

We will get the result of Either.bind like this.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant