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
|
Container: the concept
======================
.. currentmodule:: returns.primitives.container
Container is a concept that allows you
to write code around the existing wrapped values
while maintaining the execution context.
List of supported containers:
- :class:`Maybe <returns.maybe.Maybe>` to handle ``None`` cases
- :class:`Result <returns.result.Result>` to handle possible exceptions
- :class:`IO <returns.io.IO>` to mark explicit ``IO`` actions
- :class:`Future <returns.future.Future>` to work with ``async`` code
- :class:`RequiresContext <returns.context.requires_context.RequiresContext>`
to pass context to your functions (DI and similar)
There are also some combinations like
:class:`IOResult <returns.io.IOResult>`,
:class:`FutureResult <returns.future.FutureResult>`,
:class:`RequiresContextResult <.RequiresContextResult>`,
:class:`RequiresContextIOResult <.RequiresContextIOResult>` and
:class:`RequiresContextFutureResult <.RequiresContextFutureResult>`.
We will show you container's simple API of one attribute
and several simple methods.
Basics
------
The main idea behind a container is that it wraps some internal state.
That's what
:attr:`._inner_value <returns.primitives.container.Container._inner_value>`
is used for.
And we have several functions
to create new containers based on the previous state.
And we can see how this state is evolving during the execution.
.. mermaid::
:caption: State evolution.
graph LR
F1["Container(Initial)"] --> F2["Container(UserId(1))"]
F2 --> F3["Container(UserAccount(156))"]
F3 --> F4["Container(FailedLoginAttempt(1))"]
F4 --> F5["Container(SentNotificationId(992))"]
Working with a container
------------------------
We use two methods to create a new container from the previous one.
``bind`` and ``map``.
The difference is simple:
- ``map`` works with functions that return regular value
- ``bind`` works with functions that return new container of the same type
We have :func:`returns.interfaces.mappable.MappableN.map`
to compose containers with regular functions.
Here's how it looks:
.. mermaid::
:caption: Illustration of ``map`` method.
graph LR
F1["Container[A]"] -- "map(function)" --> F2["Container[B]"]
style F1 fill:green
style F2 fill:green
.. code:: python
>>> from typing import Any
>>> from returns.result import Success, Result
>>> def double(state: int) -> int:
... return state * 2
>>> result: Result[int, Any] = Success(1).map(double)
>>> assert str(result) == '<Success: 2>'
>>> result: Result[int, Any] = result.map(lambda state: state + 1)
>>> assert str(result) == '<Success: 3>'
The same works with built-in functions as well:
.. code:: python
>>> from returns.io import IO
>>> io = IO('bytes').map(list)
>>> str(io)
"<IO: ['b', 'y', 't', 'e', 's']>"
The second method is ``bind``. It is a bit different.
We pass a function that returns another container to it.
:func:`returns.interfaces.bindable.BindableN.bind`
is used to literally bind two different containers together.
Here's how it looks:
.. mermaid::
:caption: Illustration of ``bind`` method.
graph LR
F1["Container[A]"] -- "bind(function)" --> F2["Container[B]"]
F1["Container[A]"] -- "bind(function)" --> F3["Container[C]"]
style F1 fill:green
style F2 fill:green
style F3 fill:red
.. code:: python
from returns.result import Result, Success
def may_fail(user_id: int) -> Result[float, str]:
...
value: Result[int, str] = Success(1)
# Can be assumed as either Success[float] or Failure[str]:
result: Result[float, str] = value.bind(may_fail)
.. note::
All containers support these methods.
Because all containers implement
:class:`returns.interfaces.mappable.MappableN`
and
:class:`returns.interfaces.bindable.BindableN`.
You can read more about methods
that some other containers support
and :ref:`interfaces <base-interfaces>` behind them.
Instantiating a container
-------------------------
All :class:`returns.interfaces.applicative.ApplicativeN` containers
support special ``.from_value`` method
to construct a new container from a raw value.
.. code:: python
>>> from returns.result import Result
>>> assert str(Result.from_value(1)) == '<Success: 1>'
There are also other methods in other interfaces.
For example, here are some of them:
- :func:`returns.interfaces.specific.maybe.MaybeLikeN.from_optional`
creates a value from ``Optional`` value
.. code:: python
>>> from returns.maybe import Maybe, Some, Nothing
>>> assert Maybe.from_optional(1) == Some(1)
>>> assert Maybe.from_optional(None) == Nothing
- :func:`returns.interfaces.failable.DiverseFailableN.from_failure`
creates a failing container from a value
.. code:: python
>>> from returns.result import Result, Failure
>>> assert Result.from_failure(1) == Failure(1)
There are many other constructors!
Check out concrete types and their interfaces.
Working with multiple containers
--------------------------------
Multiple container arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We have already seen how we can work with one container and functions
that receive a single argument.
Let's say you have a function of two arguments and two containers:
.. code:: python
>>> def sum_two_numbers(first: int, second: int) -> int:
... return first + second
And here are our two containers:
.. code:: python
>>> from returns.io import IO
>>> one = IO(1)
>>> two = IO(2)
The naive approach to compose two ``IO`` containers and a function
would be too hard to show here.
Luckily, we support partial application and the ``.apply()`` method.
Here are the required steps:
0. We make ``sum_two_numbers`` to receive :ref:`partial arguments <curry>`
1. We create a new container that wraps ``sum_two_numbers`` function as a value
2. We then call ``.apply()`` twice to pass each value
It can be done like so:
.. code:: python
>>> from returns.curry import curry
>>> from returns.io import IO
>>> @curry
... def sum_two_numbers(first: int, second: int) -> int:
... return first + second
>>> one = IO(1)
>>> two = IO(2)
>>> assert two.apply(one.apply(IO(sum_two_numbers))) == IO(3)
But, there are other ways to make ``sum_two_numbers`` partial.
One can use ``partial`` as well:
.. code:: python
>>> from returns.curry import partial
>>> one = IO(1)
>>> two = IO(2)
>>> assert two.apply(one.apply(
... IO(lambda x: partial(sum_two_numbers, x)),
... )) == IO(3)
Or even native ``lambda`` functions:
.. code:: python
>>> one = IO(1)
>>> two = IO(2)
>>> assert two.apply(one.apply(
... IO(lambda x: lambda y: sum_two_numbers(x, y)),
... )) == IO(3)
It would be faster, but not as elegant (and type-safe).
Working with iterable of containers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Imagine that you have to take 10 random numbers
and then sum them to get the final result.
So, here's how your code will look like:
.. code:: python
>>> import random
>>> from returns.io import IO
>>> def random_number() -> IO[int]:
... return IO(2) # Example, basically alias of ``random.randint(1, 5)``
>>> numbers = [random_number() for _ in range(10)]
>>> assert len(numbers) == 10
>>> assert all(isinstance(number, IO) for number in numbers)
So, how to sum these random values into a single ``IO[int]`` value?
That's where
:meth:`Fold.loop <returns.iterables.AbstractFold.loop>` really helps!
.. code:: python
>>> from typing import Callable
>>> from returns.iterables import Fold
>>> def sum_two_numbers(first: int) -> Callable[[int], int]:
... return lambda second: first + second
>>> assert Fold.loop(
... numbers, # let's loop on our ``IO`` values
... IO(0), # starting from ``0`` value
... sum_two_numbers, # and getting the sum of each two numbers in a loop
... ) == IO(20)
We can also change the initial element to some other value:
.. code:: python
>>> assert Fold.loop(
... numbers,
... IO(5), # now we will start from ``5``, not ``0`
... sum_two_numbers,
... ) == IO(25)
``Fold.loop`` is eager. It will be executed for all items in your iterable.
Collecting an iterable of containers into a single container
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You might end up with an iterable of containers:
.. code:: python
>>> from typing import List
>>> from returns.maybe import Maybe, Some, Nothing, maybe
>>> source = {'a': 1, 'b': 2}
>>> fetched_values: List[Maybe[int]] = [
... maybe(source.get)(key)
... for key in ('a', 'b')
... ]
To work with iterable of containers,
it is recommended to cast it into a container with the iterable inside
using the :meth:`Fold.collect <returns.iterables.AbstractFold.collect>` method:
.. code:: python
>>> from returns.iterables import Fold
>>> assert Fold.collect(fetched_values, Some(())) == Some((1, 2))
Any falsy values will result in a falsy result (pun intended):
.. code:: python
>>> fetched_values: List[Maybe[int]] = [
... maybe(source.get)(key)
... for key in ('a', 'c') # 'c' is missing!
... ]
>>> assert Fold.collect(fetched_values, Some(())) == Nothing
You can also use a different strategy to fetch values you need,
to do just that we have
:meth:`Fold.collect_all <returns.iterables.AbstractFold.collect_all>` method:
.. code:: python
>>> fetched_values: Maybe[int] = [
... maybe(source.get)(key)
... for key in ('a', 'c') # 'c' is missing!
... ]
>>> assert Fold.collect_all(fetched_values, Some(())) == Some((1,))
We support any ``Iterable[T]`` input type
and return a ``Container[Sequence[T]]``.
You can subclass ``Fold`` type to change how any of these methods work.
.. _immutability:
Immutability
------------
We like to think of ``returns``
as :ref:`immutable <primitive-types>` structures.
You cannot mutate the inner state of the created container,
because we redefine ``__setattr__`` and ``__delattr__`` magic methods.
You cannot also set new attributes to container instances,
since we are using ``__slots__`` for better performance and strictness.
Well, nothing is **really** immutable in python, but you were warned.
We also provide :class:`returns.primitives.types.Immutable` mixin
that users can use to quickly make their classes immutable.
.. _type-safety:
Type safety
-----------
We try to make our containers optionally type safe.
What does it mean?
1. It is still good old ``python``, do whatever you want without ``mypy``
2. If you are using ``mypy`` you will be notified about type violations
We also ship `PEP561 <https://www.python.org/dev/peps/pep-0561/>`_
compatible ``.pyi`` files together with the source code.
In this case these types will be available to users
when they install our application.
We also ship custom ``mypy`` plugins to overcome some existing problems,
please make sure to use them,
since they increase your developer experience and type-safety level:
Check out our docs on using our :ref:`mypy plugins <mypy-plugins>`.
Further reading
---------------
- :ref:`Railway oriented programming <railway>`
.. _base-interfaces:
API Reference
-------------
``BaseContainer`` is a base class for all other containers.
It defines some basic things like representation, hashing, pickling, etc.
.. autoclasstree:: returns.primitives.container
:strict:
.. automodule:: returns.primitives.container
:members:
:special-members:
|