mirror of
https://github.com/correl/typesafe-monads.git
synced 2024-11-14 11:09:36 +00:00
Fix circular imports in Result and Maybe
Result and Maybe include methods for converting themselves into each other. In order for the forward references to work correctly, the imports need to be module-only imports.
This commit is contained in:
parent
894e272d16
commit
7745f5ff6a
2 changed files with 10 additions and 16 deletions
|
@ -1,6 +1,7 @@
|
|||
from __future__ import annotations
|
||||
import functools
|
||||
from typing import Any, Callable, Generic, Iterable, List, Optional, TypeVar
|
||||
from . import result
|
||||
from .monad import Monad
|
||||
from .monoid import Monoid
|
||||
|
||||
|
@ -55,14 +56,14 @@ class Maybe(Monad[T]):
|
|||
return default
|
||||
|
||||
@classmethod
|
||||
def fromResult(cls, m: Result[T, E]) -> Maybe[T]:
|
||||
def fromResult(cls, m: result.Result[T, E]) -> Maybe[T]:
|
||||
return m.map(Maybe.pure).withDefault(Nothing())
|
||||
|
||||
def toResult(self, error: E) -> Result[T, E]:
|
||||
def toResult(self, error: E) -> result.Result[T, E]:
|
||||
if isinstance(self, Just):
|
||||
return Ok(self.value)
|
||||
return result.Ok(self.value)
|
||||
else:
|
||||
return Err(error)
|
||||
return result.Err(error)
|
||||
|
||||
@classmethod
|
||||
def fromList(self, xs: List[T]) -> Maybe[T]:
|
||||
|
@ -146,7 +147,3 @@ class Last(Monoid[Maybe[T]]):
|
|||
|
||||
def last(xs: List[Maybe[T]]) -> Maybe[T]:
|
||||
return Last.mconcat(map(lambda x: Last(x), xs)).value
|
||||
|
||||
|
||||
# Import Result last to avoid a circular import error
|
||||
from .result import Result, Ok, Err
|
||||
|
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
|||
import functools
|
||||
from typing import Any, Callable, Generic, Iterable, List, TypeVar
|
||||
|
||||
from . import maybe
|
||||
from .monad import Monad
|
||||
|
||||
T = TypeVar("T")
|
||||
|
@ -53,7 +54,7 @@ class Result(Monad[T], Generic[T, E]):
|
|||
raise TypeError
|
||||
|
||||
@classmethod
|
||||
def sequence(cls, xs: Iterable[Result[T, E]]) -> Maybe[List[T]]:
|
||||
def sequence(cls, xs: Iterable[Result[T, E]]) -> Result[List[T], E]:
|
||||
"""Evaluate monadic actions in sequence, collecting results."""
|
||||
|
||||
def mcons(acc: Result[List[T], E], x: Result[T, E]) -> Result[List[T], E]:
|
||||
|
@ -69,11 +70,11 @@ class Result(Monad[T], Generic[T, E]):
|
|||
return default
|
||||
|
||||
@classmethod
|
||||
def fromMaybe(cls, m: Maybe[T], error: E) -> Result[T, E]:
|
||||
def fromMaybe(cls, m: maybe.Maybe[T], error: E) -> Result[T, E]:
|
||||
return m.map(Result.pure).withDefault(Err(error))
|
||||
|
||||
def toMaybe(self) -> Maybe[T]:
|
||||
return self.map(Maybe.pure).withDefault(Nothing())
|
||||
def toMaybe(self) -> maybe.Maybe[T]:
|
||||
return self.map(maybe.Maybe.pure).withDefault(maybe.Nothing())
|
||||
|
||||
__rshift__ = bind
|
||||
__and__ = lambda other, self: Result.apply(self, other)
|
||||
|
@ -119,7 +120,3 @@ def safe(function: Callable[..., T]) -> Callable[..., Result[T, Exception]]:
|
|||
return Err(e)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
# Import Maybe last to avoid a circular import error
|
||||
from .maybe import Maybe, Nothing
|
||||
|
|
Loading…
Reference in a new issue