11
11
12
12
import ast
13
13
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
15
15
16
16
# CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1"
17
17
__version__ = "22.7.6"
18
18
19
-
20
19
Error = Tuple [int , int , str , Type [Any ]]
20
+
21
+
21
22
checkpoint_node_types = (ast .Await , ast .AsyncFor , ast .AsyncWith )
22
23
cancel_scope_names = (
23
24
"fail_after" ,
@@ -43,7 +44,7 @@ def __init__(self):
43
44
self .suppress_errors = False
44
45
45
46
@classmethod
46
- def run (cls , tree : ast .AST ) -> Generator [Error , None , None ]:
47
+ def run (cls , tree : ast .AST ) -> Iterable [Error ]:
47
48
visitor = cls ()
48
49
visitor .visit (tree )
49
50
yield from visitor .problems
@@ -75,6 +76,10 @@ def set_state(self, attrs: Dict[str, Any]):
75
76
for attr , value in attrs .items ():
76
77
setattr (self , attr , value )
77
78
79
+ def walk (self , * body : ast .AST ) -> Iterable [ast .AST ]:
80
+ for b in body :
81
+ yield from ast .walk (b )
82
+
78
83
79
84
class TrioScope :
80
85
def __init__ (self , node : ast .Call , funcname : str , packagename : str ):
@@ -609,9 +614,9 @@ def visit_Yield(self, node: ast.Yield):
609
614
def visit_Try (self , node : ast .Try ):
610
615
# check worst case try exception
611
616
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 )
615
620
break
616
621
617
622
# check try body
@@ -758,7 +763,7 @@ def from_filename(cls, filename: str) -> "Plugin":
758
763
source = f .read ()
759
764
return cls (ast .parse (source ))
760
765
761
- def run (self ) -> Generator [ Tuple [ int , int , str , Type [ Any ]], None , None ]:
766
+ def run (self ) -> Iterable [ Error ]:
762
767
for v in Flake8TrioVisitor .__subclasses__ ():
763
768
yield from v .run (self ._tree )
764
769
0 commit comments