|
14 | 14 | from abc import ABC, abstractmethod
|
15 | 15 | from collections.abc import Callable, Generator, Iterable, Iterator
|
16 | 16 | from typing import (
|
| 17 | + TYPE_CHECKING, |
17 | 18 | Any,
|
18 | 19 | ClassVar,
|
19 | 20 | Generic,
|
|
24 | 25 | get_origin,
|
25 | 26 | )
|
26 | 27 |
|
27 |
| -from . import option |
| 28 | + |
| 29 | +if TYPE_CHECKING: |
| 30 | + from expression.core.option import Option |
| 31 | + |
28 | 32 | from .curry import curry_flip
|
29 | 33 | from .error import EffectError
|
30 | 34 | from .pipe import PipeMixin
|
@@ -139,24 +143,24 @@ def dict(self) -> builtins.dict[str, _TSource | _TError]:
|
139 | 143 |
|
140 | 144 | @abstractmethod
|
141 | 145 | def swap(self) -> Result[_TError, _TResult]:
|
142 |
| - """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok""" |
| 146 | + """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok.""" |
143 | 147 | raise NotImplementedError
|
144 | 148 |
|
145 | 149 | @abstractmethod
|
146 |
| - def to_option(self) -> option.Option[_TSource]: |
| 150 | + def to_option(self) -> Option[_TSource]: |
147 | 151 | """Convert result to an option."""
|
148 | 152 | raise NotImplementedError
|
149 | 153 |
|
150 | 154 | @classmethod
|
151 | 155 | def of_option(
|
152 |
| - cls, value: option.Option[_TSource], error: _TError |
| 156 | + cls, value: Option[_TSource], error: _TError |
153 | 157 | ) -> Result[_TSource, _TError]:
|
154 | 158 | """Convert option to a result."""
|
155 | 159 | return of_option(value, error)
|
156 | 160 |
|
157 | 161 | @classmethod
|
158 | 162 | def of_option_with(
|
159 |
| - cls, value: option.Option[_TSource], error: Callable[[], _TError] |
| 163 | + cls, value: Option[_TSource], error: Callable[[], _TError] |
160 | 164 | ) -> Result[_TSource, _TError]:
|
161 | 165 | """Convert option to a result."""
|
162 | 166 | return of_option_with(value, error)
|
@@ -254,12 +258,14 @@ def dict(self) -> builtins.dict[str, _TSource | _TError]:
|
254 | 258 | return {"ok": value}
|
255 | 259 |
|
256 | 260 | def swap(self) -> Result[_TError, _TSource]:
|
257 |
| - """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok""" |
| 261 | + """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok.""" |
258 | 262 | return Error(self._value)
|
259 | 263 |
|
260 |
| - def to_option(self) -> option.Option[_TSource]: |
| 264 | + def to_option(self) -> Option[_TSource]: |
261 | 265 | """Convert result to an option."""
|
262 |
| - return option.Some(self._value) |
| 266 | + from expression.core.option import Some |
| 267 | + |
| 268 | + return Some(self._value) |
263 | 269 |
|
264 | 270 | def __match__(self, pattern: Any) -> Iterable[_TSource]:
|
265 | 271 | if self is pattern or self == pattern:
|
@@ -376,12 +382,14 @@ def dict(self) -> builtins.dict[str, Any]:
|
376 | 382 | return {"error": error}
|
377 | 383 |
|
378 | 384 | def swap(self) -> Result[_TError, _TSource]:
|
379 |
| - """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok""" |
| 385 | + """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok.""" |
380 | 386 | return Ok(self._error)
|
381 | 387 |
|
382 |
| - def to_option(self) -> option.Option[_TSource]: |
| 388 | + def to_option(self) -> Option[_TSource]: |
383 | 389 | """Convert result to an option."""
|
384 |
| - return option.Nothing |
| 390 | + from expression.core.option import Nothing |
| 391 | + |
| 392 | + return Nothing |
385 | 393 |
|
386 | 394 | def __eq__(self, o: Any) -> bool:
|
387 | 395 | if isinstance(o, Error):
|
@@ -471,26 +479,26 @@ def is_error(result: Result[_TSource, _TError]) -> TypeGuard[Error[_TSource, _TE
|
471 | 479 |
|
472 | 480 |
|
473 | 481 | def swap(result: Result[_TSource, _TError]) -> Result[_TError, _TSource]:
|
474 |
| - """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok""" |
| 482 | + """Swaps the value in the result so an Ok becomes an Error and an Error becomes an Ok.""" |
475 | 483 | return result.swap()
|
476 | 484 |
|
477 | 485 |
|
478 |
| -def to_option(result: Result[_TSource, _TError]) -> option.Option[_TSource]: |
| 486 | +def to_option(result: Result[_TSource, _TError]) -> Option[_TSource]: |
| 487 | + from expression.core.option import Nothing, Some |
| 488 | + |
479 | 489 | match result:
|
480 | 490 | case Ok(value):
|
481 |
| - return option.Some(value) |
| 491 | + return Some(value) |
482 | 492 | case _:
|
483 |
| - return option.Nothing |
| 493 | + return Nothing |
484 | 494 |
|
485 | 495 |
|
486 |
| -def of_option( |
487 |
| - value: option.Option[_TSource], error: _TError |
488 |
| -) -> Result[_TSource, _TError]: |
| 496 | +def of_option(value: Option[_TSource], error: _TError) -> Result[_TSource, _TError]: |
489 | 497 | return value.to_result(error)
|
490 | 498 |
|
491 | 499 |
|
492 | 500 | def of_option_with(
|
493 |
| - value: option.Option[_TSource], error: Callable[[], _TError] |
| 501 | + value: Option[_TSource], error: Callable[[], _TError] |
494 | 502 | ) -> Result[_TSource, _TError]:
|
495 | 503 | return value.to_result_with(error)
|
496 | 504 |
|
|
0 commit comments