@@ -67,24 +67,14 @@ def bind(
6767
6868 def fix (
6969 self ,
70- function : Union [
71- # We use this union to make a good balance
72- # between correct and useful typing:
73- Callable [[None ], Optional [_NewValueType ]], # correct
74- Callable [[], Optional [_NewValueType ]], # useful
75- ],
70+ function : Callable [[None ], Optional [_NewValueType ]],
7671 ) -> 'Maybe[_NewValueType]' :
7772 """Abstract method to compose container with a pure function."""
7873 raise NotImplementedError
7974
8075 def rescue (
8176 self ,
82- function : Union [
83- # We use this union to make a good balance
84- # between correct and useful typing:
85- Callable [[None ], 'Maybe[_NewValueType]' ], # correct
86- Callable [[], 'Maybe[_NewValueType]' ], # useful
87- ],
77+ function : Callable [[None ], 'Maybe[_NewValueType]' ],
8878 ) -> 'Maybe[_NewValueType]' :
8979 """Abstract method to compose container with other container."""
9080 raise NotImplementedError
@@ -100,6 +90,10 @@ def unwrap(self) -> _ValueType:
10090 """Get value or raise exception."""
10191 raise NotImplementedError
10292
93+ def failure (self ) -> None :
94+ """Get failed value or raise exception."""
95+ raise NotImplementedError
96+
10397
10498@final
10599class _Nothing (Maybe [Any ]):
@@ -160,17 +154,14 @@ def fix(self, function):
160154
161155 .. code:: python
162156
163- >>> def fixable() -> str:
157+ >>> def fixable(_state ) -> str:
164158 ... return 'ab'
165159 ...
166160 >>> Nothing.fix(fixable) == Some('ab')
167161 True
168162
169163 """
170- try :
171- return Maybe .new (function ())
172- except TypeError :
173- return Maybe .new (function (self ._inner_value ))
164+ return Maybe .new (function (self ._inner_value ))
174165
175166 def rescue (self , function ):
176167 """
@@ -181,17 +172,14 @@ def rescue(self, function):
181172
182173 .. code:: python
183174
184- >>> def rescuable() -> Maybe[str]:
175+ >>> def rescuable(_state ) -> Maybe[str]:
185176 ... return Some('ab')
186177 ...
187178 >>> Nothing.rescue(rescuable) == Some('ab')
188179 True
189180
190181 """
191- try :
192- return function ()
193- except TypeError :
194- return function (self ._inner_value )
182+ return function (self ._inner_value )
195183
196184 def value_or (self , default_value ):
197185 """
@@ -219,6 +207,18 @@ def unwrap(self):
219207 """
220208 raise UnwrapFailedError (self )
221209
210+ def failure (self ) -> None :
211+ """
212+ Get failed value.
213+
214+ .. code:: python
215+
216+ >>> Nothing.failure() is None
217+ True
218+
219+ """
220+ return self ._inner_value
221+
222222
223223@final
224224class _Some (Maybe [_ValueType ]):
@@ -278,7 +278,7 @@ def fix(self, function):
278278
279279 .. code:: python
280280
281- >>> def fixable() -> str:
281+ >>> def fixable(_state ) -> str:
282282 ... return 'ab'
283283 ...
284284 >>> Some('a').fix(fixable) == Some('a')
@@ -293,7 +293,7 @@ def rescue(self, function):
293293
294294 .. code:: python
295295
296- >>> def rescuable() -> Maybe[str]:
296+ >>> def rescuable(_state ) -> Maybe[str]:
297297 ... return Some('ab')
298298 ...
299299 >>> Some('a').rescue(rescuable) == Some('a')
@@ -326,6 +326,20 @@ def unwrap(self):
326326 """
327327 return self ._inner_value
328328
329+ def failure (self ):
330+ """
331+ Raises an exception, since it does not have a failure inside.
332+
333+ .. code:: python
334+
335+ >>> Some(1).failure()
336+ Traceback (most recent call last):
337+ ...
338+ returns.primitives.exceptions.UnwrapFailedError
339+
340+ """
341+ raise UnwrapFailedError (self )
342+
329343
330344def Some (inner_value : Optional [_ValueType ]) -> Maybe [_ValueType ]: # noqa: N802
331345 """Public unit function of protected `_Some` type."""
0 commit comments