File: reader_future_result.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 (153 lines) | stat: -rw-r--r-- 4,204 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
from __future__ import annotations

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

from returns.interfaces.specific import future_result, reader, reader_ioresult
from returns.primitives.asserts import assert_equal
from returns.primitives.hkt import KindN
from returns.primitives.laws import (
    Law,
    Law2,
    Lawful,
    LawSpecDef,
    law_definition,
)

if TYPE_CHECKING:
    from returns.context import ReaderFutureResult  # noqa: WPS433
    from returns.future import FutureResult  # noqa: F401, WPS433

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

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

_ReaderFutureResultLikeType = TypeVar(
    '_ReaderFutureResultLikeType',
    bound='ReaderFutureResultLikeN',
)


class ReaderFutureResultLikeN(
    reader_ioresult.ReaderIOResultLikeN[_FirstType, _SecondType, _ThirdType],
    future_result.FutureResultLikeN[_FirstType, _SecondType, _ThirdType],
):
    """
    Interface for all types that do look like ``ReaderFutureResult`` instance.

    Cannot be called.
    """

    __slots__ = ()

    @abstractmethod
    def bind_context_future_result(
        self: _ReaderFutureResultLikeType,
        function: Callable[
            [_FirstType],
            ReaderFutureResult[_UpdatedType, _SecondType, _ThirdType],
        ],
    ) -> KindN[
        _ReaderFutureResultLikeType,
        _UpdatedType,
        _SecondType,
        _ThirdType,
    ]:
        """Bind a ``ReaderFutureResult`` returning function over a container."""

    @abstractmethod
    def bind_async_context_future_result(
        self: _ReaderFutureResultLikeType,
        function: Callable[
            [_FirstType],
            Awaitable[
                ReaderFutureResult[_UpdatedType, _SecondType, _ThirdType],
            ],
        ],
    ) -> KindN[
        _ReaderFutureResultLikeType,
        _UpdatedType,
        _SecondType,
        _ThirdType,
    ]:
        """Bind async ``ReaderFutureResult`` function."""

    @classmethod
    @abstractmethod
    def from_future_result_context(
        cls: type[_ReaderFutureResultLikeType],
        inner_value: ReaderFutureResult[_ValueType, _ErrorType, _EnvType],
    ) -> KindN[_ReaderFutureResultLikeType, _ValueType, _ErrorType, _EnvType]:
        """Unit method to create new containers from ``ReaderFutureResult``."""


#: Type alias for kinds with three type arguments.
ReaderFutureResultLike3 = ReaderFutureResultLikeN[
    _FirstType,
    _SecondType,
    _ThirdType,
]


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

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

    __slots__ = ()

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


class ReaderFutureResultBasedN(
    ReaderFutureResultLikeN[_FirstType, _SecondType, _ThirdType],
    reader.CallableReader3[
        _FirstType,
        _SecondType,
        _ThirdType,
        # Calls:
        'FutureResult[_FirstType, _SecondType]',
        _ThirdType,
    ],
    Lawful['ReaderFutureResultBasedN[_FirstType, _SecondType, _ThirdType]'],
):
    """
    This interface is very specific to our ``ReaderFutureResult`` type.

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

    In this case the return type of ``__call__`` is ``FutureResult``.
    """

    __slots__ = ()

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


#: Type alias for kinds with three type arguments.
ReaderFutureResultBased3 = ReaderFutureResultBasedN[
    _FirstType,
    _SecondType,
    _ThirdType,
]