Skip to content

Commit 9997034

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 4e9ae32 commit 9997034

File tree

10 files changed

+136
-86
lines changed

10 files changed

+136
-86
lines changed

swesmith/bug_gen/adapters/ruby.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ def _check_control_flow(self, n):
119119
"""Check for control flow patterns."""
120120
if n.type in ["if", "unless", "if_modifier", "unless_modifier"]:
121121
self._tags.add(CodeProperty.HAS_IF)
122-
if n.type in ["if", "unless"] and any(c.type in ["else", "elsif"] for c in n.children):
122+
if n.type in ["if", "unless"] and any(
123+
c.type in ["else", "elsif"] for c in n.children
124+
):
123125
self._tags.add(CodeProperty.HAS_IF_ELSE)
124126
if n.type in ["while", "until", "for", "while_modifier", "until_modifier"]:
125127
self._tags.add(CodeProperty.HAS_LOOP)

swesmith/bug_gen/procedural/ruby/base.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ def walk(n):
4545
def replace_node(code: str, node, replacement: str) -> str:
4646
"""Replace a tree-sitter node's text via byte offsets."""
4747
code_bytes = code.encode("utf8")
48-
new_bytes = (code_bytes[:node.start_byte]
49-
+ replacement.encode("utf8") + code_bytes[node.end_byte:])
48+
new_bytes = (
49+
code_bytes[: node.start_byte]
50+
+ replacement.encode("utf8")
51+
+ code_bytes[node.end_byte :]
52+
)
5053
return new_bytes.decode("utf8")
5154

