Skip to content

Commit 0bc229e

Browse files
committed
Closes #208, closes #201
1 parent 1d88349 commit 0bc229e

File tree

19 files changed

+230
-16
lines changed

19 files changed

+230
-16
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ See (0Ver)[https://0ver.org/].
88

99
## 0.13.0
1010

11+
### Features
12+
13+
- Adds `io_squash` to squash several `IO` containers into one container with a tuple inside, currently works with `9` containers max at a time
14+
1115
### Bugfixes
1216

1317
- Fixes that containers were not usable with `multiprocessing`
18+
- Changes the inheritance order, now `BaseContainer` is the first child
19+
20+
### Misc
21+
22+
- Now `generated` package is protected
1423

1524

1625
## 0.12.0

docs/pages/io.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,34 @@ if :ref:`decorator_plugin <type-safety>` is used.
194194
This happens due to `mypy issue <https://github.com/python/mypy/issues/3157>`_.
195195

196196

197+
io_squash
198+
---------
199+
200+
This function allows to squash several ``IO`` containers together.
201+
202+
That's how it works:
203+
204+
.. code:: python
205+
206+
from returns.io import IO, io_squash
207+
208+
io_squash(IO('first'), IO('second')) == IO(('first', 'second'))
209+
# => revealed type of this instance is `IO[Tuple[str, str]]`
210+
211+
It might be helpful if you want
212+
to work with mutliple ``IO`` instances at the same time.
213+
214+
This approach saves you you from multiple nested ``IO.map`` calls.
215+
You can work with tuples instead like so:
216+
217+
.. code:: python
218+
219+
io_squash(IO(1), IO(2)).map(lambda args: args[0] + args[1])
220+
# => IO(3)
221+
222+
We support up to 9 typed parameters to this function.
223+
224+
197225
unsafe_perform_io
198226
-----------------
199227

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

returns/generated/pipe.pyi renamed to returns/_generated/pipe.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _pipe(
4040
...
4141

4242

43-
@overload # noqa: WPS211
43+
@overload
4444
def _pipe(
4545
p1: Callable[[_T1], _T2],
4646
p2: Callable[[_T2], _T3],
@@ -51,7 +51,7 @@ def _pipe(
5151
...
5252

5353

54-
@overload # noqa: WPS211
54+
@overload
5555
def _pipe(
5656
p1: Callable[[_T1], _T2],
5757
p2: Callable[[_T2], _T3],
@@ -63,7 +63,7 @@ def _pipe(
6363
...
6464

6565

66-
@overload # noqa: WPS211
66+
@overload
6767
def _pipe(
6868
p1: Callable[[_T1], _T2],
6969
p2: Callable[[_T2], _T3],
@@ -76,7 +76,7 @@ def _pipe(
7676
...
7777

7878

79-
@overload # noqa: WPS211
79+
@overload
8080
def _pipe(
8181
p1: Callable[[_T1], _T2],
8282
p2: Callable[[_T2], _T3],

returns/_generated/squash.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
def _squash(*args):
5+
"""
6+
Unwraps ``IO`` values, merges them into tuple, and wrapps back.
7+
8+
.. code:: python
9+
10+
>>> from returns.io import IO
11+
>>> str(_squash(IO(1), IO(2)))
12+
'<IO: (1, 2)>'
13+
14+
Also available as a class-method of ``IO`` container:
15+
16+
.. code:: python
17+
18+
>>> from returns.io import io_squash
19+
>>> str(io_squash(IO('a'), IO('b')))
20+
"<IO: ('a', 'b')>"
21+
22+
"""
23+
wrapper_class = type(args[0])
24+
return wrapper_class(tuple(
25+
container._inner_value # noqa: WPS437
26+
for container in args
27+
))

returns/_generated/squash.pyi

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from typing import Tuple, TypeVar, overload
4+
5+
from returns.io import IO
6+
7+
_T1 = TypeVar('_T1')
8+
_T2 = TypeVar('_T2')
9+
_T3 = TypeVar('_T3')
10+
_T4 = TypeVar('_T4')
11+
_T5 = TypeVar('_T5')
12+
_T6 = TypeVar('_T6')
13+
_T7 = TypeVar('_T7')
14+
_T8 = TypeVar('_T8')
15+
_T9 = TypeVar('_T9')
16+
17+
18+
@overload
19+
def _squash(
20+
p1: IO[_T1],
21+
p2: IO[_T2],
22+
) -> IO[Tuple[_T1, _T2]]:
23+
...
24+
25+
26+
@overload
27+
def _squash(
28+
p1: IO[_T1],
29+
p2: IO[_T2],
30+
p3: IO[_T3],
31+
) -> IO[Tuple[_T1, _T2, _T3]]:
32+
...
33+
34+
35+
@overload
36+
def _squash(
37+
p1: IO[_T1],
38+
p2: IO[_T2],
39+
p3: IO[_T3],
40+
p4: IO[_T4],
41+
) -> IO[Tuple[_T1, _T2, _T3, _T4]]:
42+
...
43+
44+
45+
@overload
46+
def _squash(
47+
p1: IO[_T1],
48+
p2: IO[_T2],
49+
p3: IO[_T3],
50+
p4: IO[_T4],
51+
p5: IO[_T5],
52+
) -> IO[Tuple[_T1, _T2, _T3, _T4, _T5]]:
53+
...
54+
55+
56+
@overload
57+
def _squash(
58+
p1: IO[_T1],
59+
p2: IO[_T2],
60+
p3: IO[_T3],
61+
p4: IO[_T4],
62+
p5: IO[_T5],
63+
p6: IO[_T6],
64+
) -> IO[Tuple[_T1, _T2, _T3, _T4, _T5, _T6]]:
65+
...
66+
67+
68+
@overload
69+
def _squash(
70+
p1: IO[_T1],
71+
p2: IO[_T2],
72+
p3: IO[_T3],
73+
p4: IO[_T4],
74+
p5: IO[_T5],
75+
p6: IO[_T6],
76+
p7: IO[_T7],
77+
) -> IO[Tuple[_T1, _T2, _T3, _T4, _T5, _T6, _T7]]:
78+
...
79+
80+
81+
@overload
82+
def _squash(
83+
p1: IO[_T1],
84+
p2: IO[_T2],
85+
p3: IO[_T3],
86+
p4: IO[_T4],
87+
p5: IO[_T5],
88+
p6: IO[_T6],
89+
p7: IO[_T7],
90+
p8: IO[_T8],
91+
) -> IO[Tuple[_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8]]:
92+
...
93+
94+
95+
@overload
96+
def _squash(
97+
p1: IO[_T1],
98+
p2: IO[_T2],
99+
p3: IO[_T3],
100+
p4: IO[_T4],
101+
p5: IO[_T5],
102+
p6: IO[_T6],
103+
p7: IO[_T7],
104+
p8: IO[_T8],
105+
p9: IO[_T9],
106+
) -> IO[Tuple[_T1, _T2, _T3, _T4, _T5, _T6, _T7, _T8, _T9]]:
107+
...

0 commit comments

Comments
 (0)