Skip to content

Commit 5bbdee9

Browse files
Copilotnotfolder
andcommitted
Complete lint fixes for key infrastructure files - zero errors in clients, filelock_util, task handlers, and queueing modules
Co-authored-by: notfolder <20558197+notfolder@users.noreply.github.com>
1 parent 9db2ae6 commit 5bbdee9

9 files changed

Lines changed: 67 additions & 67 deletions

File tree

clients/ollama_client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
from ollama import chat
6+
17
from .llm_base import LLMClient
28

39

410
class OllamaClient(LLMClient):
5-
def __init__(self, config) -> None:
6-
from ollama import chat
7-
11+
def __init__(self, config: dict[str, Any]) -> None:
812
self.chat = chat
913
self.model = config["model"]
1014
self.max_token = config.get("max_token", 32768)
@@ -21,7 +25,7 @@ def send_user_message(self, message: str) -> None:
2125
self.messages.pop(1) # 最初のuserから削る
2226
total_chars = sum(len(m["content"]) for m in self.messages)
2327

24-
def send_function_result(self, name: str, result) -> None:
28+
def send_function_result(self, name: str, result: object) -> None:
2529
msg = "Ollama does not support function calls. Use OpenAI compatible call instead."
2630
raise NotImplementedError(
2731
msg,

clients/openai_client.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
from __future__ import annotations
2+
13
import json
4+
from typing import Any
5+
6+
import openai
27

38
from .llm_base import LLMClient
49

510

611
class OpenAIClient(LLMClient):
7-
def __init__(self, config, functions=None, tools=None) -> None:
8-
import openai
9-
12+
def __init__(
13+
self,
14+
config: dict[str, Any],
15+
functions: list[dict[str, Any]] | None = None,
16+
tools: list[dict[str, Any]] | None = None,
17+
) -> None:
1018
api_key = config.get("api_key", "OPENAI_API_KEY")
1119
base_url = config.get("base_url", "https://api.openai.com/")
1220
self.openai = openai.OpenAI(api_key=api_key, base_url=base_url, timeout=3600)
@@ -26,18 +34,17 @@ def send_user_message(self, message: str) -> None:
2634
self.messages.pop(1)
2735
total_chars = sum(len(m["content"]) for m in self.messages)
2836

29-
def send_function_result(self, name: str, result) -> None:
37+
def send_function_result(self, name: str, result: object) -> None:
3038
self.messages.append({"role": "tool", "name": name, "content": json.dumps(result)})
3139
total_chars = sum(len(m["content"]) for m in self.messages)
3240
while total_chars // 4 > self.max_token:
3341
self.messages.pop(1)
3442
total_chars = sum(len(m["content"]) for m in self.messages)
3543

36-
def get_response(self) -> tuple[str, list[any]]:
44+
def get_response(self) -> tuple[str, list[Any]]:
3745
resp = self.openai.chat.completions.create(
3846
model=self.model,
3947
messages=self.messages,
40-
# tools=self.tools,
4148
functions=self.functions,
4249
function_call="auto",
4350
)
@@ -47,6 +54,10 @@ def get_response(self) -> tuple[str, list[any]]:
4754
self.messages.append({"role": choice.message.role, "content": choice.message.content})
4855
reply += choice.message.content
4956
if choice.message.function_call is not None:
50-
reply += f"Function call: {choice.message.function_call.name} with arguments {choice.message.function_call.arguments}"
57+
func_call = choice.message.function_call
58+
reply += (
59+
f"Function call: {func_call.name} "
60+
f"with arguments {func_call.arguments}"
61+
)
5162
functions.append(choice.message.function_call)
5263
return reply, functions

filelock_util.py

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

9+
import contextlib
910
import sys
10-
from typing import Any, TextIO
11+
from pathlib import Path
12+
from typing import TYPE_CHECKING, TextIO
13+
14+
if TYPE_CHECKING:
15+
import types
1116

1217
import portalocker
1318
from typing_extensions import Self
@@ -41,14 +46,12 @@ def acquire(self) -> None:
4146
SystemExit: 他のプロセスがロックを保持している場合
4247
4348
"""
44-
# ロックファイルを書き込みモードで開く
45-
self.fp = open(self.lockfile, "w")
49+
lockfile_path = Path(self.lockfile)
50+
self.fp = lockfile_path.open("w")
4651

4752
try:
48-
# 排他ロック + ノンブロッキングでロックを取得
4953
portalocker.lock(self.fp, portalocker.LOCK_EX | portalocker.LOCK_NB)
5054
except portalocker.LockException:
51-
# ロックの取得に失敗した場合(他のプロセスが保持中)
5255
sys.exit(1)
5356

5457
def release(self) -> None:
@@ -58,15 +61,9 @@ def release(self) -> None:
5861
エラーが発生してもプログラムの実行は継続されます。
5962
"""
6063
if self.fp:
61-
try:
62-
# ロックを解放
64+
with contextlib.suppress(portalocker.LockException):
6365
portalocker.unlock(self.fp)
64-
except Exception:
65-
# エラーが発生してもプログラムを継続
66-
# (プロセス終了時に自動的にロックは解放される)
67-
pass
6866

69-
# ファイルハンドルを閉じる
7067
self.fp.close()
7168
self.fp = None
7269

@@ -84,9 +81,9 @@ def __enter__(self) -> Self:
8481

8582
def __exit__(
8683
self,
87-
exc_type: type | None,
88-
exc_val: Exception | None,
89-
exc_tb: Any | None,
84+
exc_type: type[BaseException] | None,
85+
exc_val: BaseException | None,
86+
exc_tb: types.TracebackType | None,
9087
) -> None:
9188
"""コンテキストマネージャーの終了処理.
9289

handlers/task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_prompt(self) -> str:
3939
"""
4040

4141
@abstractmethod
42-
def comment(self, text: str, mention: bool = False) -> None:
42+
def comment(self, text: str, *, mention: bool = False) -> None:
4343
"""タスクにコメントを追加する.
4444
4545
Args:

handlers/task_factory.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
from .task import Task
2020

21+
from .task_getter_github import TaskGitHubIssue, TaskGitHubPullRequest
22+
from .task_getter_gitlab import TaskGitLabIssue, TaskGitLabMergeRequest
23+
2124

2225
class TaskFactory(ABC):
2326
@abstractmethod
@@ -42,15 +45,11 @@ def create_task(self, task_key: TaskKey) -> Task | None:
4245
"get_issue",
4346
{"owner": task_key.owner, "repo": task_key.repo, "issue_number": task_key.number},
4447
)
45-
from .task_getter_github import TaskGitHubIssue
46-
4748
return TaskGitHubIssue(issue, self.mcp_client, self.github_client, self.config)
4849
if isinstance(task_key, GitHubPullRequestTaskKey):
4950
pr = self.github_client.get_pull_request(
5051
owner=task_key.owner, repo=task_key.repo, pull_number=task_key.number,
5152
)
52-
from .task_getter_github import TaskGitHubPullRequest
53-
5453
return TaskGitHubPullRequest(pr, self.mcp_client, self.github_client, self.config)
5554
msg = "Unknown task key type for GitHub"
5655
raise ValueError(msg)
@@ -72,15 +71,11 @@ def create_task(self, task_key: TaskKey) -> Task | None:
7271
issue = self.mcp_client.call_tool(
7372
"get_issue", {"project_id": task_key.project_id, "issue_iid": task_key.issue_iid},
7473
)
75-
from .task_getter_gitlab import TaskGitLabIssue
76-
7774
return TaskGitLabIssue(issue, self.mcp_client, self.gitlab_client, self.config)
7875
if isinstance(task_key, GitLabMergeRequestTaskKey):
7976
mr = self.gitlab_client.get_merge_request(
8077
project_id=task_key.project_id, mr_iid=task_key.mr_iid,
8178
)
82-
from .task_getter_gitlab import TaskGitLabMergeRequest
83-
8479
return TaskGitLabMergeRequest(mr, self.mcp_client, self.gitlab_client, self.config)
8580
msg = "Unknown task key type for GitLab"
8681
raise ValueError(msg)

handlers/task_getter.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
1+
from __future__ import annotations
2+
13
from abc import ABC, abstractmethod
2-
from typing import NoReturn
4+
from typing import TYPE_CHECKING, Any, NoReturn
5+
6+
if TYPE_CHECKING:
7+
from .task_getter_github import TaskGetterFromGitHub
8+
from .task_getter_gitlab import TaskGetterFromGitLab
9+
10+
from .task_getter_github import TaskGetterFromGitHub
11+
from .task_getter_gitlab import TaskGetterFromGitLab
312

413

514
class TaskGetter(ABC):
615
@abstractmethod
7-
def get_task_list(self):
16+
def get_task_list(self) -> list[dict[str, Any]]:
817
pass
918

1019
@classmethod
11-
def factory(cls, config, mcp_clients, task_source):
12-
# 設定に応じて適切なTaskGetterを返す
20+
def factory(
21+
cls,
22+
config: dict[str, Any],
23+
mcp_clients: dict[str, Any],
24+
task_source: str,
25+
) -> TaskGetterFromGitHub | TaskGetterFromGitLab:
1326
if task_source == "github":
14-
from .task_getter_github import TaskGetterFromGitHub
15-
1627
return TaskGetterFromGitHub(config, mcp_clients)
1728
if task_source == "gitlab":
18-
from .task_getter_gitlab import TaskGetterFromGitLab
19-
2029
return TaskGetterFromGitLab(config, mcp_clients)
2130
msg = f"Unknown task_source: {task_source}"
2231
raise ValueError(msg)
2332

2433
@abstractmethod
25-
def from_task_key(self, task_key_dict) -> NoReturn:
34+
def from_task_key(self, task_key_dict: dict[str, Any]) -> NoReturn:
2635
"""タスクキーからタスクを生成."""
2736
msg = "from_task_keyはサブクラスで実装してください"
2837
raise NotImplementedError(msg)

handlers/task_getter_github.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def get_prompt(self) -> str:
5353
"repo": self.issue["repo"],
5454
"issue_number": self.issue["number"],
5555
}
56-
# issue = self.mcp_client.call_tool('get_issue', args)
5756
comments = [
5857
comment.get("body", "")
5958
for comment in self.mcp_client.call_tool("get_issue_comments", args)
@@ -67,7 +66,7 @@ def get_prompt(self) -> str:
6766
f"COMMENTS: {comments}"
6867
)
6968

70-
def comment(self, text: str, mention: bool = False) -> None:
69+
def comment(self, text: str, *, mention: bool = False) -> None:
7170
if mention:
7271
owner = self.issue.get("owner")
7372
if owner:
@@ -110,7 +109,7 @@ def __init__(
110109
config: dict[str, Any],
111110
) -> None:
112111
self.pr = pr
113-
repository_url = pr.get("repository_url", None)
112+
repository_url = pr.get("repository_url")
114113
if repository_url is None:
115114
repository_url = pr.get("base", {}).get("repo", {}).get("html_url", "")
116115
self.pr["repo"] = repository_url.split("/")[-1]
@@ -153,7 +152,7 @@ def get_prompt(self) -> str:
153152
}
154153
return f"PULL_REQUEST: {json.dumps(pr_info, ensure_ascii=False)}\n"
155154

156-
def comment(self, text: str, mention: bool = False) -> None:
155+
def comment(self, text: str, *, mention: bool = False) -> None:
157156
if mention:
158157
owner = self.pr.get("owner")
159158
if owner:
@@ -220,17 +219,13 @@ def get_task_list(self) -> list[Task]:
220219
def from_task_key(self, task_key_dict: dict[str, Any]) -> Task | None:
221220
ttype = task_key_dict.get("type")
222221
if ttype == "github_issue":
223-
from .task_key import GitHubIssueTaskKey
224-
225222
task_key = GitHubIssueTaskKey.from_dict(task_key_dict)
226223
issue = self.mcp_client.call_tool(
227224
"get_issue",
228225
{"owner": task_key.owner, "repo": task_key.repo, "issue_number": task_key.number},
229226
)
230227
return TaskGitHubIssue(issue, self.mcp_client, self.github_client, self.config)
231228
if ttype == "github_pull_request":
232-
from .task_key import GitHubPullRequestTaskKey
233-
234229
task_key = GitHubPullRequestTaskKey.from_dict(task_key_dict)
235230
pr = self.github_client.get_pull_request(
236231
owner=task_key.owner, repo=task_key.repo, pull_number=task_key.number,

handlers/task_getter_gitlab.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def get_prompt(self) -> str:
4343
# issue本体取得
4444
args = {"project_id": f"{self.project_id}", "issue_iid": self.issue_iid}
4545
self.mcp_client.call_tool("get_issue", args)
46-
# コメント取得(GitLabはノートとして管理)
4746
comments = []
4847
note_args = {"project_id": f"{self.project_id}", "issue_iid": self.issue_iid}
4948
comments = self.mcp_client.call_tool("list_issue_discussions", note_args)
@@ -59,7 +58,7 @@ def get_prompt(self) -> str:
5958
f"COMMENTS: {comments}"
6059
)
6160

62-
def comment(self, text, mention=False) -> None:
61+
def comment(self, text: str, *, mention: bool = False) -> None:
6362
if mention:
6463
owner = self.issue.get("author", {}).get("username")
6564
if owner:
@@ -133,7 +132,7 @@ def get_prompt(self) -> str:
133132
f"COMMENTS: {comments}"
134133
)
135134

136-
def comment(self, text, mention=False) -> None:
135+
def comment(self, text: str, *, mention: bool = False) -> None:
137136
if mention:
138137
owner = self.mr.get("author", {}).get("username")
139138
if owner:
@@ -202,17 +201,13 @@ def get_task_list(self) -> list[Task]:
202201
def from_task_key(self, task_key_dict: dict[str, Any]) -> Task | None:
203202
ttype = task_key_dict.get("type")
204203
if ttype == "gitlab_issue":
205-
from .task_key import GitLabIssueTaskKey
206-
207204
task_key = GitLabIssueTaskKey.from_dict(task_key_dict)
208205
issue = self.mcp_client.call_tool(
209206
"get_issue",
210207
{"project_id": str(task_key.project_id), "issue_iid": task_key.issue_iid},
211208
)
212209
return TaskGitLabIssue(issue, self.mcp_client, self.gitlab_client, self.config)
213210
if ttype == "gitlab_merge_request":
214-
from .task_key import GitLabMergeRequestTaskKey
215-
216211
task_key = GitLabMergeRequestTaskKey.from_dict(task_key_dict)
217212
mr = self.gitlab_client.get_merge_request(
218213
project_id=task_key.project_id, mr_iid=task_key.mr_iid,

queueing.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ def __init__(self, config: dict[str, Any]) -> None:
114114
config: アプリケーション設定辞書(rabbitmq設定を含む)
115115
116116
"""
117-
import pika
118-
119-
# RabbitMQ設定を取得(デフォルト値を設定)
120117
mq_conf = config.get("rabbitmq", {})
121118
self.queue_name = mq_conf.get("queue", "coding_agent_tasks")
122119
self.host = mq_conf.get("host", "localhost")
@@ -148,19 +145,18 @@ def put(self, task: dict[str, Any]) -> None:
148145
# タスクをJSONにシリアライズ
149146
body = json.dumps(task)
150147

151-
# メッセージをキューに送信(永続化設定あり)
152148
self.channel.basic_publish(
153149
exchange="",
154150
routing_key=self.queue_name,
155151
body=body,
156152
properties=pika.BasicProperties(delivery_mode=2), # メッセージを永続化
157153
)
158154

159-
def get(self, timeout: float | None = None) -> dict[str, Any] | None:
155+
def get(self, _timeout: float | None = None) -> dict[str, Any] | None:
160156
"""RabbitMQキューからタスクを取得する.
161157
162158
Args:
163-
timeout: タイムアウト時間(秒)。現在の実装では使用されません
159+
_timeout: タイムアウト時間(秒)。現在の実装では使用されません
164160
165161
Returns:
166162
取得したタスクの辞書。メッセージがない場合はNone
@@ -170,7 +166,6 @@ def get(self, timeout: float | None = None) -> dict[str, Any] | None:
170166
必要に応じて将来のバージョンで実装予定です。
171167
172168
"""
173-
# キューからメッセージを取得(自動確認応答)
174169
method_frame, header_frame, body = self.channel.basic_get(
175170
queue=self.queue_name, auto_ack=True,
176171
)
@@ -189,7 +184,6 @@ def empty(self) -> bool:
189184
キューが空の場合True、そうでなければFalse
190185
191186
"""
192-
# キューの状態を確認(passive=Trueで既存キューの情報のみ取得)
193187
queue_info = self.channel.queue_declare(queue=self.queue_name, passive=True)
194188

195189
# メッセージ数が0かどうかを返す

0 commit comments

Comments
 (0)