9
9
from returns .primitives .monad import Monad , NewValueType , ValueType
10
10
from returns .primitives .types import MonadType
11
11
12
- ErrorType = TypeVar ('ErrorType ' )
12
+ _ErrorType = TypeVar ('_ErrorType ' )
13
13
14
14
15
15
# That's the most ugly part.
16
16
# We need to express `Either` with two type parameters and
17
17
# Left and Right with just one parameter.
18
18
# And that's how we do it. Any other and more cleaner ways are appreciated.
19
- class Either (Generic [ValueType , ErrorType ], metaclass = ABCMeta ):
19
+ class Either (Generic [ValueType , _ErrorType ], metaclass = ABCMeta ):
20
20
"""
21
21
Represents a calculation that may either fail or succeed.
22
22
@@ -26,7 +26,7 @@ class Either(Generic[ValueType, ErrorType], metaclass=ABCMeta):
26
26
and 'Left' (or its alias 'Failure').
27
27
"""
28
28
29
- _inner_value : Union [ValueType , ErrorType ]
29
+ _inner_value : Union [ValueType , _ErrorType ]
30
30
31
31
@abstractmethod
32
32
def unwrap (self ) -> ValueType : # pragma: no cover
@@ -40,33 +40,33 @@ def unwrap(self) -> ValueType: # pragma: no cover
40
40
41
41
42
42
@final
43
- class Left (Either [Any , ErrorType ], Monad [ErrorType ]):
43
+ class Left (Either [Any , _ErrorType ], Monad [_ErrorType ]):
44
44
"""
45
45
Represents a calculation which has failed.
46
46
47
47
It should contain an error code or message.
48
48
To help with readability you may alternatively use the alias 'Failure'.
49
49
"""
50
50
51
- def __init__ (self , inner_value : ErrorType ) -> None :
51
+ def __init__ (self , inner_value : _ErrorType ) -> None :
52
52
"""
53
53
Wraps the given value in the Container.
54
54
55
55
'value' is any arbitrary value of any type including functions.
56
56
"""
57
57
object .__setattr__ (self , '_inner_value' , inner_value )
58
58
59
- def fmap (self , function ) -> 'Left[ErrorType ]' :
59
+ def fmap (self , function ) -> 'Left[_ErrorType ]' :
60
60
"""Returns the 'Left' instance that was used to call the method."""
61
61
return self
62
62
63
- def bind (self , function ) -> 'Left[ErrorType ]' :
63
+ def bind (self , function ) -> 'Left[_ErrorType ]' :
64
64
"""Returns the 'Left' instance that was used to call the method."""
65
65
return self
66
66
67
67
def efmap (
68
68
self ,
69
- function : Callable [[ErrorType ], NewValueType ],
69
+ function : Callable [[_ErrorType ], NewValueType ],
70
70
) -> 'Right[NewValueType]' :
71
71
"""
72
72
Applies function to the inner value.
@@ -78,7 +78,7 @@ def efmap(
78
78
"""
79
79
return Right (function (self ._inner_value ))
80
80
81
- def ebind (self , function : Callable [[ErrorType ], MonadType ]) -> MonadType :
81
+ def ebind (self , function : Callable [[_ErrorType ], MonadType ]) -> MonadType :
82
82
"""
83
83
Applies 'function' to the result of a previous calculation.
84
84
@@ -95,7 +95,7 @@ def unwrap(self) -> NoReturn:
95
95
"""Raises an exception, since it does not have a value inside."""
96
96
raise UnwrapFailedError (self )
97
97
98
- def failure (self ) -> ErrorType :
98
+ def failure (self ) -> _ErrorType :
99
99
"""Unwraps inner error value from failed monad."""
100
100
return self ._inner_value
101
101
0 commit comments