1111
1212import ast
1313import 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-
2019Error = Tuple [int , int , str , Type [Any ]]
20+
21+
2122checkpoint_node_types = (ast .Await , ast .AsyncFor , ast .AsyncWith )
2223cancel_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
7984class 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
0 commit comments