5255
def _remove_matching_nodes(
@@ -74,7 +77,9 @@ def collect(n):
7477

7578
source_bytes = code_entity.src_code.encode("utf8")
7679
for node in sorted(removals, key=lambda x: x.start_byte, reverse=True):
77-
source_bytes = source_bytes[:node.start_byte] + source_bytes[node.end_byte:]
80+
source_bytes = (
81+
source_bytes[: node.start_byte] + source_bytes[node.end_byte :]
82+
)
7883

7984
modified_code = source_bytes.decode("utf8")
8085

swesmith/bug_gen/procedural/ruby/control_flow.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ def collect(n):
6767
continue
6868

6969
# Extract the text of the actual statement content
70-
then_content = source_bytes[then_stmts[0].start_byte:then_stmts[-1].end_byte]
71-
else_content = source_bytes[else_stmts[0].start_byte:else_stmts[-1].end_byte]
70+
then_content = source_bytes[
71+
then_stmts[0].start_byte : then_stmts[-1].end_byte
72+
]
73+
else_content = source_bytes[
74+
else_stmts[0].start_byte : else_stmts[-1].end_byte
75+
]
7276

7377
# Determine indentation for each branch by looking at the first
7478
# non-whitespace content position
@@ -78,7 +82,7 @@ def get_indent(node_list):
7882
line_start = source_bytes.rfind(b"\n", 0, start)
7983
if line_start == -1:
8084
return b""
81-
return source_bytes[line_start + 1:start]
85+
return source_bytes[line_start + 1 : start]
8286

8387
then_indent = get_indent(then_stmts)
8488
else_indent = get_indent(else_stmts)
@@ -91,7 +95,7 @@ def reindent(content, from_indent, to_indent):
9195
if i == 0:
9296
result.append(line)
9397
elif line.startswith(from_indent):
94-
result.append(to_indent + line[len(from_indent):])
98+
result.append(to_indent + line[len(from_indent) :])
9599
else:
96100
result.append(line)
97101
return b"\n".join(result)
@@ -101,14 +105,14 @@ def reindent(content, from_indent, to_indent):
101105

102106
# Replace else body first (later in file), then then body
103107
source_bytes = (
104-
source_bytes[:else_stmts[0].start_byte]
108+
source_bytes[: else_stmts[0].start_byte]
105109
+ new_else
106-
+ source_bytes[else_stmts[-1].end_byte:]
110+
+ source_bytes[else_stmts[-1].end_byte :]
107111
)
108112
source_bytes = (
109-
source_bytes[:then_stmts[0].start_byte]
113+
source_bytes[: then_stmts[0].start_byte]
110114
+ new_then
111-
+ source_bytes[then_stmts[-1].end_byte:]
115+
+ source_bytes[then_stmts[-1].end_byte :]
112116
)
113117

114118
return source_bytes.decode("utf8")
@@ -147,9 +151,9 @@ def collect(n):
147151
if child.type == "body_statement":
148152
# Collect top-level statements (skip comments)
149153
statements = [
150-
c for c in child.children
151-
if c.type not in ("comment",)
152-
and c.start_byte != c.end_byte
154+
c
155+
for c in child.children
156+
if c.type not in ("comment",) and c.start_byte != c.end_byte
153157
]
154158
if len(statements) >= 2:
155159
modifications.append(statements)
@@ -171,9 +175,7 @@ def collect(n):
171175
if len(statements) >= 2:
172176
indices[0], indices[1] = indices[1], indices[0]
173177

174-
texts = [
175-
source_bytes[s.start_byte:s.end_byte] for s in statements
176-
]
178+
texts = [source_bytes[s.start_byte : s.end_byte] for s in statements]
177179
shuffled = [texts[i] for i in indices]
178180

179181
first_start = statements[0].start_byte
@@ -186,9 +188,7 @@ def collect(n):
186188
new_content = (b"\n" + indent).join(shuffled)
187189

188190
source_bytes = (
189-
source_bytes[:first_start]
190-
+ new_content
191-
+ source_bytes[last_end:]
191+
source_bytes[:first_start] + new_content + source_bytes[last_end:]
192192
)
193193

194194
return source_bytes.decode("utf8")

swesmith/bug_gen/procedural/ruby/nil_introduction.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55

66
class SafeNavigationRemovalModifier(RubyProceduralModifier):
7-
explanation: str = "A safe navigation operator (&.) has been removed, allowing nil to propagate."
7+
explanation: str = (
8+
"A safe navigation operator (&.) has been removed, allowing nil to propagate."
9+
)
810
name: str = "func_pm_ruby_safe_nav_removal"
911
conditions: list = [CodeProperty.IS_FUNCTION, CodeProperty.HAS_FUNCTION_CALL]
1012

@@ -21,7 +23,9 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
2123
candidates = []
2224
for call in calls:
2325
for child in call.children:
24-
if child.type == "&." or (hasattr(child, 'text') and child.text == b"&."):
26+
if child.type == "&." or (
27+
hasattr(child, "text") and child.text == b"&."
28+
):
2529
candidates.append(child)
2630

2731
if not candidates:
@@ -42,7 +46,9 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
4246

4347

4448
class OrDefaultRemovalModifier(RubyProceduralModifier):
45-
explanation: str = "A fallback default (|| value) has been removed, allowing nil to propagate."
49+
explanation: str = (
50+
"A fallback default (|| value) has been removed, allowing nil to propagate."
51+
)
4652
name: str = "func_pm_ruby_or_default_removal"
4753
conditions: list = [CodeProperty.IS_FUNCTION, CodeProperty.HAS_BINARY_OP]
4854

@@ -58,7 +64,7 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
5864
candidates = []
5965
for node in binaries:
6066
for child in node.children:
61-
if hasattr(child, 'text') and child.text in (b"||", b"or"):
67+
if hasattr(child, "text") and child.text in (b"||", b"or"):
6268
candidates.append(node)
6369
break
6470

@@ -68,7 +74,9 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
6874
target = self.rand.choice(candidates)
6975
# Replace with just the left operand
7076
left = target.children[0]
71-
left_text = code_entity.src_code.encode("utf8")[left.start_byte:left.end_byte].decode("utf8")
77+
left_text = code_entity.src_code.encode("utf8")[
78+
left.start_byte : left.end_byte
79+
].decode("utf8")
7280

7381
modified_code = self.replace_node(code_entity.src_code, target, left_text)
7482

@@ -122,7 +130,9 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
122130

123131
call, receiver = self.rand.choice(candidates)
124132
src_bytes = code_entity.src_code.encode("utf8")
125-
receiver_text = src_bytes[receiver.start_byte:receiver.end_byte].decode("utf8")
133+
receiver_text = src_bytes[receiver.start_byte : receiver.end_byte].decode(
134+
"utf8"
135+
)
126136
modified_code = self.replace_node(code_entity.src_code, call, receiver_text)
127137

128138
valid = self.validate_syntax(code_entity.src_code, modified_code)
@@ -140,7 +150,9 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
140150

141151

142152
class BangMethodStripModifier(RubyProceduralModifier):
143-
explanation: str = "A bang method (!) has been replaced with its non-raising variant."
153+
explanation: str = (
154+
"A bang method (!) has been replaced with its non-raising variant."
155+
)
144156
name: str = "func_pm_ruby_bang_method_strip"
145157
conditions: list = [CodeProperty.IS_FUNCTION, CodeProperty.HAS_FUNCTION_CALL]
146158

@@ -180,7 +192,9 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
180192

181193

182194
class OrEqualsRemovalModifier(RubyProceduralModifier):
183-
explanation: str = "A memoization operator (||=) has been replaced with plain assignment."
195+
explanation: str = (
196+
"A memoization operator (||=) has been replaced with plain assignment."
197+
)
184198
name: str = "func_pm_ruby_or_equals_removal"
185199
conditions: list = [CodeProperty.IS_FUNCTION, CodeProperty.HAS_ASSIGNMENT]
186200

@@ -196,7 +210,7 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
196210
candidates = []
197211
for node in op_assigns:
198212
for child in node.children:
199-
if hasattr(child, 'text') and child.text == b"||=":
213+
if hasattr(child, "text") and child.text == b"||=":
200214
candidates.append(child)
201215

202216
if not candidates:

swesmith/bug_gen/procedural/ruby/operations.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
LOGICAL_OPS = ["&&", "||"]
3131
KEYWORD_LOGICAL_OPS = ["and", "or"]
3232

33-
ALL_BINARY_OPS = set(ARITHMETIC_OPS + BITWISE_OPS + COMPARISON_OPS + LOGICAL_OPS + KEYWORD_LOGICAL_OPS + ["**"])
33+
ALL_BINARY_OPS = set(
34+
ARITHMETIC_OPS
35+
+ BITWISE_OPS
36+
+ COMPARISON_OPS
37+
+ LOGICAL_OPS
38+
+ KEYWORD_LOGICAL_OPS
39+
+ ["**"]
40+
)
3441

3542

3643
def _find_operator_child(node):
@@ -74,11 +81,13 @@ def collect(n):
7481
return None
7582

7683
source_bytes = code_entity.src_code.encode("utf8")
77-
for op_node, new_op in sorted(modifications, key=lambda x: x[0].start_byte, reverse=True):
84+
for op_node, new_op in sorted(
85+
modifications, key=lambda x: x[0].start_byte, reverse=True
86+
):
7887
source_bytes = (
79-
source_bytes[:op_node.start_byte]
88+
source_bytes[: op_node.start_byte]
8089
+ new_op.encode("utf8")
81-
+ source_bytes[op_node.end_byte:]
90+
+ source_bytes[op_node.end_byte :]
8291
)
8392

8493
modified_code = source_bytes.decode("utf8")
@@ -137,11 +146,13 @@ def collect(n):
137146
return None
138147

139148
source_bytes = code_entity.src_code.encode("utf8")
140-
for op_node, new_op in sorted(modifications, key=lambda x: x[0].start_byte, reverse=True):
149+
for op_node, new_op in sorted(
150+
modifications, key=lambda x: x[0].start_byte, reverse=True
151+
):
141152
source_bytes = (
142-
source_bytes[:op_node.start_byte]
153+
source_bytes[: op_node.start_byte]
143154
+ new_op.encode("utf8")
144-
+ source_bytes[op_node.end_byte:]
155+
+ source_bytes[op_node.end_byte :]
145156
)
146157

147158
modified_code = source_bytes.decode("utf8")
@@ -199,15 +210,15 @@ def collect(n):
199210
for expr, left, op, right in sorted(
200211
modifications, key=lambda x: x[0].start_byte, reverse=True
201212
):
202-
left_text = source_bytes[left.start_byte:left.end_byte]
203-
op_text = source_bytes[op.start_byte:op.end_byte]
204-
right_text = source_bytes[right.start_byte:right.end_byte]
213+
left_text = source_bytes[left.start_byte : left.end_byte]
214+
op_text = source_bytes[op.start_byte : op.end_byte]
215+
right_text = source_bytes[right.start_byte : right.end_byte]
205216

206217
new_expr = right_text + b" " + op_text + b" " + left_text
207218
source_bytes = (
208-
source_bytes[:expr.start_byte]
219+
source_bytes[: expr.start_byte]
209220
+ new_expr
210-
+ source_bytes[expr.end_byte:]
221+
+ source_bytes[expr.end_byte :]
211222
)
212223

213224
modified_code = source_bytes.decode("utf8")
@@ -273,11 +284,13 @@ def collect(n):
273284
for expr, replacement in sorted(
274285
modifications, key=lambda x: x[0].start_byte, reverse=True
275286
):
276-
replacement_text = source_bytes[replacement.start_byte:replacement.end_byte]
287+
replacement_text = source_bytes[
288+
replacement.start_byte : replacement.end_byte
289+
]
277290
source_bytes = (
278-
source_bytes[:expr.start_byte]
291+
source_bytes[: expr.start_byte]
279292
+ replacement_text
280-
+ source_bytes[expr.end_byte:]
293+
+ source_bytes[expr.end_byte :]
281294
)
282295

283296
modified_code = source_bytes.decode("utf8")
@@ -337,9 +350,9 @@ def collect(n):
337350
modifications, key=lambda x: x[0].start_byte, reverse=True
338351
):
339352
source_bytes = (
340-
source_bytes[:const_node.start_byte]
353+
source_bytes[: const_node.start_byte]
341354
+ new_value.encode("utf8")
342-
+ source_bytes[const_node.end_byte:]
355+
+ source_bytes[const_node.end_byte :]
343356
)
344357

