Skip to content

Commit 5db1009

Browse files
committed
Fixes docs
1 parent ef2ed8a commit 5db1009

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Contents
1919
pages/container.rst
2020
pages/result.rst
2121
pages/io.rst
22+
pages/unsafe.rst
2223
pages/functions.rst
2324

2425
.. toctree::

docs/pages/io.rst

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ See? It is now impossible for a pure function to use ``IO[bool]``.
101101
It is impossible to unwrap or get a value from this container.
102102
Once it is marked as ``IO`` it will never return to the pure state.
103103

104+
Well, there's a hack actually:
105+
:py:func:`unsafe_perform_io <returns.unsafe.unsafe_perform_io>`
106+
104107
It also needs to be explicitly mapped to produce new ``IO`` result:
105108

106109
.. code:: python
@@ -148,37 +151,6 @@ There's one limitation in typing
148151
that we are facing right now
149152
due to `mypy issue <https://github.com/python/mypy/issues/3157>`_.
150153

151-
unsafe
152-
------
153-
154-
Sometimes you really need to get the raw value.
155-
For example:
156-
157-
.. code:: python
158-
159-
def index_view(request, user_id):
160-
user: IO[User] = get_user(user_id)
161-
return render('index.html', { user: user }) # ???
162-
163-
In this case your web-framework will not render your user correctly.
164-
Since it does not expect it to be wrapped inside ``IO`` containers.
165-
166-
What to do? Use ``unsafe_perform_io``:
167-
168-
.. code::
169-
170-
from returns.unsafe import unsafe_perform_io
171-
172-
def index_view(request, user_id):
173-
user: IO[User] = get_user(user_id)
174-
return render('index.html', { user: unsafe_perform_io(user) }) # Ok
175-
176-
We need it as an escape and compatibility mechanism for our imperative shell.
177-
178-
It is recommended
179-
to use `import-linter <https://github.com/seddonym/import-linter>`_
180-
to restrict imports from ``returns.unsafe`` expect the top-level modules.
181-
182154
Further reading
183155
---------------
184156

@@ -193,6 +165,3 @@ API Reference
193165

194166
.. automodule:: returns.io
195167
:members:
196-
197-
.. automodule:: returns.unsafe
198-
:members:

docs/pages/unsafe.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
unsafe
2+
======
3+
4+
Sometimes you really need to get the raw value.
5+
For example:
6+
7+
.. code:: python
8+
9+
def index_view(request, user_id):
10+
user: IO[User] = get_user(user_id)
11+
return render('index.html', { user: user }) # ???
12+
13+
In this case your web-framework will not render your user correctly.
14+
Since it does not expect it to be wrapped inside ``IO`` containers.
15+
16+
What to do? Use ``unsafe_perform_io``:
17+
18+
.. code::
19+
20+
from returns.unsafe import unsafe_perform_io
21+
22+
def index_view(request, user_id):
23+
user: IO[User] = get_user(user_id)
24+
return render('index.html', { user: unsafe_perform_io(user) }) # Ok
25+
26+
We need it as an escape and compatibility mechanism for our imperative shell.
27+
28+
It is recommended
29+
to use `import-linter <https://github.com/seddonym/import-linter>`_
30+
to restrict imports from ``returns.unsafe`` expect the top-level modules.
31+
32+
Inspired by Haskell's
33+
`unsafePerformIO <https://hackage.haskell.org/package/base-4.12.0.0/docs/System-IO-Unsafe.html#v:unsafePerformIO>`_
34+
35+
API Reference
36+
-------------
37+
38+
.. automodule:: returns.unsafe
39+
:members:

returns/unsafe.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ def unsafe_perform_io(wrapped_in_io):
55
"""
66
Compatibility utility and escape mechanism from IO world.
77
8-
Just unwraps the internal value from IO container.
8+
Just unwraps the internal value
9+
from :py:class:`IO <returns.io.IO>` container.
910
Should be used with caution!
1011
Since it might be overused by tired developers.
1112
1213
It is recommended to have only one place (module / file)
13-
in your program where you allow unsafe opetaions.
14+
in your program where you allow unsafe operations.
1415
1516
We recommend to use ``import-linter`` to enforce this rule:
17+
1618
- https://github.com/seddonym/import-linter
1719
1820
"""

0 commit comments

Comments
 (0)