Skip to content

Commit c2621a4

Browse files
committed
cleaned up ugly loop by adding a vararg walk wrapper, replaced instances of Generator with Iterable
1 parent 7acf4d6 commit c2621a4

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

flake8_trio.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
import ast
1313
import tokenize
14-
from typing import Any, Dict, Generator, Iterable, List, Optional, Tuple, Type, Union
14+
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union
1515

1616
# CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1"
1717
__version__ = "22.7.6"
1818

19-
2019
Error = Tuple[int, int, str, Type[Any]]
20+
21+
2122
checkpoint_node_types = (ast.Await, ast.AsyncFor, ast.AsyncWith)
2223
cancel_scope_names = (
2324
"fail_after",
@@ -43,7 +44,7 @@ def __init__(self):
4344
self.suppress_errors = False
4445

4546
@classmethod
46-
def run(cls, tree: ast.AST) -> Generator[Error, None, None]:
47+
def run(cls, tree: ast.AST) -> Iterable[Error]:
4748
visitor = cls()
4849
visitor.visit(tree)
4950
yield from visitor.problems
@@ -75,6 +76,10 @@ def set_state(self, attrs: Dict[str, Any]):
7576
for attr, value in attrs.items():
7677
setattr(self, attr, value)
7778

79+
def walk(self, *body: ast.AST) -> Iterable[ast.AST]:
80+
for b in body:
81+
yield from ast.walk(b)
82+
7883

7984
class TrioScope:
8085
def __init__(self, node: ast.Call, funcname: str, packagename: str):
@@ -609,9 +614,9 @@ def visit_Yield(self, node: ast.Yield):
609614
def visit_Try(self, node: ast.Try):
610615
# check worst case try exception
611616
body_always_checkpoint = self.always_checkpoint
612-
for n in (b for body in node.body for b in ast.walk(body)):
613-
if isinstance(n, ast.Yield):
614-
body_always_checkpoint = ("yield", n.lineno)
617+
for inner_node in self.walk(*node.body):
618+
if isinstance(inner_node, ast.Yield):
619+
body_always_checkpoint = ("yield", inner_node.lineno)
615620
break
616621

617622
# check try body
@@ -758,7 +763,7 @@ def from_filename(cls, filename: str) -> "Plugin":
758763
source = f.read()
759764
return cls(ast.parse(source))
760765

761-
def run(self) -> Generator[Tuple[int, int, str, Type[Any]], None, None]:
766+
def run(self) -> Iterable[Error]:
762767
for v in Flake8TrioVisitor.__subclasses__():
763768
yield from v.run(self._tree)
764769

tests/trio108.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,22 @@ async def foo_try_6():
328328

329329

330330
async def foo_try_7():
331+
await foo()
331332
try:
332-
await foo()
333333
yield
334+
await foo()
334335
except ValueError:
335336
await foo()
336337
yield
337338
await foo()
338-
yield # error: 4, yield, yield, $lineno-5
339+
except SyntaxError:
340+
yield # error: 8, yield, yield, $lineno-7
341+
await foo()
342+
finally:
343+
pass
344+
# If the try raises an exception without checkpointing, and it's not caught
345+
# by any of the excepts, jumping straight to the finally.
346+
yield # error: 4, yield, yield, $lineno-13
339347

340348

341349
# if

0 commit comments

Comments
 (0)