You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ to likely performance issues, to minor points of idiom that might signal
16
16
a misunderstanding.
17
17
18
18
19
-
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.
19
+
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.
Copy file name to clipboardExpand all lines: docs/rules.rst
+22Lines changed: 22 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,13 @@ General rules
16
16
- **ASYNC110**: ``while <condition>: await [trio/anyio].sleep()`` should be replaced by a ``[trio/anyio].Event``.
17
17
- **ASYNC111**: Variable, from context manager opened inside nursery, passed to ``start[_soon]`` might be invalidly accessed while in use, due to context manager closing before the nursery. This is usually a bug, and nurseries should generally be the inner-most context manager.
18
18
- **ASYNC112**: Nursery body with only a call to ``nursery.start[_soon]`` and not passing itself as a parameter can be replaced with a regular function call.
19
+
20
+
.. _async113:
21
+
19
22
- **ASYNC113**: Using :meth:`trio.Nursery.start_soon` in ``__aenter__`` doesn't wait for the task to begin. Consider replacing with ``nursery.start``.
23
+
24
+
.. _async114:
25
+
20
26
- **ASYNC114**: Startable function (i.e. has a ``task_status`` keyword parameter) not in ``--startable-in-context-manager`` parameter list, please add it so ASYNC113 can catch errors when using it.
21
27
- **ASYNC115**: Replace ``[trio/anyio].sleep(0)`` with the more suggestive ``[trio/anyio].lowlevel.checkpoint()``.
22
28
- **ASYNC116**: ``[trio/anyio].sleep()`` with >24 hour interval should usually be ``[trio/anyio].sleep_forever()``.
@@ -33,6 +39,7 @@ Blocking sync calls in async functions
33
39
34
40
Note: 22X, 23X and 24X has not had asyncio-specific suggestions written.
35
41
42
+
.. _async200:
36
43
37
44
- **ASYNC200**: User-configured error for blocking sync calls in async functions. Does nothing by default, see :ref:`async200-blocking-calls` for how to configure it.
38
45
- **ASYNC210**: Sync HTTP call in async function, use ``httpx.AsyncClient``. This and the other ASYNC21x checks look for usage of ``urllib3`` and ``httpx.Client``, and recommend using ``httpx.AsyncClient`` as that's the largest http client supporting anyio/trio.
@@ -55,11 +62,26 @@ Optional rules disabled by default
55
62
.. _async900:
56
63
57
64
- **ASYNC900**: Async generator without ``@asynccontextmanager`` not allowed. You might want to enable this on a codebase since async generators are inherently unsafe and cleanup logic might not be performed. See https://github.com/python-trio/flake8-async/issues/211 and https://discuss.python.org/t/using-exceptiongroup-at-anthropic-experience-report/20888/6 for discussion.
65
+
66
+
.. _async910:
67
+
58
68
- **ASYNC910**: Exit or ``return`` from async function with no guaranteed checkpoint or exception since function definition. You might want to enable this on a codebase to make it easier to reason about checkpoints, and make the logic of ASYNC911 correct.
69
+
70
+
.. _async911:
71
+
59
72
- **ASYNC911**: Exit, ``yield`` or ``return`` from async iterable with no guaranteed checkpoint since possible function entry (yield or function definition)
60
73
Checkpoints are ``await``, ``async for``, and ``async with`` (on one of enter/exit).
61
74
- **ASYNC912**: A timeout/cancelscope has checkpoints, but they're not guaranteed to run. Similar to ASYNC100, but it does not warn on trivial cases where there is no checkpoint at all. It instead shares logic with ASYNC910 and ASYNC911 for parsing conditionals and branches.
62
75
76
+
.. _autofix-support:
77
+
78
+
Autofix support
79
+
===============
80
+
The following rules support :ref:`autofixing <autofix>`.
Copy file name to clipboardExpand all lines: docs/usage.rst
+91-30Lines changed: 91 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,9 @@
2
2
Installation
3
3
************
4
4
5
-
.. code-block:: console
5
+
Install from `PyPI <https://pypi.org/project/flake8-async>`__
6
+
7
+
.. code-block:: sh
6
8
7
9
pip install flake8-async
8
10
@@ -23,7 +25,7 @@ install and run through flake8
23
25
install and run with pre-commit
24
26
===============================
25
27
26
-
If you use `pre-commit <https://pre-commit.com/>`_, you can use it with flake8-async by
28
+
If you use `pre-commit <https://pre-commit.com/>`__, you can use it with flake8-async by
27
29
adding the following to your ``.pre-commit-config.yaml``:
28
30
29
31
.. code-block:: yaml
@@ -38,17 +40,21 @@ adding the following to your ``.pre-commit-config.yaml``:
38
40
39
41
This is often considerably faster for large projects, because ``pre-commit``
40
42
can avoid running ``flake8-async`` on unchanged files.
43
+
``flake8-async`` does not retain any memory between files, they are parsed completely independently.
44
+
41
45
Afterwards, run
42
46
43
47
.. code-block:: sh
44
48
45
49
pip install pre-commit flake8-async
46
50
pre-commit run .
47
51
52
+
.. _run_standalone:
53
+
48
54
install and run as standalone
49
55
=============================
50
56
51
-
If inside a git repository, running without arguments will run it against all ``*.py`` files in the repository.
57
+
If inside a git repository, running without arguments will run it against all ``*.py`` files in the repository tree.
52
58
53
59
.. code-block:: sh
54
60
@@ -81,7 +87,7 @@ Run through ruff
81
87
================
82
88
`Ruff <https://github.com/astral-sh/ruff>`_ is a linter and formatter that reimplements a lot of rules from various flake8 plugins.
83
89
84
-
They currently only support a small subset of the rules though, see https://github.com/astral-sh/ruff/issues/8451 for current status and https://docs.astral.sh/ruff/rules/#flake8-async-async for documentation.
90
+
They currently only support a small subset of the ``flake8-async`` rules though, see https://github.com/astral-sh/ruff/issues/8451 for current status and https://docs.astral.sh/ruff/rules/#flake8-async-async for documentation.
85
91
86
92
*************
87
93
Configuration
@@ -105,60 +111,110 @@ config files since flake8>=6, as flake8 tries to validate correct
105
111
configuration with a regex. We have decided not to conform to this, as it
106
112
would be a breaking change for end-users requiring them to update ``noqa``\ s
107
113
and configurations, we think the ``ASYNC`` code is much more readable than
108
-
e.g. ``ASYxxx``, and ruff does not enforce such a limit. The easiest option
109
-
for users hitting this error is to instead use the ``--disable`` option as
110
-
documented `below <#--disable>`__. See further discussion and other
114
+
e.g. ``ASYxxx``, and ruff does not enforce such a limit.
115
+
The easiest option for users hitting this error is to instead use the :ref:`disable`
116
+
option.
117
+
See further discussion and other
111
118
workarounds in https://github.com/python-trio/flake8-async/issues/230.
112
119
120
+
.. _enable:
113
121
114
-
``--enable``
122
+
``enable``
115
123
------------
116
124
117
-
Comma-separated list of error codes to enable, similar to flake8 --select but is additionally more performant as it will disable non-enabled visitors from running instead of just silencing their errors.
125
+
Comma-separated list of error codes to enable, similar to flake8 --select but is additionally more performant as it will disable non-enabled visitors from running instead of just silencing their errors. Defaults to "ASYNC".
126
+
127
+
Example
128
+
^^^^^^^
129
+
.. code-block:: none
118
130
119
-
.. _--disable:
131
+
enable=ASYNC1,ASYNC200
120
132
121
-
``--disable``
133
+
.. _disable:
134
+
135
+
``disable``
122
136
-------------
123
137
124
138
Comma-separated list of error codes to disable, similar to flake8 ``--ignore`` but is additionally more performant as it will disable non-enabled visitors from running instead of just silencing their errors. It will also bypass errors introduced in flake8>=6, see above.
139
+
This is parsed after :ref:`enable`, so if a rule is both "enabled" and "disabled" it will be disabled.
140
+
Defaults to "ASYNC9".
141
+
142
+
Example
143
+
^^^^^^^
144
+
.. code-block:: none
145
+
146
+
disable=ASYNC91,ASYNC117
125
147
126
-
``--autofix``
148
+
.. _autofix:
149
+
150
+
``autofix``
127
151
-------------
128
152
129
-
Comma-separated list of error-codes to enable autofixing for if implemented. Requires running as a standalone program. Pass ``--autofix=ASYNC`` to enable all autofixes.
153
+
Comma-separated list of error-codes to enable autofixing for if implemented.
154
+
Requires :ref:`running as a standalone program <run_standalone>`.
155
+
Only a subset of rules support autofixing, see :ref:`this list <autofix-support>`.
156
+
Pass ``--autofix=ASYNC`` to enable all available autofixes.
157
+
158
+
Defaults to an empty list.
130
159
160
+
Example
161
+
^^^^^^^
162
+
.. code-block:: none
131
163
132
-
``--error-on-autofix``
164
+
autofix=ASYNC
165
+
166
+
167
+
``error-on-autofix``
133
168
----------------------
134
169
135
-
Whether to also print an error message for autofixed errors.
170
+
Whether to also print an error message for autofixed errors. Defaults to ``False``
171
+
172
+
Example
173
+
^^^^^^^
174
+
.. code-block:: none
175
+
176
+
error-on-autofix=True
136
177
137
178
Modifying rule behaviour
138
179
========================
139
180
140
-
.. _--anyio:
181
+
.. _anyio:
141
182
142
-
``--anyio``
183
+
``anyio``
143
184
-----------
144
185
145
-
Change the default library to be anyio instead of trio. If trio is imported it will assume both are available and print suggestions with [anyio/trio].
186
+
Change the default library to be anyio instead of trio. This is mostly used for the sake of printing suggestions in error messages, but may affect some logic. If additional libraries are imported other than the default then rules will assume multiple are available simultaneously. It is currently not possible to set multiple default libraries, other than `anyio`+`asyncio`.
187
+
188
+
Example
189
+
^^^^^^^
190
+
.. code-block:: none
146
191
147
-
``--asyncio``
192
+
anyio=True
193
+
194
+
.. _asyncio:
195
+
196
+
``asyncio``
148
197
-------------
149
-
Set default library to be ``asyncio``. See :ref:`--anyio`
198
+
Set default library to be ``asyncio``. See :ref:`anyio`
199
+
200
+
Example
201
+
^^^^^^^
202
+
.. code-block:: none
203
+
204
+
asyncio=True
150
205
151
206
152
207
``no-checkpoint-warning-decorators``
153
208
------------------------------------
154
209
155
-
Comma-separated list of decorators to disable checkpointing checks for, turning off ASYNC910 and ASYNC911 warnings for functions decorated with any decorator matching any in the list. Matching is done with `fnmatch <https://docs.python.org/3/library/fnmatch.html>`_. Defaults to disabling for ``asynccontextmanager``.
210
+
Comma-separated list of decorators to disable checkpointing checks for, turning off :ref:`ASYNC910 <async910>` and :ref:`ASYNC911<async911>` warnings for functions decorated with any decorator matching any in the list. Matching is done with `fnmatch <https://docs.python.org/3/library/fnmatch.html>`_. Defaults to disabling for ``asynccontextmanager``.
156
211
157
212
Decorators-to-match must be identifiers or dotted names only (not PEP-614 expressions), and will match against the name only - e.g. ``foo.bar`` matches ``foo.bar``, ``foo.bar()``, and ``foo.bar(args, here)``, etc.
158
213
159
-
For example:
214
+
Example
215
+
^^^^^^^
160
216
161
-
::
217
+
.. code-block:: none
162
218
163
219
no-checkpoint-warning-decorators =
164
220
mydecorator,
@@ -171,9 +227,13 @@ For example:
171
227
172
228
Comma-separated list of methods which should be used with ``.start()`` when opening a context manager,
173
229
in addition to the default ``trio.run_process``, ``trio.serve_tcp``, ``trio.serve_ssl_over_tcp``, and
174
-
``trio.serve_listeners``. Names must be valid identifiers as per ``str.isidentifier()``. For example:
230
+
``trio.serve_listeners``. Names must be valid identifiers as per ``str.isidentifier()``.
231
+
Used by :ref:`ASYNC113 <async113>`, and :ref:`ASYNC114 <async114>` will warn when encountering methods not in the list.
232
+
233
+
Example
234
+
^^^^^^^
175
235
176
-
::
236
+
.. code-block:: none
177
237
178
238
startable-in-context-manager =
179
239
myfun,
@@ -182,15 +242,16 @@ in addition to the default ``trio.run_process``, ``trio.serve_tcp``, ``trio.serv
182
242
.. _async200-blocking-calls:
183
243
184
244
``async200-blocking-calls``
185
-
---------------------------
245
+
-----------------------------
186
246
187
-
Comma-separated list of pairs of values separated by ``->`` (optional whitespace stripped), where the first is a pattern for a call that should raise an error if found inside an async function, and the second is what should be suggested to use instead. It uses fnmatch as per `no-checkpoint-warning-decorators`_ for matching. The part after ``->`` is not used by the checker other than when printing the error, so you could add extra info there if you want.
247
+
Comma-separated list of pairs of values separated by ``->`` (optional whitespace stripped), where the first is a pattern for a call that should raise :ref:`ASYNC200 <async200>` if found inside an async function, and the second is what should be suggested to use instead. It uses fnmatch as per `no-checkpoint-warning-decorators`_ for matching. The part after ``->`` is not used by the checker other than when printing the error, so you could add extra info there if you want.
188
248
189
249
The format of the error message is ``User-configured blocking sync call {0} in async function, consider replacing with {1}.``, where ``{0}`` is the pattern the call matches and ``{1}`` is the suggested replacement.
190
250
191
-
Example:
251
+
Example
252
+
^^^^^^^
192
253
193
-
::
254
+
.. code-block:: none
194
255
195
256
async200-blocking-calls =
196
257
my_blocking_call -> async.alternative,
@@ -201,7 +262,7 @@ Example:
201
262
202
263
Specified patterns must not have parentheses, and will only match when the pattern is the name of a call, so given the above configuration
0 commit comments