|
| 1 | +"""Test file for ASYNC128 task-status-never-started.""" |
| 2 | + |
| 3 | +# ASYNC128 does not care about the imported library, so will raise errors regardless |
| 4 | +# of trio/anyio/asyncio |
| 5 | + |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import trio |
| 9 | +from trio import TaskStatus |
| 10 | + |
| 11 | + |
| 12 | +async def never_started( # error: 0, "never_started", "task_status" |
| 13 | + task_status=trio.TASK_STATUS_IGNORED, |
| 14 | +): |
| 15 | + await trio.sleep(1) |
| 16 | + |
| 17 | + |
| 18 | +async def started(task_status=trio.TASK_STATUS_IGNORED): |
| 19 | + task_status.started() |
| 20 | + |
| 21 | + |
| 22 | +async def started_with_value(task_status=trio.TASK_STATUS_IGNORED): |
| 23 | + task_status.started(7) |
| 24 | + |
| 25 | + |
| 26 | +async def no_task_status(): |
| 27 | + await trio.sleep(1) |
| 28 | + |
| 29 | + |
| 30 | +# a conditional call counts - no attempt is made to check all code paths |
| 31 | +async def conditional_start(condition: bool, *, task_status): |
| 32 | + if condition: |
| 33 | + task_status.started() |
| 34 | + |
| 35 | + |
| 36 | +# ... even if the call can never actually execute |
| 37 | +async def unreachable_start(task_status): |
| 38 | + for _ in range(0): |
| 39 | + task_status.started() |
| 40 | + |
| 41 | + |
| 42 | +# annotated parameters trigger regardless of their name |
| 43 | +async def annotated(status: TaskStatus[int]): # error: 0, "annotated", "status" |
| 44 | + await trio.sleep(1) |
| 45 | + |
| 46 | + |
| 47 | +async def annotated_bare(status: TaskStatus): # error: 0, "annotated_bare", "status" |
| 48 | + await trio.sleep(1) |
| 49 | + |
| 50 | + |
| 51 | +async def annotated_qualified( # error: 0, "annotated_qualified", "status" |
| 52 | + status: trio.TaskStatus[int], |
| 53 | +): |
| 54 | + await trio.sleep(1) |
| 55 | + |
| 56 | + |
| 57 | +async def annotated_ok(status: TaskStatus[int]): |
| 58 | + status.started(5) |
| 59 | + |
| 60 | + |
| 61 | +# a `task_status` parameter that's positional-only can't be startable, but an |
| 62 | +# explicit annotation still counts |
| 63 | +async def posonly_ignored(task_status, /): |
| 64 | + await trio.sleep(1) |
| 65 | + |
| 66 | + |
| 67 | +async def posonly_annotated( # error: 0, "posonly_annotated", "status" |
| 68 | + status: TaskStatus[int], / |
| 69 | +): |
| 70 | + await trio.sleep(1) |
| 71 | + |
| 72 | + |
| 73 | +async def starargs_ignored(*task_status, **kwargs): |
| 74 | + await trio.sleep(1) |
| 75 | + |
| 76 | + |
| 77 | +# passing `task_status` to a helper does not count, even if the helper calls |
| 78 | +# `started()` for you. The check is intentionally strict, silence it with `noqa` |
| 79 | +# if you're intentionally proxying it. |
| 80 | +async def helper(fn: Any, task_status): # error: 0, "helper", "task_status" |
| 81 | + await fn(task_status=task_status) |
| 82 | + |
| 83 | + |
| 84 | +# aliasing does not count either |
| 85 | +async def aliased(task_status): # error: 0, "aliased", "task_status" |
| 86 | + ts = task_status |
| 87 | + ts.started() |
| 88 | + |
| 89 | + |
| 90 | +# accessing `.started` without calling it does not count |
| 91 | +async def not_called(task_status): # error: 0, "not_called", "task_status" |
| 92 | + task_status.started |
| 93 | + |
| 94 | + |
| 95 | +# the call must be on the parameter itself, not e.g. an attribute by the same name |
| 96 | +class AttributeStatus: |
| 97 | + task_status: TaskStatus[None] |
| 98 | + |
| 99 | + async def relay(self, task_status): # error: 4, "relay", "task_status" |
| 100 | + self.task_status.started() |
| 101 | + |
| 102 | + |
| 103 | +# calls in nested functions closing over the parameter do count |
| 104 | +async def closure(task_status=trio.TASK_STATUS_IGNORED): |
| 105 | + def inner(): |
| 106 | + task_status.started() |
| 107 | + |
| 108 | + inner() |
| 109 | + |
| 110 | + |
| 111 | +async def lambda_closure(task_status=trio.TASK_STATUS_IGNORED): |
| 112 | + fn = lambda: task_status.started() |
| 113 | + fn() |
| 114 | + |
| 115 | + |
| 116 | +# ... but not if the nested function rebinds the name; it is instead |
| 117 | +# checked on its own |
| 118 | +async def shadowed(task_status): # error: 0, "shadowed", "task_status" |
| 119 | + async def inner(task_status=trio.TASK_STATUS_IGNORED): |
| 120 | + task_status.started() |
| 121 | + |
| 122 | + await inner() |
| 123 | + |
| 124 | + |
| 125 | +async def shadowed_by_lambda( # error: 0, "shadowed_by_lambda", "task_status" |
| 126 | + task_status, |
| 127 | +): |
| 128 | + fn = lambda task_status: task_status.started() |
| 129 | + fn(None) |
| 130 | + |
| 131 | + |
| 132 | +async def shadowed_by_vararg( # error: 0, "shadowed_by_vararg", "task_status" |
| 133 | + task_status, |
| 134 | +): |
| 135 | + def inner(*task_status: Any): |
| 136 | + task_status[0].started() |
| 137 | + |
| 138 | + inner(None) |
| 139 | + |
| 140 | + |
| 141 | +async def nested_never_started(): |
| 142 | + async def inner( # error: 4, "inner", "task_status" |
| 143 | + task_status=trio.TASK_STATUS_IGNORED, |
| 144 | + ): |
| 145 | + await trio.sleep(1) |
| 146 | + |
| 147 | + await inner() |
| 148 | + |
| 149 | + |
| 150 | +# stub bodies don't error, e.g. overloads, protocols, and abstract methods |
| 151 | +class StartableProtocol: |
| 152 | + async def ellipsis_body(self, *, task_status: TaskStatus[None]): ... |
| 153 | + |
| 154 | + async def pass_body(self, *, task_status: TaskStatus[None]): |
| 155 | + pass |
| 156 | + |
| 157 | + async def docstring_body(self, *, task_status: TaskStatus[None]): |
| 158 | + """It has a docstring.""" |
| 159 | + |
| 160 | + async def raise_body(self, *, task_status: TaskStatus[None]): |
| 161 | + raise NotImplementedError |
| 162 | + |
| 163 | + async def method_never_started( # error: 4, "method_never_started", "task_status" |
| 164 | + self, task_status=trio.TASK_STATUS_IGNORED |
| 165 | + ): |
| 166 | + await trio.sleep(1) |
| 167 | + |
| 168 | + |
| 169 | +# sync functions are not checked - they cannot be passed to `.start()` |
| 170 | +def sync_fn(task_status): |
| 171 | + return None |
0 commit comments