Skip to content

Commit 376fe3b

Browse files
committed
fix: typecheck and fix errors
Run typecheck and fix errors. There are many errors so make a plan about which module to do first based on dependencies. ```git-revs cbc5d78 (Base revision) 3e0e7f2 Fix return type of run_command to specify CompletedProcess type parameters 7c760d1 Add type parameter to CompletedProcess instantiation d66377f Fix type issue with wait_time in TimeoutExpired constructor by ensuring it's a float 688b278 Remove unnecessary isinstance check since input is already typed as Optional[str] 5069fc5 Add necessary imports and type definitions for anyio file operations 60cc81f Fix the mode type parameter in async_open_text function e658af6 Fix the mode type parameter in async_open_binary function 96e35cc Fix the mode type parameter in async_write_text function 263feed Fix the mode type parameter in async_write_binary function and remove the unnecessary newline parameter for binary mode 5208044 Update the async_readlines function to use OpenTextMode 9520770 Expand OpenTextMode and OpenBinaryMode literals to include write and append modes dc10140 Add proper imports including type definitions 24b253e Update async_open_text to use OpenTextMode c495c2d Update write_text_content to use Optional instead of union type 5eb7591 Fix the write mode by using an explicit OpenTextMode variable 7d8a50d Update return type annotation in check_file_path_and_permissions f02bdae Fix returncode handling to avoid None value 121b4d9 Remove unused Any import bea7686 Remove unused imports 07dd0c8 Fix async_readlines by using string literal for mode f020f12 Remove unused imports from file_utils.py 6202623 Cast stdout to str in get_head_commit_message function 350e6ed Cast stdout to str in get_head_commit_hash function 6683f1b Cast stdout to str in get_repository_root function ed6d8c0 Cast stdout to str in get_ref_commit_chat_id function e214ad5 Cast tree_hash to str in create_commit_reference 672df85 Cast head_hash to str 1664682 Cast commit_hash to str 5d39ae8 Cast tree_hash and ref_message to str 5eb976c Cast new_commit_hash to str 5e4e2bc Fix chat_id parameter using Optional type 75fc0b2 Add Union to imports in code_command.py 9eb3519 Add necessary imports to common.py 572f0c3 Update truncate_output_content to handle bytes input 1f7c76a Add type annotation for the result list in get_edit_snippet ab4cd32 Add type annotation for processed_lines list in truncate_output_content 24e0ea9 Convert chat_id to string to fix type error in commit_changes call c5a765a Convert chat_id to string for earlier commit_changes call HEAD Auto-commit format changes ``` codemcp-id: 220-fix-typecheck-and-fix-errors ghstack-source-id: c4b2f88 Pull-Request-resolved: #211
1 parent f0326d4 commit 376fe3b

File tree

7 files changed

+112
-35
lines changed

7 files changed

+112
-35
lines changed

codemcp/async_file_utils.py

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,73 @@
11
#!/usr/bin/env python3
22

33
import os
4-
from typing import List
4+
from typing import List, Literal
55

66
import anyio
77

88
from .line_endings import detect_line_endings
99

10+
# Define OpenTextMode and OpenBinaryMode similar to what anyio uses
11+
OpenTextMode = Literal[
12+
"r",
13+
"r+",
14+
"+r",
15+
"rt",
16+
"rt+",
17+
"r+t",
18+
"+rt",
19+
"tr",
20+
"tr+",
21+
"t+r",
22+
"w",
23+
"w+",
24+
"+w",
25+
"wt",
26+
"wt+",
27+
"w+t",
28+
"+wt",
29+
"tw",
30+
"tw+",
31+
"t+w",
32+
"a",
33+
"a+",
34+
"+a",
35+
"at",
36+
"at+",
37+
"a+t",
38+
"+at",
39+
"ta",
40+
"ta+",
41+
"t+a",
42+
]
43+
OpenBinaryMode = Literal[
44+
"rb",
45+
"rb+",
46+
"r+b",
47+
"+rb",
48+
"br",
49+
"br+",
50+
"b+r",
51+
"wb",
52+
"wb+",
53+
"w+b",
54+
"+wb",
55+
"bw",
56+
"bw+",
57+
"b+w",
58+
"ab",
59+
"ab+",
60+
"a+b",
61+
"+ab",
62+
"ba",
63+
"ba+",
64+
"b+a",
65+
]
66+
1067

