File: io.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 (87 lines) | stat: -rw-r--r-- 2,614 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
from __future__ import annotations

from abc import abstractmethod
from collections.abc import Callable
from typing import TYPE_CHECKING, TypeVar

from typing_extensions import Never

from returns.interfaces import container, equable
from returns.primitives.hkt import KindN

if TYPE_CHECKING:
    from returns.io import IO  # noqa: WPS433

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

_IOLikeType = TypeVar('_IOLikeType', bound='IOLikeN')


class IOLikeN(container.ContainerN[_FirstType, _SecondType, _ThirdType]):
    """
    Represents interface for types that looks like fearless ``IO``.

    This type means that ``IO`` cannot fail. Like random numbers, date, etc.
    Don't use this type for ``IO`` that can. Instead, use
    :class:`returns.interfaces.specific.ioresult.IOResultBasedN` type.

    """

    __slots__ = ()

    @abstractmethod
    def bind_io(
        self: _IOLikeType,
        function: Callable[[_FirstType], IO[_UpdatedType]],
    ) -> KindN[_IOLikeType, _UpdatedType, _SecondType, _ThirdType]:
        """Allows to apply a wrapped function over a container."""

    @classmethod
    @abstractmethod
    def from_io(
        cls: type[_IOLikeType],
        inner_value: IO[_UpdatedType],
    ) -> KindN[_IOLikeType, _UpdatedType, _SecondType, _ThirdType]:
        """Unit method to create new containers from successful ``IO``."""


#: Type alias for kinds with one type argument.
IOLike1 = IOLikeN[_FirstType, Never, Never]

#: Type alias for kinds with two type arguments.
IOLike2 = IOLikeN[_FirstType, _SecondType, Never]

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


class IOBasedN(
    IOLikeN[_FirstType, _SecondType, _ThirdType],
    equable.Equable,
):
    """
    Represents the base interface for types that do fearless ``IO``.

    This type means that ``IO`` cannot fail. Like random numbers, date, etc.
    Don't use this type for ``IO`` that can. Instead, use
    :class:`returns.interfaces.specific.ioresult.IOResultBasedN` type.

    This interface also supports direct comparison of two values.
    While ``IOLikeN`` is different. It can be lazy and cannot be compared.

    """

    __slots__ = ()


#: Type alias for kinds with one type argument.
IOBased1 = IOBasedN[_FirstType, Never, Never]

#: Type alias for kinds with two type arguments.
IOBased2 = IOBasedN[_FirstType, _SecondType, Never]

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