345358
modified_code = source_bytes.decode("utf8")

swesmith/bug_gen/procedural/ruby/ruby_specific.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
2626
text = node.text.decode("utf8")
2727
# Strip leading : to get the name
2828
name = text[1:]
29-
if re.match(r'^[a-zA-Z_]\w*[?!]?$', name):
29+
if re.match(r"^[a-zA-Z_]\w*[?!]?$", name):
3030
candidates.append(("symbol_to_string", node, name))
3131

3232
# Find simple strings ("foo") — no interpolation, alphanumeric
@@ -36,7 +36,7 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
3636
content_nodes = [c for c in node.children if c.type == "string_content"]
3737
if len(content_nodes) == 1:
3838
content = content_nodes[0].text.decode("utf8")
39-
if re.match(r'^[a-zA-Z_]\w*[?!]?$', content):
39+
if re.match(r"^[a-zA-Z_]\w*[?!]?$", content):
4040
candidates.append(("string_to_symbol", node, content))
4141

4242
if not candidates:
@@ -77,7 +77,8 @@ def modify(self, code_entity: CodeEntity) -> BugRewrite:
7777
# Collect two types of candidates
7878
block_params = self.find_nodes(tree.root_node, "block_parameters")
7979
yield_nodes = [
80-
n for n in self.find_nodes(tree.root_node, "yield")
80+
n
81+
for n in self.find_nodes(tree.root_node, "yield")
8182
if any(c.type == "argument_list" for c in n.children)
8283
]
8384

swesmith/profiles/ruby.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ def parse_log_ruby_test(log: str) -> dict[str, str]:
7373
current_class = stripped[:-1]
7474
continue
7575
# test-unit verbose: " test_name: .: (0.001234)"
76-
if current_class and ".: (" in stripped or "F: (" in stripped or "E: (" in stripped:
76+
if (
77+
current_class
78+
and ".: (" in stripped
79+
or "F: (" in stripped
80+
or "E: (" in stripped
81+
):
7782
match = re.match(r"^(\S+):\s+([.FE]):\s+\(", stripped)
7883
if match:
79-
test_name = f"{current_class}#{ match.group(1)}"
84+
test_name = f"{current_class}#{match.group(1)}"
8085
status_char = match.group(2)
8186
if status_char == ".":
8287
test_status_map[test_name] = TestStatus.PASSED.value

0 commit comments

Comments
 (0)