@@ -101,7 +101,10 @@ def main():
101101 cwd = root ,
102102 ).stdout .splitlines ()
103103 except (subprocess .SubprocessError , FileNotFoundError ):
104- print ("Doesn't seem to be a git repo; pass filenames to format." )
104+ print (
105+ "Doesn't seem to be a git repo; pass filenames to format." ,
106+ file = sys .stderr ,
107+ )
105108 sys .exit (1 )
106109 all_filenames = [
107110 os .path .join (root , f ) for f in all_filenames if _should_format (f )
@@ -110,6 +113,9 @@ def main():
110113 plugin = Plugin .from_filename (file )
111114 for error in sorted (plugin .run ()):
112115 print (f"{ file } :{ error } " )
116+ if plugin .options .autofix :
117+ with open (file , "w" ) as file :
118+ file .write (plugin .module .code )
113119
114120
115121class Plugin :
@@ -122,7 +128,7 @@ def __init__(self, tree: ast.AST, lines: Sequence[str]):
122128 self ._tree = tree
123129 source = "" .join (lines )
124130
125- self ._module : cst .Module = cst_parse_module_native (source )
131+ self .module : cst .Module = cst_parse_module_native (source )
126132
127133 @classmethod
128134 def from_filename (cls , filename : str | PathLike [str ]) -> Plugin : # pragma: no cover
@@ -137,12 +143,14 @@ def from_source(cls, source: str) -> Plugin:
137143 plugin = Plugin .__new__ (cls )
138144 super (Plugin , plugin ).__init__ ()
139145 plugin ._tree = ast .parse (source )
140- plugin ._module = cst_parse_module_native (source )
146+ plugin .module = cst_parse_module_native (source )
141147 return plugin
142148
143149 def run (self ) -> Iterable [Error ]:
144150 yield from Flake8TrioRunner .run (self ._tree , self .options )
145- yield from Flake8TrioRunner_cst (self .options ).run (self ._module )
151+ cst_runner = Flake8TrioRunner_cst (self .options , self .module )
152+ yield from cst_runner .run ()
153+ self .module = cst_runner .module
146154
147155 @staticmethod
148156 def add_options (option_manager : OptionManager | ArgumentParser ):
@@ -157,6 +165,7 @@ def add_options(option_manager: OptionManager | ArgumentParser):
157165 add_argument = functools .partial (
158166 option_manager .add_option , parse_from_config = True
159167 )
168+ add_argument ("--autofix" , action = "store_true" , required = False )
160169
161170 add_argument (
162171 "--no-checkpoint-warning-decorators" ,
0 commit comments