|
6 | 6 | General rules
|
7 | 7 | =============
|
8 | 8 |
|
| 9 | +Our ``ASYNC1xx`` rules check for semantic problems ranging from fatal errors (e.g. 101), |
| 10 | +to idioms for clearer code (e.g. 116). |
| 11 | + |
9 | 12 | _`ASYNC100` : cancel-scope-no-checkpoint
|
10 | 13 | A :ref:`timeout_context` does not contain any :ref:`checkpoints <checkpoint>`.
|
11 | 14 | This makes it pointless, as the timeout can only be triggered by a checkpoint.
|
@@ -70,15 +73,14 @@ _`ASYNC119` : yield-in-cm-in-async-gen
|
70 | 73 | ``yield`` in context manager in async generator is unsafe, the cleanup may be delayed until ``await`` is no longer allowed.
|
71 | 74 | We strongly encourage you to read `PEP 533 <https://peps.python.org/pep-0533/>`_ and use `async with aclosing(...) <https://docs.python.org/3/library/contextlib.html#contextlib.aclosing>`_, or better yet avoid async generators entirely (see `ASYNC900`_ ) in favor of context managers which return an iterable :ref:`channel/stream/queue <channel_stream_queue>`.
|
72 | 75 |
|
73 |
| -_`ASYNC120` : create-task-no-reference |
74 |
| - Calling :func:`asyncio.create_task` without saving the result. A task that isn't referenced elsewhere may get garbage collected at any time, even before it's done. |
75 |
| - Note that this rule won't check whether the variable the result is saved in is susceptible to being garbage-collected itself. See the asyncio documentation for best practices. |
76 |
| - You might consider instead using a :ref:`TaskGroup <taskgroup_nursery>` and calling :meth:`asyncio.TaskGroup.create_task` to avoid this problem, and gain the advantages of structured concurrency with e.g. better cancellation semantics. |
77 |
| - |
78 | 76 |
|
79 | 77 | Blocking sync calls in async functions
|
80 | 78 | ======================================
|
81 | 79 |
|
| 80 | +Our 2xx lint rules warn you to use the async equivalent for slow sync calls which |
| 81 | +would otherwise block the event loop (and therefore cause performance problems, |
| 82 | +or even deadlock). |
| 83 | + |
82 | 84 | .. _httpx.Client: https://www.python-httpx.org/api/#client
|
83 | 85 | .. _httpx.AsyncClient: https://www.python-httpx.org/api/#asyncclient
|
84 | 86 | .. _urllib3: https://github.com/urllib3/urllib3
|
@@ -130,9 +132,27 @@ ASYNC251 : blocking-sleep
|
130 | 132 | Use :func:`trio.sleep`/:func:`anyio.sleep`/:func:`asyncio.sleep`.
|
131 | 133 |
|
132 | 134 |
|
| 135 | +Asyncio-specific rules |
| 136 | +====================== |
| 137 | + |
| 138 | +Asyncio *encourages* structured concurrency, with :obj:`asyncio.TaskGroup`, but does not *require* it. |
| 139 | +We therefore provide some additional lint rules for common problems - although we'd also recommend a |
| 140 | +gradual migration to AnyIO, which is much less error-prone. |
| 141 | + |
| 142 | +_`ASYNC300` : create-task-no-reference |
| 143 | + Calling :func:`asyncio.create_task` without saving the result. A task that isn't referenced elsewhere may get garbage collected at any time, even before it's done. |
| 144 | + Note that this rule won't check whether the variable the result is saved in is susceptible to being garbage-collected itself. See the asyncio documentation for best practices. |
| 145 | + You might consider instead using a :ref:`TaskGroup <taskgroup_nursery>` and calling :meth:`asyncio.TaskGroup.create_task` to avoid this problem, and gain the advantages of structured concurrency with e.g. better cancellation semantics. |
| 146 | + |
| 147 | + |
133 | 148 | Optional rules disabled by default
|
134 | 149 | ==================================
|
135 | 150 |
|
| 151 | +Our 9xx rules check for semantics issues, like 1xx rules, but are disabled by default due |
| 152 | +to the higher volume of warnings. We encourage you to enable them - without guaranteed |
| 153 | +:ref:`checkpoint`\ s timeouts and cancellation can be arbitrarily delayed, and async |
| 154 | +generators are prone to the problems described in :pep:`533`. |
| 155 | + |
136 | 156 | _`ASYNC900` : unsafe-async-generator
|
137 | 157 | Async generator without :func:`@asynccontextmanager <contextlib.asynccontextmanager>` not allowed.
|
138 | 158 | You might want to enable this on a codebase since async generators are inherently unsafe and cleanup logic might not be performed.
|
|
0 commit comments