Skip to content

Commit 0786f9d

Browse files
authored
Merge pull request #55 from jakkdl/trio102_ignore_contextmanager
trio102 now ignores contextmanager
2 parents c137af3 + 7132d08 commit 0786f9d

File tree

2 files changed

+8
-29
lines changed

2 files changed

+8
-29
lines changed

flake8_trio.py

+5-28
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ def __eq__(self, other: Any) -> bool:
135135
"move_on_at",
136136
"CancelScope",
137137
)
138-
context_manager_names = (
139-
"contextmanager",
140-
"asynccontextmanager",
141-
)
142138

143139

144140
class Flake8TrioVisitor(ast.NodeVisitor):
@@ -312,8 +308,7 @@ def visit_FunctionDef(self, node: Union[ast.FunctionDef, ast.AsyncFunctionDef]):
312308
outer = self.get_state()
313309
self.set_state(self.defaults, copy=True)
314310

315-
# check for @<context_manager_name> and @<library>.<context_manager_name>
316-
if has_decorator(node.decorator_list, *context_manager_names):
311+
if has_decorator(node.decorator_list, "contextmanager", "asynccontextmanager"):
317312
self._safe_decorator = True
318313

319314
self.generic_visit(node)
@@ -509,21 +504,16 @@ def __init__(self, *args: Any, **kwargs: Any):
509504
super().__init__(*args, **kwargs)
510505
self._critical_scope: Optional[Statement] = None
511506
self._trio_context_managers: List[Visitor102.TrioScope] = []
512-
self._safe_decorator = False
513507

514-
# if we're inside a finally, and not inside a context_manager, and we're not
515-
# inside a scope that doesn't have both a timeout and shield
508+
# if we're inside a finally, and we're not inside a scope that doesn't have
509+
# both a timeout and shield
516510
def visit_Await(
517511
self,
518512
node: Union[ast.Await, ast.AsyncFor, ast.AsyncWith],
519513
visit_children: bool = True,
520514
):
521-
if (
522-
self._critical_scope is not None
523-
and not self._safe_decorator
524-
and not any(
525-
cm.has_timeout and cm.shielded for cm in self._trio_context_managers
526-
)
515+
if self._critical_scope is not None and not any(
516+
cm.has_timeout and cm.shielded for cm in self._trio_context_managers
527517
):
528518
self.error("TRIO102", node, self._critical_scope)
529519
if visit_children:
@@ -560,19 +550,6 @@ def visit_AsyncWith(self, node: ast.AsyncWith):
560550
self.visit_Await(node, visit_children=False)
561551
self.visit_With(node)
562552

563-
def visit_FunctionDef(self, node: Union[ast.FunctionDef, ast.AsyncFunctionDef]):
564-
outer = self.get_state("_safe_decorator")
565-
566-
# check for @<context_manager_name> and @<library>.<context_manager_name>
567-
if has_decorator(node.decorator_list, *context_manager_names):
568-
self._safe_decorator = True
569-
570-
self.generic_visit(node)
571-
572-
self.set_state(outer)
573-
574-
visit_AsyncFunctionDef = visit_FunctionDef
575-
576553
def critical_visit(
577554
self,
578555
node: Union[ast.ExceptHandler, Iterable[ast.AST]],

tests/trio102.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,14 @@ async def foo():
136136
await foo() # error: 12, Statement("try/finally", lineno-3)
137137

138138

139+
# change of functionality, no longer treated as safe
140+
# https://github.com/Zac-HD/flake8-trio/issues/54
139141
@asynccontextmanager
140142
async def foo2():
141143
try:
142144
yield 1
143145
finally:
144-
await foo() # safe
146+
await foo() # error: 8, Statement("try/finally", lineno-3)
145147

146148

147149
async def foo3():

0 commit comments

Comments
 (0)