1168
async def async_open_text(
1269
file_path: str,
13-
mode: str = "r",
70+
mode: OpenTextMode = "r",
1471
encoding: str = "utf-8",
1572
errors: str = "replace",
1673
) -> str:
@@ -31,7 +88,7 @@ async def async_open_text(
3188
return await f.read()
3289

3390

34-
async def async_open_binary(file_path: str, mode: str = "rb") -> bytes:
91+
async def async_open_binary(file_path: str, mode: OpenBinaryMode = "rb") -> bytes:
3592
"""Asynchronously open and read a binary file.
3693
3794
Args:
@@ -67,7 +124,7 @@ async def async_readlines(
67124
async def async_write_text(
68125
file_path: str,
69126
content: str,
70-
mode: str = "w",
127+
mode: OpenTextMode = "w",
71128
encoding: str = "utf-8",
72129
) -> None:
73130
"""Asynchronously write text to a file.
@@ -84,15 +141,17 @@ async def async_write_text(
84141
await f.write(content)
85142

86143

87-
async def async_write_binary(file_path: str, content: bytes, mode: str = "wb") -> None:
144+
async def async_write_binary(
145+
file_path: str, content: bytes, mode: OpenBinaryMode = "wb"
146+
) -> None:
88147
"""Asynchronously write binary data to a file.
89148
90149
Args:
91150
file_path: The path to the file
92151
content: The binary content to write
93152
mode: The file open mode (default: 'wb')
94153
"""
95-
async with await anyio.open_file(file_path, mode, newline="") as f:
154+
async with await anyio.open_file(file_path, mode) as f:
96155
await f.write(content)
97156

98157

codemcp/code_command.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import os
55
import subprocess
6-
from typing import List, Optional
6+
from typing import List, Optional, Union
77

88
import tomli
99

@@ -92,7 +92,7 @@ async def run_code_command(
9292
command_name: str,
9393
command: List[str],
9494
commit_message: str,
95-
chat_id: str = None,
95+
chat_id: Optional[str] = None,
9696
) -> str:
9797
"""Run a code command (lint, format, etc.) and handle git operations.
9898
@@ -131,10 +131,11 @@ async def run_code_command(
131131
# If it's a git repo, commit any pending changes before running the command
132132
if is_git_repo:
133133
logging.info(f"Committing any pending changes before {command_name}")
134+
chat_id_str = str(chat_id) if chat_id is not None else ""
134135
commit_result = await commit_changes(
135136
full_dir_path,
136137
f"Snapshot before auto-{command_name}",
137-
chat_id,
138+
chat_id_str,
138139
commit_all=True,
139140
)
140141
if not commit_result[0]:
@@ -160,8 +161,9 @@ async def run_code_command(
160161
has_changes = await check_for_changes(full_dir_path)
161162
if has_changes:
162163
logging.info(f"Changes detected after {command_name}, committing")
164+
chat_id_str = str(chat_id) if chat_id is not None else ""
163165
success, commit_result_message = await commit_changes(
164-
full_dir_path, commit_message, chat_id, commit_all=True
166+
full_dir_path, commit_message, chat_id_str, commit_all=True
165167
)
166168

167169
if success:

codemcp/common.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
import os
4+
from typing import List, Union
45

56
# Constants
67
MAX_LINES_TO_READ = 1000
@@ -78,15 +79,15 @@ def get_edit_snippet(
7879
snippet_lines = edited_lines[start_line:end_line]
7980

8081
# Format with line numbers
81-
result = []
82+
result: List[str] = []
8283
for i, line in enumerate(snippet_lines):
8384
line_num = start_line + i + 1
8485
result.append(f"{line_num:4d} | {line}")
8586

8687
return "\n".join(result)
8788

8889

89-
def truncate_output_content(content: str, prefer_end: bool = True) -> str:
90+
def truncate_output_content(content: Union[str, bytes], prefer_end: bool = True) -> str:
9091
"""Truncate command output content to a reasonable size.
9192
9293
When prefer_end is True, this function prioritizes keeping content from the end
@@ -101,15 +102,22 @@ def truncate_output_content(content: str, prefer_end: bool = True) -> str:
101102
The truncated content with appropriate indicators
102103
"""
103104
if not content:
104-
return content
105+
return "" if content is None else str(content)
106+
107+
# Convert bytes to str if needed
108+
if isinstance(content, bytes):
109+
try:
110+
content = content.decode("utf-8")
111+
except UnicodeDecodeError:
112+
return "[Binary content cannot be displayed]"
105113

106114
lines = content.splitlines()
107115
total_lines = len(lines)
108116

109117
# If number of lines is within the limit, check individual line lengths
110118
if total_lines <= MAX_LINES_TO_READ:
111119
# Process line lengths
112-
processed_lines = []
120+
processed_lines: List[str] = []
113121
for line in lines:
114122
if len(line) > MAX_LINE_LENGTH:
115123
processed_lines.append(line[:MAX_LINE_LENGTH] + "... (line truncated)")

codemcp/file_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import logging
44
import os
5+
from typing import Optional, Tuple
56

67
import anyio
78

89
from .access import check_edit_permission
910
from .git import commit_changes
1011
from .line_endings import apply_line_endings, normalize_to_lf
12+
from .async_file_utils import OpenTextMode
1113

1214
__all__ = [
1315
"check_file_path_and_permissions",
@@ -18,7 +20,7 @@
1820
]
1921

2022

21-
async def check_file_path_and_permissions(file_path: str) -> tuple[bool, str | None]:
23+
async def check_file_path_and_permissions(file_path: str) -> Tuple[bool, Optional[str]]:
2224
"""Check if the file path is valid and has the necessary permissions.
2325
2426
Args:
@@ -110,7 +112,7 @@ def ensure_directory_exists(file_path: str) -> None:
110112

111113
async def async_open_text(
112114
file_path: str,
113-
mode: str = "r",
115+
mode: OpenTextMode = "r",
114116
encoding: str = "utf-8",
115117
errors: str = "replace",
116118
) -> str:
@@ -135,7 +137,7 @@ async def write_text_content(
135137
file_path: str,
136138
content: str,
137139
encoding: str = "utf-8",
138-
line_endings: str | None = None,
140+
line_endings: Optional[str] = None,
139141
) -> None:
140142
"""Write text content to a file with specified encoding and line endings.
141143
@@ -156,7 +158,8 @@ async def write_text_content(
156158
ensure_directory_exists(file_path)
157159

158160
# Write the content using anyio
161+
write_mode: OpenTextMode = "w"
159162
async with await anyio.open_file(
160-
file_path, "w", encoding=encoding, newline=""
163+
file_path, write_mode, encoding=encoding, newline=""
161164
) as f:
162165
await f.write(final_content)

codemcp/git_commit.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async def create_commit_reference(
9191
text=True,
9292
check=True,
9393
)
94-
tree_hash = tree_result.stdout.strip()
94+
tree_hash = str(tree_result.stdout.strip())
9595
else:
9696
# Create an empty tree if no HEAD exists
9797
empty_tree_result = await run_command(
@@ -102,7 +102,7 @@ async def create_commit_reference(
102102
text=True,
103103
check=True,
104104
)
105-
tree_hash = empty_tree_result.stdout.strip()
105+
tree_hash = str(empty_tree_result.stdout.strip())
106106

107107
commit_message = commit_msg
108108

@@ -116,7 +116,7 @@ async def create_commit_reference(
116116
text=True,
117117
check=True,
118118
)
119-
head_hash = head_hash_result.stdout.strip()
119+
head_hash = str(head_hash_result.stdout.strip())
120120
parent_arg = ["-p", head_hash]
121121

122122
# Create the commit object (with GPG signing explicitly disabled)
@@ -135,7 +135,7 @@ async def create_commit_reference(
135135
text=True,
136136
check=True,
137137
)
138-
commit_hash = commit_result.stdout.strip()
138+
commit_hash = str(commit_result.stdout.strip())
139139

140140
ref_name = f"refs/codemcp/{chat_id}"
141141

@@ -309,7 +309,7 @@ async def commit_changes(
309309
text=True,
310310
check=True,
311311
)
312-
tree_hash = tree_result.stdout.strip()
312+
tree_hash = str(tree_result.stdout.strip())
313313

314314
# Get the commit message from the reference
315315
ref_message_result = await run_command(
@@ -319,7 +319,7 @@ async def commit_changes(
319319
text=True,
320320
check=True,
321321
)
322-
ref_message = ref_message_result.stdout.strip()
322+
ref_message = str(ref_message_result.stdout.strip())
323323

324324
# Create a new commit with the same tree as HEAD but message from the reference
325325
# This effectively creates the commit without changing the working tree
@@ -339,7 +339,7 @@ async def commit_changes(
339339
text=True,
340340
check=True,
341341
)
342-
new_commit_hash = new_commit_result.stdout.strip()
342+
new_commit_hash = str(new_commit_result.stdout.strip())
343343

344344
# Update HEAD to point to the new commit
345345
await run_command(

codemcp/git_query.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def get_head_commit_message(directory: str) -> str:
4242
text=True,
4343
)
4444

45-
return result.stdout.strip()
45+
return str(result.stdout.strip())
4646

4747

4848
async def get_head_commit_hash(directory: str, short: bool = True) -> str:
@@ -73,7 +73,7 @@ async def get_head_commit_hash(directory: str, short: bool = True) -> str:
7373
text=True,
7474
)
7575

76-
return result.stdout.strip()
76+
return str(result.stdout.strip())
7777

7878

7979
async def get_head_commit_chat_id(directory: str) -> str | None:
@@ -150,7 +150,7 @@ async def get_repository_root(path: str) -> str:
150150
text=True,
151151
)
152152

153-
return result.stdout.strip()
153+
return str(result.stdout.strip())
154154

155155

156156
async def is_git_repository(path: str) -> bool:
@@ -207,7 +207,7 @@ async def get_ref_commit_chat_id(directory: str, ref_name: str) -> str | None:
207207
capture_output=True,
208208
text=True,
209209
)
210-
commit_message = message_result.stdout.strip()
210+
commit_message = str(message_result.stdout.strip())
211211

212212
# Use regex to find the last occurrence of codemcp-id: XXX
213213
# The pattern looks for "codemcp-id: " followed by any characters up to a newline or end of string

0 commit comments

Comments
 (0)