Coverage for src/monadc/either/left.py: 100%
88 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-19 20:24 -0700
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-19 20:24 -0700
1from typing import Callable, Union, Any, Optional, cast
2from .either import Either, L, R, U
5class Left(Either[L, R]):
6 """
7 Represents a failed computation in an Either monad.
9 Left values pass through transformations unchanged, allowing error information
10 to propagate through a chain of operations.
11 """
13 __match_args__ = ("_value",)
15 def __new__(cls, value: L) -> 'Left[L, R]':
16 """Create a new Left instance directly, bypassing Either.__new__."""
17 return object.__new__(cls)
19 def __init__(self, value: Any = None, **kwargs: Any) -> None:
20 # If _value already exists, this is a second call from Python's object creation process
21 # We should ignore it since the object has already been properly initialized
22 if hasattr(self, '_value'):
23 return
25 # Handle factory construction: Either(left=x) calls Left.__init__(left_obj, left=x)
26 if 'left' in kwargs:
27 self._value = kwargs['left']
28 # Handle direct construction: Left(value)
29 else:
30 self._value = value
32 def __eq__(self, other: object) -> bool:
33 if isinstance(other, Left):
34 return self._value == other._value # type: ignore[no-any-return]
35 return False
37 def __repr__(self) -> str:
38 return f"Left({self._value!r})"
40 def __str__(self) -> str:
41 return self.__repr__()
43 # ========================================
44 # Rust / Scala Common API
45 # ========================================
47 def is_left(self) -> bool:
48 return True
50 def is_right(self) -> bool:
51 return False
53 def left(self) -> Any: # Returns Option[L]
54 from ..option import Some
55 return Some(self._value)
57 def right(self) -> Any: # Returns Option[R]
58 from ..option import Nil
59 return Nil()
61 def map(self, func: Callable[[Union[L, R]], U]) -> U:
62 return func(self._value)
64 def map_left(self, func: Callable[[L], U]) -> Either[U, R]:
65 result = func(self._value)
66 return Left(result)
68 def map_right(self, func: Callable[[R], U]) -> Either[L, U]:
69 # Left passes through unchanged
70 return cast(Either[L, U], self)
72 # ========================================
73 # RUST API
74 # ========================================
76 def either(self, left_func: Callable[[L], U], right_func: Callable[[R], U]) -> U:
77 """Apply left_func to Left value or right_func to Right value. Alias for fold."""
78 return self.fold(left_func, right_func)
80 def map_either(self, left_func: Callable[[L], U], right_func: Callable[[R], U]) -> U:
81 return left_func(self._value)
83 def unwrap_left(self) -> L:
84 return self._value # type: ignore[no-any-return]
86 def unwrap_right(self) -> R:
87 raise ValueError("Cannot unwrap right value from Left")
89 def expect_left(self, message: str) -> L:
90 return self._value # type: ignore[no-any-return]
92 def expect_right(self, message: str) -> R:
93 raise ValueError(message)
95 def left_or(self, other: Either[U, R]) -> Either[U, R]:
96 # Return self (Left) rather than other
97 return cast(Either[U, R], self)
99 def right_or(self, other: Either[L, U]) -> Either[L, U]:
100 # Return other since self is Left
101 return other
103 def left_or_else(self, func: Callable[[], Either[U, R]]) -> Either[U, R]:
104 # Return self (Left) rather than calling func
105 return cast(Either[U, R], self)
107 def right_or_else(self, func: Callable[[], Either[L, U]]) -> Either[L, U]:
108 # Call func and return result since self is Left
109 return func()
111 def left_and_then(self, func: Callable[[L], Either[U, R]]) -> Either[U, R]:
112 return func(self._value)
114 def right_and_then(self, func: Callable[[R], Either[L, U]]) -> Either[L, U]:
115 # Left passes through unchanged
116 return cast(Either[L, U], self)
118 def flip(self) -> Either[R, L]:
119 from .right import Right
120 return Right(self._value)
122 # ========================================
123 # SCALA API
124 # ========================================
126 def swap(self) -> Either[R, L]:
127 from .right import Right
128 return Right(self._value)
130 def fold(self, if_left: Callable[[L], U], if_right: Callable[[R], U]) -> U:
131 return if_left(self._value)
133 def foreach(self, func: Callable[[R], Any]) -> None:
134 # Left does nothing - Scala style right-biased behavior
135 pass
137 def get(self) -> R:
138 raise ValueError("Cannot get Right value from Left")
140 def get_or_else(self, default: Union[R, Callable[[], R]]) -> R:
141 if callable(default):
142 return default()
143 return default
145 def to_option(self) -> Any: # Returns Option[R]
146 from ..option import Nil
147 return Nil()
149 def contains(self, value: Any) -> bool:
150 # Left never contains a Right value
151 return False
153 def exists(self, predicate: Callable[[R], bool]) -> bool:
154 # Left has no Right value to test predicate against
155 return False
157 def or_else(self, other: Union['Either[L, U]', Callable[[], 'Either[L, U]']]) -> 'Either[L, Union[R, U]]':
158 # Return other since this is a Left
159 if callable(other):
160 return other()
161 return other