File: reader.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (262 lines) | stat: -rw-r--r-- 7,669 bytes parent folder | download | duplicates (2)
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
"""
This module is special.

``Reader`` does not produce ``ReaderLikeN`` interface as other containers.

Because ``Reader`` can be used with two or three type arguments:
- ``RequiresContext[value, env]``
- ``RequiresContextResult[value, error, env]``

Because the second type argument changes its meaning
based on the used ``KindN`` instance,
we need to have two separate interfaces for two separate use-cases:
- ``ReaderLike2`` is used for types where the second type argument is ``env``
- ``ReaderLike3`` is used for types where the third type argument is ``env``

We also have two methods and two poinfree helpers
for ``bind_context`` composition: one for each interface.

Furthermore, ``Reader`` cannot have ``ReaderLike1`` type,
because we need both ``value`` and ``env`` types at all cases.

See also:
    - https://github.com/dry-python/returns/issues/485

"""

from __future__ import annotations

from abc import abstractmethod
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, ClassVar, Generic, TypeVar, final

from returns.interfaces.container import Container2, Container3
from returns.primitives.hkt import Kind2, Kind3
from returns.primitives.laws import (
    Law,
    Law2,
    Lawful,
    LawSpecDef,
    law_definition,
)

if TYPE_CHECKING:
    from returns.context import NoDeps, RequiresContext  # noqa: WPS433

_FirstType = TypeVar('_FirstType')
_SecondType = TypeVar('_SecondType')
_ThirdType = TypeVar('_ThirdType')
_UpdatedType = TypeVar('_UpdatedType')

_ValueType = TypeVar('_ValueType')
_EnvType = TypeVar('_EnvType')

_ReaderLike2Type = TypeVar('_ReaderLike2Type', bound='ReaderLike2')
_ReaderLike3Type = TypeVar('_ReaderLike3Type', bound='ReaderLike3')


class Contextable(Generic[_ValueType, _EnvType]):
    """
    Special type we use as a base one for all callble ``Reader`` instances.

    It only has a single method.
    And is a base type for every single one of them.

    But, each ``Reader`` defines the return type differently.
    For example:

    - ``Reader`` has just ``_ReturnType``
    - ``ReaderResult`` has ``Result[_FirstType, _SecondType]``
    - ``ReaderIOResult`` has ``IOResult[_FirstType, _SecondType]``

    And so on.
    """

    __slots__ = ()

    @abstractmethod
    def __call__(self, deps: _EnvType) -> _ValueType:
        """Receives one parameter, returns a value. As simple as that."""


class ReaderLike2(Container2[_FirstType, _SecondType]):
    """
    Reader interface for ``Kind2`` based types.

    It has two type arguments and treats the second type argument as env type.
    """

    __slots__ = ()

    @property
    @abstractmethod
    def no_args(self: _ReaderLike2Type) -> NoDeps:
        """Is required to call ``Reader`` with no explicit arguments."""

    @abstractmethod
    def bind_context(
        self: _ReaderLike2Type,
        function: Callable[
            [_FirstType],
            RequiresContext[_UpdatedType, _SecondType],
        ],
    ) -> Kind2[_ReaderLike2Type, _UpdatedType, _SecondType]:
        """Allows to apply a wrapped function over a ``Reader`` container."""

    @abstractmethod
    def modify_env(
        self: _ReaderLike2Type,
        function: Callable[[_UpdatedType], _SecondType],
    ) -> Kind2[_ReaderLike2Type, _FirstType, _UpdatedType]:
        """Transforms the environment before calling the container."""

    @classmethod
    @abstractmethod
    def ask(
        cls: type[_ReaderLike2Type],
    ) -> Kind2[_ReaderLike2Type, _SecondType, _SecondType]:
        """Returns the dependencies inside the container."""

    @classmethod
    @abstractmethod
    def from_context(
        cls: type[_ReaderLike2Type],
        inner_value: RequiresContext[_ValueType, _EnvType],
    ) -> Kind2[_ReaderLike2Type, _ValueType, _EnvType]:
        """Unit method to create new containers from successful ``Reader``."""


