Skip to content

Commit f689c1d

Browse files
committed
fix(python-sdk): resolve type checker errors in extensions.py
- Import re2 and re separately to avoid type alias confusion - Add isinstance(raw, bytes) assertions after read_file(encoding=None) to narrow the bytes | str union for the type checker - Use type: ignore comments for re2 optional import and attribute access
1 parent 21c86ba commit f689c1d

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

sdk/python/agentfs_sdk/extensions.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
from .filesystem import Filesystem
1616

17+
import re
18+
1719
try:
18-
import re2 as re
20+
import re2 # type: ignore[import-not-found]
1921

2022
_RE2_AVAILABLE = True
2123
except ImportError:
22-
import re # type: ignore[no-redef]
23-
2424
_RE2_AVAILABLE = False
2525

2626

@@ -98,6 +98,7 @@ async def grep(
9898
... print(match.line)
9999
"""
100100
raw = await fs.read_file(path, encoding=None)
101+
assert isinstance(raw, bytes)
101102

102103
if _is_binary(raw):
103104
return GrepResult(message=f"Binary file {path}, cannot search")
@@ -115,9 +116,9 @@ async def grep(
115116
safe_pattern = re.escape(pattern) if fixed_string else pattern
116117

117118
if _RE2_AVAILABLE:
118-
opts = re.Options()
119+
opts = re2.Options() # type: ignore[possibly-undefined]
119120
opts.case_sensitive = not ignore_case
120-
compiled = re.compile(safe_pattern, opts)
121+
compiled = re2.compile(safe_pattern, opts) # type: ignore[possibly-undefined]
121122
else:
122123
# TODO: add timeout protection for re fallback — compile and search can both
123124
# hang on pathological patterns from untrusted input (ReDoS). signal.SIGALRM
@@ -276,6 +277,7 @@ async def wc(fs: Filesystem, path: str) -> WcResult:
276277
>>> print(f"{result.lines} {result.words} {result.bytes}")
277278
"""
278279
raw = await fs.read_file(path, encoding=None)
280+
assert isinstance(raw, bytes)
279281
byte_count = len(raw)
280282

281283
if _is_binary(raw):

0 commit comments

Comments
 (0)