Skip to content

Commit f846025

Browse files
committed
Wrap configuration in dataclass
1 parent efb2d5f commit f846025

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

auto_walrus.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import ast
5+
import dataclasses
56
import os
67
import pathlib
78
import re
@@ -30,6 +31,11 @@
3031
)
3132

3233

34+
@dataclasses.dataclass
35+
class Config:
36+
line_length: int
37+
38+
3339
def name_lineno_coloffset_iterable(
3440
tokens: Iterable[Token],
3541
) -> list[tuple[str, int, int]]:
@@ -255,7 +261,7 @@ def visit_function_def(
255261

256262
def 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:

tests/main_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
],
9090
)
9191
def test_rewrite(src: str, expected: str) -> None:
92-
ret = auto_walrus(src, 88)
92+
ret = auto_walrus(src, Config(line_length=88))
9393
assert ret == expected
9494

9595

@@ -125,7 +125,7 @@ def test_rewrite(src: str, expected: str) -> None:
125125
],
126126
)
127127
def test_noop(src: str) -> None:
128-
ret = auto_walrus(src, 40)
128+
ret = auto_walrus(src, Config(line_length=40))
129129
assert ret is None
130130

131131

0 commit comments

Comments
 (0)