8000 Tweak message, variable name · python-trio/flake8-async@70da625 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70da625

Browse files
committed
Tweak message, variable name
1 parent a582b01 commit 70da625

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
*[CalVer, YY.month.patch](https://calver.org/)*
33

44
## 22.7.3
5-
Added:
6-
- **TRIO102** `await` in `finally` must have a cancel scope with shielding.
5+
- Added TRIO102 check for unsafe checkpoints inside `finally:` blocks
76

87
## 22.7.2
98
- Avoid `TRIO100` false-alarms on cancel scopes containing `async for` or `async with`.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ pip install flake8-trio
2222
context does not contain any `await` statements. This makes it pointless, as
2323
the timeout can only be triggered by a checkpoint.
2424
- **TRIO101** `yield` inside a nursery or cancel scope is only safe when implementing a context manager - otherwise, it breaks exception handling.
25-
- **TRIO102** `await` in `finally` must have a cancel scope with shielding.
25+
- **TRIO102** it's unsafe to await inside `finally:` unless you use a shielded
26+
cancel scope with a timeout"

flake8_trio.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, node: ast.Call, funcname: str, packagename: str):
4343
self.packagename = packagename
4444
self.variable_name: Optional[str] = None
4545
self.shielded: bool = False
46-
self.timeout: bool = False
46+
self.has_timeout: bool = False
4747

4848
if self.funcname == "CancelScope":
4949
for kw in node.keywords:
@@ -52,9 +52,9 @@ def __init__(self, node: ast.Call, funcname: str, packagename: str):
5252
self.shielded = kw.value.value
5353
# sets to True even if timeout is explicitly set to inf
5454
if kw.arg == "deadline":
55-
self.timeout = True
55+
self.has_timeout = True
5656
else:
57-
self.timeout = True
57+
self.has_timeout = True
5858

5959
def __str__(self):
6060
# Not supporting other ways of importing trio
@@ -89,7 +89,7 @@ class Visitor102(ast.NodeVisitor):
8989
def __init__(self) -> None:
9090
super().__init__()
9191
self.problems: List[Error] = []
92-
self._inside_finally: Optional[ast.Try] = None
92+
self._inside_finally: bool = False
9393
self._scopes: List[TrioScope] = []
9494
self._context_manager = False
9595

@@ -169,7 +169,7 @@ def visit_Try(self, node: ast.Try) -> None:
169169
outer_scopes = self._scopes
170170

171171
self._scopes = []
172-
self._inside_finally = node
172+
self._inside_finally = True
173173

174174
for item in node.finalbody:
175175
self.visit(item)
@@ -180,12 +180,12 @@ def visit_Try(self, node: ast.Try) -> None:
180180
def check_for_trio102(self, node: Union[ast.Await, ast.AsyncFor, ast.AsyncWith]):
181181
# if we're inside a finally, and not inside a context_manager, and we're not
182182
# inside a scope that doesn't have both a timeout and shield
183-
if self._inside_finally is not None and not self._context_manager:
184-
for scope in self._scopes:
185-
if scope.timeout and scope.shielded:
186-
break
187-
else:
188-
self.problems.append(make_error(TRIO102, node.lineno, node.col_offset))
183+
if (
184+
self._inside_finally
185+
and not self._context_manager
186+
and not any(scope.has_timeout and scope.shielded for scope in self._scopes)
187+
):
188+
self.problems.append(make_error(TRIO102, node.lineno, node.col_offset))
189189

190190

191191
class Visitor(ast.NodeVisitor):
@@ -277,4 +277,4 @@ def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]:
277277

278278
TRIO100 = "TRIO100: {} context contains no checkpoints, add `await trio.sleep(0)`"
279279
TRIO101 = "TRIO101: yield inside a nursery or cancel scope is only safe when implementing a context manager - otherwise, it breaks exception handling"
280-
TRIO102 = "TRIO102: await in finally without a cancel scope and shielding"
280+
TRIO102 = "TRIO102: it's unsafe to await inside `finally:` unless you use a shielded cancel scope with a timeout"

0 commit comments

Comments
 (0)
0