class CallableReader2(
    ReaderLike2[_FirstType, _SecondType],
    Contextable[_ValueType, _EnvType],
):
    """
    Intermediate interface for ``ReaderLike2`` + ``__call__`` method.

    Has 4 type variables to type ``Reader`` and ``__call__`` independently.
    Since, we don't have any other fancy ways of doing it.

    Should not be used directly
    other than defining your own ``Reader`` interfaces.
    """

    __slots__ = ()


class ReaderLike3(Container3[_FirstType, _SecondType, _ThirdType]):
    """
    Reader interface for ``Kind3`` based types.

    It has three type arguments and treats the third type argument as env type.
    The second type argument is not used here.
    """

    __slots__ = ()

    @property
    @abstractmethod
    def no_args(self: _ReaderLike3Type) -> NoDeps:
        """Is required to call ``Reader`` with no explicit arguments."""

    @abstractmethod
    def bind_context(
        self: _ReaderLike3Type,
        function: Callable[
            [_FirstType],
            RequiresContext[_UpdatedType, _ThirdType],
        ],
    ) -> Kind3[_ReaderLike3Type, _UpdatedType, _SecondType, _ThirdType]:
        """Allows to apply a wrapped function over a ``Reader`` container."""

    @abstractmethod
    def modify_env(
        self: _ReaderLike3Type,
        function: Callable[[_UpdatedType], _ThirdType],
    ) -> Kind3[_ReaderLike3Type, _FirstType, _SecondType, _UpdatedType]:
        """Transforms the environment before calling the container."""

    @classmethod
    @abstractmethod
    def ask(
        cls: type[_ReaderLike3Type],
    ) -> Kind3[_ReaderLike3Type, _ThirdType, _SecondType, _ThirdType]:
        """Returns the dependencies inside the container."""

    @classmethod
    @abstractmethod
    def from_context(
        cls: type[_ReaderLike3Type],
        inner_value: RequiresContext[_ValueType, _EnvType],
    ) -> Kind3[_ReaderLike3Type, _ValueType, _SecondType, _EnvType]:
        """Unit method to create new containers from successful ``Reader``."""


class CallableReader3(
    ReaderLike3[_FirstType, _SecondType, _ThirdType],
    Contextable[_ValueType, _EnvType],
):
    """
    Intermediate interface for ``ReaderLike3`` + ``__call__`` method.

    Has 5 type variables to type ``Reader`` and ``__call__`` independently.
    Since, we don't have any other fancy ways of doing it.

    Should not be used directly
    other than defining your own ``Reader`` interfaces.
    """

    __slots__ = ()


@final
class _LawSpec(LawSpecDef):
    """
    Concrete laws for ``ReaderBased2``.

    See: https://github.com/haskell/mtl/pull/61/files
    """

    __slots__ = ()

    @law_definition
    def purity_law(
        container: ReaderBased2[_FirstType, _SecondType],
        env: _SecondType,
    ) -> None:
        """Calling a ``Reader`` twice has the same result with the same env."""
        assert container(env) == container(env)

    @law_definition
    def asking_law(
        container: ReaderBased2[_FirstType, _SecondType],
        env: _SecondType,
    ) -> None:
        """Asking for an env, always returns the env."""
        assert container.ask().__call__(  # noqa: PLC2801
            env,
        ) == container.from_value(env).__call__(env)  # noqa: PLC2801


class ReaderBased2(
    CallableReader2[
        _FirstType,
        _SecondType,
        # Used for call typing:
        _FirstType,
        _SecondType,
    ],
    Lawful['ReaderBased2[_FirstType, _SecondType]'],
):
    """
    This interface is very specific to our ``Reader`` type.

    The only thing that differs from ``ReaderLike2`` is that we know
    the specific types for its ``__call__`` method.
    """

    __slots__ = ()

    _laws: ClassVar[Sequence[Law]] = (
        Law2(_LawSpec.purity_law),
        Law2(_LawSpec.asking_law),
    )