22
33import argparse
44import ast
5+ import dataclasses
56import os
67import pathlib
78import re
3031)
3132
3233
34+ @dataclasses .dataclass
35+ class Config :
36+ line_length : int
37+
38+
3339def name_lineno_coloffset_iterable (
3440 tokens : Iterable [Token ],
3541) -> list [tuple [str , int , int ]]:
@@ -255,7 +261,7 @@ def visit_function_def(
255261
256262def auto_walrus (
257263 content : str ,
258- line_length : int ,
264+ config : Config ,
259265) -> str | None :
260266 lines = content .splitlines ()
261267 try :
@@ -290,7 +296,7 @@ def auto_walrus(
290296 line_with_walrus = left_bit + replace + right_bit
291297 else :
292298 line_with_walrus = left_bit + "(" + replace + ")" + right_bit
293- if len (line_with_walrus ) > line_length :
299+ if len (line_with_walrus ) > config . line_length :
294300 # don't rewrite if it would split over multiple lines
295301 continue
296302 # replace assignment
@@ -373,6 +379,7 @@ def main(argv: Sequence[str] | None = None) -> int: # pragma: no cover
373379
374380 ret = 0
375381
382+ config = Config (line_length = args .line_length )
376383 for path in paths :
377384 if path .is_file ():
378385 filepaths = iter ((path ,))
@@ -392,10 +399,7 @@ def main(argv: Sequence[str] | None = None) -> int: # pragma: no cover
392399 content = fd .read ()
393400 except UnicodeDecodeError :
394401 continue
395- new_content = auto_walrus (
396- content ,
397- line_length = args .line_length ,
398- )
402+ new_content = auto_walrus (content , config )
399403 if new_content is not None and content != new_content :
400404 sys .stdout .write (f"Rewriting { filepath } \n " )
401405 with open (filepath , "w" , encoding = "utf-8" ) as fd :
0 commit comments