Skip to content

Commit 45cbe43

Browse files
committed
fix: resolve SonarQube issues (ReDoS, float equality, concatenated strings)
1 parent b0fcba0 commit 45cbe43

6 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/treemapper/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _validate_budget(budget: int | None) -> None:
3434

3535

3636
def _validate_alpha(alpha: float) -> None:
37-
if not (0 < alpha < 1):
37+
if alpha <= 0 or alpha >= 1:
3838
_exit_error(f"--alpha must be between 0 and 1 (exclusive), got {alpha}")
3939

4040

src/treemapper/diffctx/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,12 @@ def build_diff_context(
306306
def _validate_inputs(root_dir: Path, alpha: float, tau: float, budget_tokens: int | None) -> None:
307307
if not is_git_repo(root_dir):
308308
raise GitError(f"'{root_dir}' is not a git repository")
309-
if not (0.0 < alpha < 1.0):
309+
if alpha <= 0.0 or alpha >= 1.0:
310310
raise ValueError(f"alpha must be in (0, 1), got {alpha}")
311311
if tau < 0.0:
312312
raise ValueError(f"tau must be >= 0, got {tau}")
313-
if tau == 0.0:
314-
logging.warning("tau=0 disables adaptive stopping; budget will be fully consumed")
313+
if tau < 1e-15:
314+
logging.warning("tau0 disables adaptive stopping; budget will be fully consumed")
315315
if budget_tokens is not None and budget_tokens <= 0:
316316
raise ValueError(f"budget_tokens must be > 0, got {budget_tokens}")
317317

@@ -352,7 +352,7 @@ def _coherence_post_pass(
352352
else:
353353
sig_candidates = [f for f in candidates if "_signature" in f.kind]
354354
full_candidates = [f for f in candidates if "_signature" not in f.kind]
355-
pick = sig_candidates[0] if sig_candidates else (full_candidates[0] if full_candidates else None)
355+
pick = next(iter(sig_candidates or full_candidates), None)
356356
if pick and pick.token_count <= remaining and pick.id not in selected_ids:
357357
added.append(pick)
358358
selected_ids.add(pick.id)

src/treemapper/diffctx/utility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
_CONCEPT_RE = re.compile(r"[A-Za-z_]\w*")
1717
_CALL_RE = re.compile(r"(\w+)\s*\(")
18-
_TYPE_REF_RE = re.compile(r"(?::\s*|->)\s*([A-Z]\w+)")
18+
_TYPE_REF_RE = re.compile(r"(?::|->)\s*([A-Z]\w+)")
1919
_CLOSURE_MIN_EDGE_WEIGHT = 0.5
2020

2121

tests/test_diffctx_integrity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,9 @@ def test_cicd_does_not_create_spurious_edges_from_greedy_prefix(self, tmp_path:
674674
assert any(
675675
"test.py" == Path(p).name for p in all_paths
676676
), "test.py should be in context — exact match for CI/CD script reference"
677-
assert "TESTING_UTILS_MARKER_XYZZY" not in all_content, (
678-
"testing_utils.py should NOT be in context — " "'test' prefix without separator must not match"
679-
)
677+
assert (
678+
"TESTING_UTILS_MARKER_XYZZY" not in all_content
679+
), "testing_utils.py should NOT be in context — 'test' prefix without separator must not match"
680680
assert "APP_MARKER_QWERTY" not in all_content, "app.py should NOT be in context — completely unrelated file"
681681

682682

tests/test_fragment_integrity.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def _assert_all_fragments_bracket_balanced(frags):
1616
if frag.kind in _CONTAINER_HEADER_KINDS:
1717
continue
1818
balance = _compute_bracket_balance(frag.content)
19-
assert balance == 0, f"Unbalanced brackets (balance={balance}) in fragment {frag.id}: " f"{frag.content[:80]}"
19+
assert balance == 0, f"Unbalanced brackets (balance={balance}) in fragment {frag.id}: {frag.content[:80]}"
2020

2121

2222
def _assert_fragments_span_file(frags, total_file_lines):
@@ -25,9 +25,9 @@ def _assert_fragments_span_file(frags, total_file_lines):
2525
min_start = min(f.id.start_line for f in frags)
2626
max_end = max(f.id.end_line for f in frags)
2727
assert min_start == 1, f"First fragment should start at line 1, starts at {min_start}"
28-
assert max_end >= total_file_lines - 1, (
29-
f"Last fragment should reach near end of file " f"(line {total_file_lines}), ends at {max_end}"
30-
)
28+
assert (
29+
max_end >= total_file_lines - 1
30+
), f"Last fragment should reach near end of file (line {total_file_lines}), ends at {max_end}"
3131

3232

3333
class TestBracketBalancedFragments:
@@ -235,7 +235,7 @@ def test_large_text_non_final_fragments_end_at_sentence_boundary(self):
235235
content = frag.content.rstrip()
236236
if content:
237237
last_char = content[-1]
238-
assert last_char in '.!?"', f"Non-final fragment should end at sentence boundary: " f"...{content[-50:]}"
238+
assert last_char in '.!?"', f"Non-final fragment should end at sentence boundary: ...{content[-50:]}"
239239

240240

241241
class TestEdgeCases:
@@ -342,9 +342,9 @@ def _assert_fragment_lines_match_content(self, frags, content):
342342
all_lines = content.splitlines()
343343
for frag in frags:
344344
assert 1 <= frag.start_line <= len(all_lines), f"start_line {frag.start_line} out of range [1, {len(all_lines)}]"
345-
assert frag.start_line <= frag.end_line <= len(all_lines), (
346-
f"end_line {frag.end_line} out of range " f"[{frag.start_line}, {len(all_lines)}]"
347-
)
345+
assert (
346+
frag.start_line <= frag.end_line <= len(all_lines)
347+
), f"end_line {frag.end_line} out of range [{frag.start_line}, {len(all_lines)}]"
348348
expected = "\n".join(all_lines[frag.start_line - 1 : frag.end_line])
349349
if not expected.endswith("\n"):
350350
expected += "\n"

tests/test_yaml_diff.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ def test_diff_yaml_structure(yaml_test_runner: YamlTestRunner, case: YamlTestCas
7272
assert isinstance(fragments, list), f"[{case.id}] 'fragments' is not a list"
7373

7474
if "fragment_count" in context:
75-
assert context["fragment_count"] == len(fragments), (
76-
f"[{case.id}] fragment_count ({context['fragment_count']}) " f"!= len(fragments) ({len(fragments)})"
77-
)
75+
assert context["fragment_count"] == len(
76+
fragments
77+
), f"[{case.id}] fragment_count ({context['fragment_count']}) != len(fragments) ({len(fragments)})"
7878

7979
for i, frag in enumerate(fragments):
8080
assert "path" in frag, f"[{case.id}] Fragment {i} missing 'path'"

0 commit comments

Comments
 (0)