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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
from abc import ABC
from collections.abc import Callable, Generator, Iterator
from functools import wraps
from typing import TYPE_CHECKING, Any, ClassVar, Optional, TypeVar, final
from typing_extensions import Never, ParamSpec
from returns.interfaces.specific.maybe import MaybeBased2
from returns.primitives.container import BaseContainer, container_equality
from returns.primitives.exceptions import UnwrapFailedError
from returns.primitives.hkt import Kind1, SupportsKind1
# Definitions:
_ValueType_co = TypeVar('_ValueType_co', covariant=True)
_NewValueType = TypeVar('_NewValueType')
_FuncParams = ParamSpec('_FuncParams')
class Maybe( # type: ignore[type-var]
BaseContainer,
SupportsKind1['Maybe', _ValueType_co],
MaybeBased2[_ValueType_co, None],
ABC,
):
"""
Represents a result of a series of computations that can return ``None``.
An alternative to using exceptions or constant ``is None`` checks.
``Maybe`` is an abstract type and should not be instantiated directly.
Instead use ``Some`` and ``Nothing``.
See also:
- https://github.com/gcanti/fp-ts/blob/master/docs/modules/Option.ts.md
"""
__slots__ = ()
_inner_value: _ValueType_co | None
__match_args__ = ('_inner_value',)
#: Alias for `Nothing`
empty: ClassVar['Maybe[Any]']
#: Typesafe equality comparison with other `Result` objects.
equals = container_equality
def map(
self,
function: Callable[[_ValueType_co], _NewValueType],
) -> 'Maybe[_NewValueType]':
"""
Composes successful container with a pure function.
.. code:: python
>>> from returns.maybe import Some, Nothing
>>> def mappable(string: str) -> str:
... return string + 'b'
>>> assert Some('a').map(mappable) == Some('ab')
>>> assert Nothing.map(mappable) == Nothing
"""
def apply(
self,
function: Kind1['Maybe', Callable[[_ValueType_co], _NewValueType]],
) -> 'Maybe[_NewValueType]':
"""
Calls a wrapped function in a container on this container.
.. code:: python
>>> from returns.maybe import Some, Nothing
>>> def appliable(string: str) -> str:
... return string + 'b'
>>> assert Some('a').apply(Some(appliable)) == Some('ab')
>>> assert Some('a').apply(Nothing) == Nothing
>>> assert Nothing.apply(Some(appliable)) == Nothing
>>> assert Nothing.apply(Nothing) == Nothing
"""
def bind(
self,
function: Callable[[_ValueType_co], Kind1['Maybe', _NewValueType]],
) -> 'Maybe[_NewValueType]':
"""
Composes successful container with a function that returns a container.
.. code:: python
>>> from returns.maybe import Nothing, Maybe, Some
>>> def bindable(string: str) -> Maybe[str]:
... return Some(string + 'b')
>>> assert Some('a').bind(bindable) == Some('ab')
>>> assert Nothing.bind(bindable) == Nothing
"""
def bind_optional(
self,
function: Callable[[_ValueType_co], _NewValueType | None],
) -> 'Maybe[_NewValueType]':
"""
Binds a function returning an optional value over a container.
.. code:: python
>>> from returns.maybe import Some, Nothing
>>> from typing import Optional
>>> def bindable(arg: str) -> Optional[int]:
... return len(arg) if arg else None
>>> assert Some('a').bind_optional(bindable) == Some(1)
>>> assert Some('').bind_optional(bindable) == Nothing
"""
def lash(
self,
function: Callable[[Any], Kind1['Maybe', _ValueType_co]],
) -> 'Maybe[_ValueType_co]':
"""
Composes failed container with a function that returns a container.
.. code:: python
>>> from returns.maybe import Maybe, Some, Nothing
>>> def lashable(arg=None) -> Maybe[str]:
... return Some('b')
>>> assert Some('a').lash(lashable) == Some('a')
>>> assert Nothing.lash(lashable) == Some('b')
We need this feature to make ``Maybe`` compatible
with different ``Result`` like operations.
"""
def __iter__(self) -> Iterator[_ValueType_co]:
"""API for :ref:`do-notation`."""
yield self.unwrap()
@classmethod
def do(
cls,
expr: Generator[_NewValueType, None, None],
) -> 'Maybe[_NewValueType]':
"""
Allows working with unwrapped values of containers in a safe way.
.. code:: python
>>> from returns.maybe import Maybe, Some, Nothing
>>> assert Maybe.do(
... first + second
... for first in Some(2)
... for second in Some(3)
... ) == Some(5)
>>> assert Maybe.do(
... first + second
... for first in Some(2)
... for second in Nothing
... ) == Nothing
See :ref:`do-notation` to learn more.
"""
try:
return Maybe.from_value(next(expr))
except UnwrapFailedError as exc:
return exc.halted_container # type: ignore
def value_or(
self,
default_value: _NewValueType,
) -> _ValueType_co | _NewValueType:
"""
Get value from successful container or default value from failed one.
.. code:: python
>>> from returns.maybe import Nothing, Some
>>> assert Some(0).value_or(1) == 0
>>> assert Nothing.value_or(1) == 1
"""
def or_else_call(
self,
function: Callable[[], _NewValueType],
) -> _ValueType_co | _NewValueType:
"""
Get value from successful container or default value from failed one.
Really close to :meth:`~Maybe.value_or` but works with lazy values.
This method is unique to ``Maybe`` container, because other containers
do have ``.alt`` method.
But, ``Maybe`` does not have this method.
There's nothing to ``alt`` in ``Nothing``.
Instead, it has this method to execute
some function if called on a failed container:
.. code:: pycon
>>> from returns.maybe import Some, Nothing
>>> assert Some(1).or_else_call(lambda: 2) == 1
>>> assert Nothing.or_else_call(lambda: 2) == 2
It might be useful to work with exceptions as well:
.. code:: pycon
>>> def fallback() -> Never:
... raise ValueError('Nothing!')
>>> Nothing.or_else_call(fallback)
Traceback (most recent call last):
...
ValueError: Nothing!
"""
def unwrap(self) -> _ValueType_co:
"""
Get value from successful container or raise exception for failed one.
.. code:: pycon
:force:
>>> from returns.maybe import Nothing, Some
>>> assert Some(1).unwrap() == 1
>>> Nothing.unwrap()
Traceback (most recent call last):
...
returns.primitives.exceptions.UnwrapFailedError
"""
def failure(self) -> None:
"""
Get failed value from failed container or raise exception from success.
.. code:: pycon
:force:
>>> from returns.maybe import Nothing, Some
>>> assert Nothing.failure() is None
>>> Some(1).failure()
Traceback (most recent call last):
...
returns.primitives.exceptions.UnwrapFailedError
"""
@classmethod
def from_value(
cls,
inner_value: _NewValueType,
) -> 'Maybe[_NewValueType]':
"""
Creates new instance of ``Maybe`` container based on a value.
.. code:: python
>>> from returns.maybe import Maybe, Some
>>> assert Maybe.from_value(1) == Some(1)
>>> assert Maybe.from_value(None) == Some(None)
"""
return Some(inner_value)
@classmethod
def from_optional(
cls,
inner_value: _NewValueType | None,
) -> 'Maybe[_NewValueType]':
"""
Creates new instance of ``Maybe`` container based on an optional value.
.. code:: python
>>> from returns.maybe import Maybe, Some, Nothing
>>> assert Maybe.from_optional(1) == Some(1)
>>> assert Maybe.from_optional(None) == Nothing
"""
if inner_value is None:
return _Nothing(inner_value)
return Some(inner_value)
def __bool__(self) -> bool:
"""Convert (or treat) an instance of ``Maybe`` as a boolean."""
@final
class _Nothing(Maybe[Any]):
"""Represents an empty state."""
__slots__ = ()
_inner_value: None
_instance: Optional['_Nothing'] = None
def __new__(cls, *args: Any, **kwargs: Any) -> '_Nothing':
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
def __init__(self, inner_value: None = None) -> None: # noqa: WPS632
"""
Private constructor for ``_Nothing`` type.
Use :attr:`~Nothing` instead.
Wraps the given value in the ``_Nothing`` container.
``inner_value`` can only be ``None``.
"""
super().__init__(None)
def __repr__(self):
"""
Custom ``str`` definition without the state inside.
.. code:: python
>>> from returns.maybe import Nothing
>>> assert str(Nothing) == '<Nothing>'
>>> assert repr(Nothing) == '<Nothing>'
"""
return '<Nothing>'
def map(self, function):
"""Does nothing for ``Nothing``."""
return self
def apply(self, container):
"""Does nothing for ``Nothing``."""
return self
def bind(self, function):
"""Does nothing for ``Nothing``."""
return self
def bind_optional(self, function):
"""Does nothing."""
return self
def lash(self, function):
"""Composes this container with a function returning container."""
return function(None)
def value_or(self, default_value):
"""Returns default value."""
return default_value
def or_else_call(self, function):
"""Returns the result of a passed function."""
return function()
def unwrap(self):
"""Raises an exception, since it does not have a value inside."""
raise UnwrapFailedError(self)
def failure(self) -> None:
"""Returns failed value."""
return self._inner_value
def __bool__(self):
"""Returns ``False``."""
return False
@final
class Some(Maybe[_ValueType_co]):
"""
Represents a calculation which has succeeded and contains the value.
Quite similar to ``Success`` type.
"""
__slots__ = ()
_inner_value: _ValueType_co
def __init__(self, inner_value: _ValueType_co) -> None:
"""Some constructor."""
super().__init__(inner_value)
if not TYPE_CHECKING: # noqa: WPS604 # pragma: no branch
def bind(self, function):
"""Binds current container to a function that returns container."""
return function(self._inner_value)
def bind_optional(self, function):
"""Binds a function returning an optional value over a container."""
return Maybe.from_optional(function(self._inner_value))
def unwrap(self):
"""Returns inner value for successful container."""
return self._inner_value
def map(self, function):
"""Composes current container with a pure function."""
return Some(function(self._inner_value))
def apply(self, container):
"""Calls a wrapped function in a container on this container."""
if isinstance(container, Some):
return self.map(container.unwrap()) # type: ignore
return container
def lash(self, function):
"""Does nothing for ``Some``."""
return self
def value_or(self, default_value):
"""Returns inner value for successful container."""
return self._inner_value
def or_else_call(self, function):
"""Returns inner value for successful container."""
return self._inner_value
def failure(self):
"""Raises exception for successful container."""
raise UnwrapFailedError(self)
def __bool__(self):
"""
Returns ``True```.
Any instance of ``Something`` is treated
as ``True``, even ``Something(None)``.
"""
return True
#: Public unit value of protected :class:`~_Nothing` type.
Nothing: Maybe[Never] = _Nothing()
Maybe.empty = Nothing
def maybe(
function: Callable[_FuncParams, _ValueType_co | None],
) -> Callable[_FuncParams, Maybe[_ValueType_co]]:
"""
Decorator to convert ``None``-returning function to ``Maybe`` container.
This decorator works with sync functions only. Example:
.. code:: python
>>> from typing import Optional
>>> from returns.maybe import Nothing, Some, maybe
>>> @maybe
... def might_be_none(arg: int) -> Optional[int]:
... if arg == 0:
... return None
... return 1 / arg
>>> assert might_be_none(0) == Nothing
>>> assert might_be_none(1) == Some(1.0)
"""
@wraps(function)
def decorator(
*args: _FuncParams.args,
**kwargs: _FuncParams.kwargs,
) -> Maybe[_ValueType_co]:
return Maybe.from_optional(function(*args, **kwargs))
return decorator
|