The ``RequiresContext`` container. It's main purpose is to wrap some specific function and to provide tools to compose other functions around it without actually calling it. The ``RequiresContext`` container passes the state you want to share between functions. Functio
| 42 | |
| 43 | @final |
| 44 | class RequiresContext( # type: ignore[type-var] |
| 45 | BaseContainer, |
| 46 | SupportsKind2['RequiresContext', _ReturnType_co, _EnvType_contra], |
| 47 | reader.ReaderBased2[_ReturnType_co, _EnvType_contra], |
| 48 | ): |
| 49 | """ |
| 50 | The ``RequiresContext`` container. |
| 51 | |
| 52 | It's main purpose is to wrap some specific function |
| 53 | and to provide tools to compose other functions around it |
| 54 | without actually calling it. |
| 55 | |
| 56 | The ``RequiresContext`` container passes the state |
| 57 | you want to share between functions. |
| 58 | Functions may read that state, but can't change it. |
| 59 | The ``RequiresContext`` container |
| 60 | lets us access shared immutable state within a specific context. |
| 61 | |
| 62 | It can be used for lazy evaluation and typed dependency injection. |
| 63 | |
| 64 | ``RequiresContext`` is used with functions that never fail. |
| 65 | If you want to use ``RequiresContext`` with returns ``Result`` |
| 66 | then consider using ``RequiresContextResult`` instead. |
| 67 | |
| 68 | Note: |
| 69 | This container does not wrap ANY value. It wraps only functions. |
| 70 | You won't be able to supply arbitrary types! |
| 71 | |
| 72 | See also: |
| 73 | - https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5 |
| 74 | - https://en.wikipedia.org/wiki/Lazy_evaluation |
| 75 | - https://bit.ly/2R8l4WK |
| 76 | |
| 77 | """ |
| 78 | |
| 79 | __slots__ = () |
| 80 | |
| 81 | #: This field has an extra 'RequiresContext' just because `mypy` needs it. |
| 82 | _inner_value: Callable[[_EnvType_contra], _ReturnType_co] |
| 83 | |
| 84 | #: A convenient placeholder to call methods created by `.from_value()`: |
| 85 | no_args: ClassVar[NoDeps] = object() |
| 86 | |
| 87 | def __init__( |
| 88 | self, |
| 89 | inner_value: Callable[[_EnvType_contra], _ReturnType_co], |
| 90 | ) -> None: |
| 91 | """ |
| 92 | Public constructor for this type. Also required for typing. |
| 93 | |
| 94 | Only allows functions of kind ``* -> *``. |
| 95 | |
| 96 | .. code:: python |
| 97 | |
| 98 | >>> from returns.context import RequiresContext |
| 99 | >>> str(RequiresContext(lambda deps: deps + 1)) |
| 100 | '<RequiresContext: <function <lambda> at ...>>' |
| 101 |
no outgoing calls