Skip to content

Commit 249189e

Browse files
committed
python refs vs refs
1 parent 697ea8b commit 249189e

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

tests/restyle.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import subprocess
88
import contextlib
99

10-
from dataclasses import dataclass
10+
from dataclasses import dataclass, fields
11+
from dataclasses import field as dataclass_field
12+
1113

1214
GIT_ROOT = pathlib.Path(
1315
subprocess.check_output(
@@ -89,11 +91,11 @@ class Changed:
8991

9092
@dataclass
9193
class Context:
92-
append_hunk: bool
93-
deleted: bool
94-
file: str
95-
hunk: list[str]
96-
markers: list[str]
94+
append_hunk: bool = False
95+
deleted: bool = False
96+
file: str = ""
97+
hunk: list[str] = dataclass_field(default_factory=list)
98+
markers: list[str] = dataclass_field(default_factory=list)
9799

98100

99101
# naive git-diff parser for clang-format aftercare
@@ -108,28 +110,24 @@ def changed_files():
108110
universal_newlines=True,
109111
)
110112

111-
def reset_context(line):
113+
def reset_context(ctx):
114+
other = Context()
115+
for field in fields(other):
116+
setattr(ctx, field.name, getattr(other, field.name))
117+
118+
def reset_with_line(ctx, line):
119+
reset_context(ctx)
112120
if line:
113-
hunk = [line]
114-
else:
115-
hunk = []
116-
117-
return Context(
118-
append_hunk=False,
119-
deleted=False,
120-
file="",
121-
hunk=hunk,
122-
markers=[],
123-
)
121+
ctx.hunk = [line]
124122

125123
def pop(out, context, line):
126124
if ctx.file and ctx.hunk and ctx.markers:
127125
out.append(Changed(ctx.file, "\n".join(ctx.hunk), ", ".join(ctx.markers)))
128126

129-
context = reset_context(line)
127+
reset_with_line(context, line)
130128

131129
out = []
132-
ctx = reset_context(None)
130+
ctx = Context()
133131

134132
for line in proc.stdout.split("\n"):
135133
# '--- a/path/to/changed/file' most likely
@@ -180,6 +178,7 @@ def warning_changed(changed: Changed):
180178
SUMMARY_PATH = pathlib.Path(os.environ.get("GITHUB_STEP_SUMMARY", os.devnull))
181179
SUMMARY_OUTPUT = SUMMARY_PATH.open("a")
182180

181+
183182
def summary_diff(changed: Changed):
184183
with contextlib.redirect_stdout(SUMMARY_OUTPUT):
185184
print(f"# {changed.file} (suggested change)")

0 commit comments

Comments
 (0)