Skip to content

Commit 314e9e2

Browse files
committed
chore: clean up CodeQL py/* quality alerts (unused-import, unused-local-variable, empty-except)
Three CodeQL Code Quality rules cleared in one pass: 1. **py/unused-import** (33 fixes) — `ruff check --select F401 --fix`. 4 in openkb/ (production code, all partial-unused where the line keeps the names actually used): Console at chat.py:341, SCHEMA_MD at linter.py:11, ItemHelpers at query.py:116, field at converter.py:6. 29 in tests/ — leftover `import pytest`, `from pathlib import Path`, and unused mock classes from test scaffolding. 2. **py/unused-local-variable** (5 fixes) — all in tests/: - tests/test_add_command.py:72, 86 — `result = runner.invoke(...)` captured but only mock-call assertions matter; dropped the binding. - tests/test_add_command.py:122 — `as mock_conv` never referenced; removed the alias from the with-patch. - tests/test_lint.py:197, 210 — `wiki = _make_wiki(tmp_path)` where the helper's side effect (creating the dir tree) is what matters; dropped the binding, kept the call. 3. **py/empty-except** (2 fixes) — both in openkb/agent/tools.py inside `parse_pages`. The `except ValueError: pass` blocks were intentionally tolerating malformed segments in user-supplied page specs ("3-5,7,foo"). Replaced with `contextlib.suppress(ValueError)`, which signals the intent explicitly and CodeQL no longer flags. Total: 40 quality alerts → 0. All 329 tests still pass. No behavior change. References: - https://github.com/VectifyAI/OpenKB/security/quality/rules/py/unused-import - https://github.com/VectifyAI/OpenKB/security/quality/rules/py/unused-local-variable - https://github.com/VectifyAI/OpenKB/security/quality/rules/py/empty-except (py/mixed-returns and py/multiple-definition: both 0 hits in the current codebase, no fixes needed.)
1 parent 97b1ca1 commit 314e9e2

22 files changed

Lines changed: 16 additions & 44 deletions

openkb/agent/chat.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,6 @@ async def _run_turn(
338338
need_blank_before_text = False
339339

340340
if use_color and not raw:
341-
from rich.console import Console
342341
from rich.live import Live
343342

344343
console = _make_rich_console()

openkb/agent/linter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from openkb.agent.tools import list_wiki_files, read_wiki_file
99

1010
MAX_TURNS = 50
11-
from openkb.schema import SCHEMA_MD, get_agents_md
11+
from openkb.schema import get_agents_md
1212

1313
_LINTER_INSTRUCTIONS_TEMPLATE = """\
1414
You are OpenKB's semantic lint agent. Your job is to audit the wiki

openkb/agent/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ async def run_query(
113113
The agent's final answer as a string.
114114
"""
115115
import sys
116-
from agents import RawResponsesStreamEvent, RunItemStreamEvent, ItemHelpers
116+
from agents import RawResponsesStreamEvent, RunItemStreamEvent
117117
from openai.types.responses import ResponseTextDeltaEvent
118118
from openkb.config import load_config
119119

openkb/agent/tools.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from __future__ import annotations
88

9+
import contextlib
910
import json as _json
1011
from pathlib import Path
1112

@@ -71,21 +72,19 @@ def parse_pages(pages: str) -> list[int]:
7172
segments = part.split("-")
7273
# Re-join to handle leading negatives: segments[0] may be empty
7374
# if part starts with "-". We just try to parse start/end.
74-
try:
75+
# Silently skip malformed segments — parse_pages is a tolerant
76+
# parser by design (user-supplied page specs may contain typos).
77+
with contextlib.suppress(ValueError):
7578
if len(segments) == 2:
7679
start, end = int(segments[0]), int(segments[1])
7780
result.update(range(start, end + 1))
7881
elif len(segments) == 3 and segments[0] == "":
7982
# e.g. "-1" split gives ['', '1']
8083
result.add(-int(segments[1]))
8184
# More complex cases (e.g. negative range) are ignored.
82-
except ValueError:
83-
pass
8485
else:
85-
try:
86+
with contextlib.suppress(ValueError):
8687
result.add(int(part))
87-
except ValueError:
88-
pass
8988
return sorted(n for n in result if n > 0)
9089

9190

openkb/converter.py

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

44
import logging
55
import shutil
6-
from dataclasses import dataclass, field
6+
from dataclasses import dataclass
77
from pathlib import Path
88

99
import pymupdf

tests/test_add_command.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
from __future__ import annotations
33

44
import json
5-
from pathlib import Path
6-
from unittest.mock import MagicMock, patch
5+
from unittest.mock import patch
76

8-
import pytest
97
from click.testing import CliRunner
108

119
from openkb.cli import SUPPORTED_EXTENSIONS, _find_kb_dir, cli
@@ -71,7 +69,7 @@ def test_add_single_file_calls_helper(self, tmp_path):
7169
runner = CliRunner()
7270
with patch("openkb.cli.add_single_file") as mock_add, \
7371
patch("openkb.cli._find_kb_dir", return_value=kb_dir):
74-
result = runner.invoke(cli, ["add", str(doc)])
72+
runner.invoke(cli, ["add", str(doc)])
7573
mock_add.assert_called_once_with(doc, kb_dir)
7674

7775
def test_add_directory_calls_helper_for_each_file(self, tmp_path):
@@ -85,7 +83,7 @@ def test_add_directory_calls_helper_for_each_file(self, tmp_path):
8583
runner = CliRunner()
8684
with patch("openkb.cli.add_single_file") as mock_add, \
8785
patch("openkb.cli._find_kb_dir", return_value=kb_dir):
88-
result = runner.invoke(cli, ["add", str(docs_dir)])
86+
runner.invoke(cli, ["add", str(docs_dir)])
8987
# Should be called for .md and .txt but not .xyz
9088
assert mock_add.call_count == 2
9189
called_names = {call.args[0].name for call in mock_add.call_args_list}
@@ -121,7 +119,7 @@ def test_add_skipped_file(self, tmp_path):
121119

122120
runner = CliRunner()
123121
with patch("openkb.cli._find_kb_dir", return_value=kb_dir), \
124-
patch("openkb.cli.convert_document", return_value=mock_result) as mock_conv, \
122+
patch("openkb.cli.convert_document", return_value=mock_result), \
125123
patch("openkb.cli.asyncio.run") as mock_arun:
126124
result = runner.invoke(cli, ["add", str(doc)])
127125
assert "SKIP" in result.output

tests/test_agent_tools.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Tests for openkb.agent.tools — plain function implementations."""
22
from __future__ import annotations
33

4-
from pathlib import Path
54

6-
import pytest
75

86
from openkb.agent.tools import get_wiki_page_content, list_wiki_files, parse_pages, read_wiki_file, write_wiki_file
97

tests/test_cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
from unittest.mock import patch
33

4-
import pytest
54
import yaml
65
from click.testing import CliRunner
76

tests/test_compiler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import annotations
33

44
import json
5-
from pathlib import Path
65
from unittest.mock import MagicMock, patch, AsyncMock
76

87
import pytest

tests/test_config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import pytest
2-
from pathlib import Path
31
from openkb.config import DEFAULT_CONFIG, load_config, save_config
42

53

0 commit comments

Comments
 (0)