Skip to content

Commit b9b0869

Browse files
jakkdlZac-HD
andauthored
Rules documentation improvements (#248)
* stray newline broke bullet list * wip rules doc improvements * improve glossary, make use of it and intersphinx in blurbs * Update docs/rules.rst Co-authored-by: Zac Hatfield-Dodds <[email protected]> * moar doc improvements * move out glossary to its own page, link to rules in the readme * actually add glossary.rst * switch from table to definition list * add more intersphinx links * glossary improvements, rename some rules, add some intersphinx links * improve the checkpoint glossary entry * fix RTD * checkpoint glossary tweaks + denser dot-points --------- Co-authored-by: Zac Hatfield-Dodds <[email protected]>
1 parent 9653fde commit b9b0869

File tree

5 files changed

+286
-65
lines changed

5 files changed

+286
-65
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ Pairs well with flake8-bugbear.
1818
Some checks are incorporated into [ruff](https://github.com/astral-sh/ruff).
1919

2020
This plugin was previously known as flake8-trio, and there was a separate small plugin known as flake8-async for asyncio. But this plugin was a superset of the checks in flake8-async, and support for anyio was added, so it's now named flake8-async to more properly convey its usage. At the same time all error codes were renamed from TRIOxxx to ASYNCxxx, as was previously used by the old flake8-async.
21+
22+
## Rules
23+
https://flake8-async.readthedocs.io/en/latest/rules.html

docs/glossary.rst

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
********
2+
Glossary
3+
********
4+
5+
.. _cancel_scope:
6+
7+
cancel scope
8+
------------
9+
A cancel scope is a context manager which can request the library cancels
10+
whatever task is executing in the body of the ``with`` (or ``async with``)
11+
block. A cancel scope is the key component of a :ref:`timeout context <timeout_context>`,
12+
and used in :ref:`TaskGroups / Nurseries <taskgroup_nursery>` to cancel any remaining child tasks if one raises an
13+
exception.
14+
15+
* Trio has an explicit :class:`trio.CancelScope` type, and `general documentation
16+
<https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts>`__
17+
about cancellation and timeouts.
18+
19+
* AnyIO similarly has :class:`anyio.CancelScope` and `documentation
20+
<https://anyio.readthedocs.io/en/stable/cancellation.html>`__ of cancellation handling.
21+
22+
* asyncio does not have an explicit cancel-scope type, but incorporates similar semantics
23+
in :func:`asyncio.timeout` and :class:`asyncio.TaskGroup` and has `some documentation
24+
<https://docs.python.org/3/library/asyncio-task.html#task-cancellation>`__.
25+
26+
27+
.. _timeout_context:
28+
29+
timeout context
30+
---------------
31+
A context manager that enforces a timeout on a block of code, by cancelling it
32+
after a specified duration or at a preset time. The timeout can also be
33+
rescheduled after creation. They are internally implemented with a :ref:`cancel scope <cancel_scope>`,
34+
which in anyio & trio can be directly initialized with a deadline.
35+
36+
* Trio has :func:`trio.move_on_after`, :func:`trio.move_on_at`,
37+
:func:`trio.fail_after`, :func:`trio.fail_at`, and :class:`trio.CancelScope`
38+
(`docs <https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts>`__)
39+
40+
* AnyIO has :func:`anyio.move_on_after`, :func:`anyio.fail_after`, and :class:`anyio.CancelScope`
41+
(`docs <https://anyio.readthedocs.io/en/stable/cancellation.html>`__)
42+
43+
* asyncio has :func:`asyncio.timeout` and :func:`asyncio.timeout_at`
44+
(`docs <https://docs.python.org/3/library/asyncio-task.html#timeouts>`__)
45+
46+
47+
.. _taskgroup_nursery:
48+
49+
TaskGroup / Nursery
50+
-------------------
51+
52+
A collection of child Tasks that can run concurrently. Internally contains a
53+
:ref:`cancel scope <cancel_scope>` for canceling any remaining child tasks if
54+
one raises an exception.
55+
56+
* Trio has :class:`trio.Nursery`, created with :func:`trio.open_nursery`
57+
(`docs <https://trio.readthedocs.io/en/stable/reference-core.html#tasks-let-you-do-multiple-things-at-once>`__)
58+
59+
* AnyIO has :class:`anyio.abc.TaskGroup`, created with :func:`anyio.create_task_group`
60+
(`docs <https://anyio.readthedocs.io/en/stable/tasks.html>`__)
61+
62+
* asyncio has :class:`asyncio.TaskGroup` since python 3.11
63+
(`docs <https://docs.python.org/3/library/asyncio-task.html#asyncio.TaskGroup>`__)
64+
65+
66+
.. _cancellation:
67+
.. _cancelled:
68+
69+
Cancelled / CancelledError
70+
--------------------------
71+
72+
Handling cancellation is very sensitive, and you generally never want to catch a
73+
cancellation exception without letting it propagate to the library.
74+
75+
General documentation on cancellation in the different async libraries:
76+
77+
* `Trio <https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts>`__
78+
* `AnyIO <https://anyio.readthedocs.io/en/stable/cancellation.html>`__
79+
* `asyncio <https://docs.python.org/3/library/asyncio-task.html#task-cancellation>`__
80+
81+
Exception classes:
82+
83+
* :class:`trio.Cancelled`
84+
* :func:`anyio.get_cancelled_exc_class`
85+
* :class:`asyncio.CancelledError`
86+
87+
.. _checkpoint:
88+
89+
Checkpoint
90+
----------
91+
Checkpoints are points where the async backend checks for cancellation and
92+
can switch which task is running, in an ``await``, ``async for``, or ``async with``
93+
expression. Regular checkpoints can be important for both performance and correctness.
94+
95+
Trio has extensive and detailed documentation on the concept of
96+
:external+trio:ref:`checkpoints <checkpoints>`, and guarantees that all async
97+
functions defined by Trio will either checkpoint or raise an exception when
98+
``await``-ed. ``async for`` on Trio iterables will checkpoint before each
99+
iteration, and when exhausting the iterator, and ``async with`` will checkpoint
100+
on at least one of enter/exit.
101+
102+
asyncio does not place any guarantees on if or when asyncio functions will
103+
checkpoint. This means that enabling and adhering to :ref:`ASYNC91x <ASYNC910>`
104+
will still not guarantee checkpoints.
105+
106+
For anyio it will depend on the current backend.
107+
108+
When using Trio (or an AnyIO library that people might use on Trio), it can be
109+
very helpful to ensure that your own code adheres to the same guarantees as
110+
Trio. For this we supply the :ref:`ASYNC91x <ASYNC910>` rules. To make it
111+
possible to reason the rules will also assume that all other async functions
112+
also adhere to those rules. This means you must be careful if you're using
113+
3rd-party async libraries.
114+
115+
To insert a checkpoint with no other side effects, you can use
116+
:func:`trio.lowlevel.checkpoint`/:func:`anyio.lowlevel.checkpoint`/:func:`asyncio.sleep(0)
117+
<asyncio.sleep>`
118+
119+
.. _channel_stream_queue:
120+
121+
Channel / Stream / Queue
122+
------------------------
123+
Interfaces used for communicating between tasks, processes, the network, etc.
124+
125+
.. anyio streams is a :doc: and not a :label:, so we can't link with intersphinx :(
126+
127+
.. _anyio_streams: https://anyio.readthedocs.io/en/stable/streams.html#streams
128+
129+
* Trio has :ref:`channels <channels>` for python objects and :ref:`streams <abstract-stream-api>` for bytes.
130+
* AnyIO has ``byte`` and ``object`` `streams <anyio_streams>`_
131+
* asyncio has :ref:`queues <asyncio-queues>` for python objects and :ref:`streams <asyncio-streams>` for bytes.

docs/index.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ flake8-async
88
############
99

1010

11-
A highly opinionated flake8 plugin for problems related to `Trio <https://github.com/python-trio/trio>`_, `AnyIO <https://github.com/agronholm/anyio>`_, or `asyncio <https://docs.python.org/3/library/asyncio.html>`_.
11+
A highly opinionated flake8 plugin for problems related to `Trio <https://github.com/python-trio/trio>`__, `AnyIO <https://github.com/agronholm/anyio>`__, or `asyncio <https://docs.python.org/3/library/asyncio.html>`__.
1212

1313

1414
This can include anything from outright bugs, to pointless/dead code,
@@ -17,10 +17,10 @@ a misunderstanding.
1717

1818

1919
The plugin may well be too noisy or pedantic depending on your requirements or opinions, in which case you should consider :ref:`disable` for those rules.
20-
Pairs well with flake8-bugbear.
20+
Pairs well with `flake8-bugbear <https://github.com/PyCQA/flake8-bugbear>`__.
2121

2222

23-
Some rules are incorporated into `ruff <https://docs.astral.sh/ruff/rules/#flake8-async-async>`_.
23+
Some rules are incorporated into `ruff <https://docs.astral.sh/ruff/rules/#flake8-async-async>`__.
2424

2525

2626
We previously maintained separate flake8-async and flake8-trio plugins, but merged both into this plugin under the more general "flake8-async" name after flake8-trio grew support for anyio and asyncio and became a superset of the former flake8-async. All flake8-trio error codes were renamed from TRIOxxx to ASYNCxxx and the flake8-trio package is now deprecated.
@@ -33,6 +33,7 @@ Contents:
3333

3434
usage
3535
rules
36+
glossary
3637
changelog
3738
contributing
3839

@@ -46,5 +47,6 @@ Indices and tables
4647
* :ref:`search`
4748
* :doc:`usage`
4849
* :doc:`rules`
50+
* :doc:`glossary`
4951
* :doc:`changelog`
5052
* :doc:`contributing`

0 commit comments

Comments
 (0)