-
Notifications
You must be signed in to change notification settings - Fork 30
/
result.py
431 lines (329 loc) · 11.9 KB
/
result.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""Result module.
The Result[TSource,TError] type lets you write error-tolerant code that
can be composed. The Result type is typically used in monadic
error-handling, which is often referred to as Railway-oriented
Programming.
There is also a simplifyed alias of this type called `Maybe` that pins
the Result type to Exception.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import (
Any,
Callable,
Dict,
Generator,
Generic,
Iterable,
Iterator,
List,
TypeVar,
Union,
cast,
get_origin,
)
from typing_extensions import TypeGuard
from .curry import curry_flip
from .error import EffectError
from .pipe import PipeMixin
from .typing import GenericValidator, ModelField, SupportsValidation
_TSource = TypeVar("_TSource")
_TOther = TypeVar("_TOther")
_TResult = TypeVar("_TResult")
_TError = TypeVar("_TError")
def _validate(result: Any, field: ModelField) -> Result[Any, Any]:
if isinstance(result, Result):
return cast(Result[Any, Any], result)
if not isinstance(result, Dict):
raise ValueError("not result type")
try:
value: Any = result["ok"]
if field.sub_fields:
sub_field = field.sub_fields[0]
value, err = sub_field.validate(value, {}, loc="Result")
if err:
raise ValueError(str(err))
return Ok(value)
except KeyError:
try:
error: Any = result["error"]
sub_field = field.sub_fields[1]
error, err = sub_field.validate(error, {}, loc="Result")
if err:
raise ValueError(str(err))
return Error(error)
except KeyError:
raise ValueError("not a result")
class Result(
Iterable[_TSource],
PipeMixin,
SupportsValidation["Result[_TSource, _TError]"],
Generic[_TSource, _TError],
ABC,
):
"""The result abstract base class."""
__validators__: List[GenericValidator[Result[_TSource, _TError]]] = [_validate]
@abstractmethod
def default_value(self, value: _TSource) -> _TSource:
"""Get with default value.
Gets the value of the option if the option is Some, otherwise
returns the specified default value.
"""
raise NotImplementedError
@abstractmethod
def default_with(self, getter: Callable[[_TError], _TSource]) -> _TSource:
"""Get with default value lazily.
Gets the value of the option if the option is Some, otherwise
returns the value produced by the getter
"""
raise NotImplementedError
@abstractmethod
def map(self, mapper: Callable[[_TSource], _TResult]) -> Result[_TResult, _TError]:
raise NotImplementedError
@abstractmethod
def map2(
self,
other: Ok[_TOther, _TError] | Error[_TOther, _TError],
mapper: Callable[[_TSource, _TOther], _TResult],
) -> Result[_TResult, _TError]:
raise NotImplementedError
@abstractmethod
def map_error(
self, mapper: Callable[[_TError], _TResult]
) -> Result[_TSource, _TResult]:
"""Return a result of the error value after applying the mapping
function, or Ok if the input is Ok."""
raise NotImplementedError
@abstractmethod
def bind(
self, mapper: Callable[[_TSource], Result[_TResult, _TError]]
) -> Result[_TResult, _TError]:
raise NotImplementedError
@abstractmethod
def is_error(self) -> TypeGuard[Error[_TSource, _TError]]:
"""Returns `True` if the result is an `Error` value."""
raise NotImplementedError
@abstractmethod
def is_ok(self) -> TypeGuard[Ok[_TSource, _TError]]:
"""Returns `True` if the result is an `Ok` value."""
raise NotImplementedError
@abstractmethod
def dict(self) -> Dict[str, Union[_TSource, _TError]]:
"""Returns a json serializable representation of the result."""
raise NotImplementedError
def __eq__(self, o: Any) -> bool:
raise NotImplementedError
@abstractmethod
def __iter__(self) -> Iterator[_TSource]:
raise NotImplementedError
def __repr__(self) -> str:
return str(self)
@classmethod
def __get_validators__(
cls,
) -> Iterator[GenericValidator[Result[_TSource, _TError]]]:
yield from cls.__validators__
@abstractmethod
def __hash__(self) -> int:
raise NotImplementedError
class Ok(Result[_TSource, _TError]):
"""The Ok result case class."""
__match_args__ = ("value",)
def default_value(self, value: _TSource) -> _TSource:
"""Get with default value.
Gets the value of the option if the option is Some, otherwise
returns the specified default value.
"""
return self._value
def default_with(self, getter: Callable[[_TError], _TSource]) -> _TSource:
"""Get with default value lazily.
Gets the value of the option if the option is Some, otherwise
returns the value produced by the getter
"""
return self._value
def __init__(self, value: _TSource) -> None:
self._value = value
@property
def value(self) -> _TSource:
return self._value
def map(self, mapper: Callable[[_TSource], _TResult]) -> Result[_TResult, _TError]:
return Ok(mapper(self._value))
def map2(
self,
other: Ok[_TOther, _TError] | Error[_TOther, _TError],
mapper: Callable[[_TSource, _TOther], _TResult],
) -> Result[_TResult, _TError]:
# assert isinstance(other, (Ok, Error))
match other:
case Ok(value):
return Ok(mapper(self._value, value))
case Error(error):
return Error(error)
def bind(
self, mapper: Callable[[_TSource], Result[_TResult, _TError]]
) -> Result[_TResult, _TError]:
return mapper(self._value)
def map_error(
self, mapper: Callable[[_TError], _TResult]
) -> Result[_TSource, _TResult]:
"""Return a result of the error value after applying the mapping
function, or Ok if the input is Ok."""
return Ok(self._value)
def is_error(self) -> TypeGuard[Error[_TSource, _TError]]:
"""Returns `True` if the result is an `Ok` value."""
return False
def is_ok(self) -> TypeGuard[Ok[_TSource, _TError]]:
"""Returns `True` if the result is an `Ok` value."""
return True
def dict(self) -> Dict[str, _TSource | _TError]:
"""Returns a json string representation of the ok value."""
attr = getattr(self._value, "dict", None) or getattr(self._value, "dict", None)
if attr and callable(attr):
value = attr()
else:
value = self._value
return {"ok": value}
def __match__(self, pattern: Any) -> Iterable[_TSource]:
if self is pattern or self == pattern:
return [self.value]
try:
origin: Any = get_origin(pattern)
if isinstance(self, origin or pattern):
return [self.value]
except TypeError:
pass
return []
def __eq__(self, o: Any) -> bool:
if isinstance(o, Ok):
return self.value == o.value # type: ignore
return False
def __iter__(self) -> Generator[_TSource, _TSource, _TSource]:
"""Return iterator for Ok case."""
return (yield self._value)
def __str__(self):
return f"Ok {self._value}"
def __hash__(self) -> int:
return hash(self._value)
class ResultException(EffectError):
"""Makes the Error case a valid exception for effect handling. Do
not use directly."""
def __init__(self, message: str):
self.message = message
class Error(
ResultException,
Result[_TSource, _TError],
):
"""The Error result case class."""
__match_args__ = ("error",)
def __init__(self, error: _TError) -> None:
super().__init__(str(error))
self._error = error
def default_value(self, value: _TSource) -> _TSource:
"""Get with default value.
Gets the value of the option if the option is Some, otherwise
returns the specified default value.
"""
return value
def default_with(self, getter: Callable[[_TError], _TSource]) -> _TSource:
"""Get with default value lazily.
Gets the value of the option if the option is Some, otherwise
returns the value produced by the getter
"""
return getter(self._error)
@property
def error(self) -> _TError:
return self._error
def map(self, mapper: Callable[[_TSource], _TResult]) -> Result[_TResult, _TError]:
return Error(self._error)
def map2(
self,
other: Result[_TOther, _TError],
mapper: Callable[[_TSource, _TOther], _TResult],
) -> Result[_TResult, _TError]:
return Error(self._error)
def bind(
self, mapper: Callable[[_TSource], Result[_TResult, _TError]]
) -> Result[_TResult, _TError]:
return Error(self._error)
def map_error(
self, mapper: Callable[[_TError], _TResult]
) -> Result[_TSource, _TResult]:
"""Return a result of the error value after applying the mapping
function, or Ok if the input is Ok."""
return Error(mapper(self._error))
def is_error(self) -> TypeGuard[Error[_TSource, _TError]]:
"""Returns `True` if the result is an `Ok` value."""
return True
def is_ok(self) -> TypeGuard[Ok[_TSource, _TError]]:
"""Returns `True` if the result is an `Ok` value."""
return False
def dict(self) -> Dict[str, Any]:
"""Returns a json serializable representation of the error value."""
attr = getattr(self._error, "dict") or getattr(self._error, "dict")
if callable(attr):
error = attr()
else:
error = self._error
return {"error": error}
def __eq__(self, o: Any) -> bool:
if isinstance(o, Error):
return self.error == o.error # type: ignore
return False
def __iter__(self) -> Iterator[_TSource]:
"""Return iterator for Error case."""
# Raise class here so sub-classes like Failure works as well.
raise self.__class__(self._error)
# We're a generator
while False:
yield
def __str__(self):
return f"Error {self._error}"
def __hash__(self) -> int:
return hash(self._error)
def default_value(value: _TSource) -> Callable[[Result[_TSource, Any]], _TSource]:
"""Gets the value of the option if the option is Some, otherwise
returns the specified default value.
"""
def _default_value(result: Result[_TSource, Any]) -> _TSource:
return result.default_value(value)
return _default_value
def default_with(
getter: Callable[[_TError], _TSource]
) -> Callable[[Result[_TSource, _TError]], _TSource]:
"""Get with default value lazily.
Gets the value of the option if the option is Some, otherwise
returns the value produced by the getter
"""
def _default_with(result: Result[_TSource, _TError]) -> _TSource:
return result.default_with(getter)
return _default_with
@curry_flip(1)
def map(
result: Result[_TSource, _TError], mapper: Callable[[_TSource], _TResult]
) -> Result[_TResult, _TError]:
return result.map(mapper)
@curry_flip(2)
def map2(
x: Ok[_TSource, _TError] | Error[_TSource, _TError],
y: Ok[_TOther, _TError] | Error[_TOther, _TError],
mapper: Callable[[_TSource, _TOther], _TResult],
) -> Result[_TResult, _TError]:
return x.map2(y, mapper)
@curry_flip(1)
def bind(
result: Result[_TSource, _TError],
mapper: Callable[[_TSource], Result[_TResult, Any]],
) -> Result[_TResult, _TError]:
return result.bind(mapper)
def dict(source: Result[_TSource, _TError]) -> Dict[str, Union[_TSource, _TError]]:
return source.dict()
__all__ = [
"Result",
"Ok",
"Error",
"default_value",
"default_with",
"map",
"bind",
"dict",
]