Skip to content

Commit 4dcd9db

Browse files
committed
Update subprocess types and fix mypy issues in run.py - Add proper type annotations for stdout/stderr lines - Fix None checks for file descriptors - Update error messages to follow style guidelines - Improve error handling for subprocess calls
1 parent 399b845 commit 4dcd9db

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/libvcs/_internal/run.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
console_encoding = sys.stdout.encoding
2626

27+
PIPE_NOT_SET_ERR = (
28+
"File descriptor is None, which should not happen as we set PIPE as default"
29+
)
30+
2731

2832
def console_to_str(s: bytes) -> str:
2933
"""From pypa/pip project, pip.backwardwardcompat. License MIT."""
@@ -226,12 +230,23 @@ def progress_cb(output: t.AnyStr, timestamp: datetime.datetime) -> None:
226230
if callback and callable(callback):
227231
callback(output="\r", timestamp=datetime.datetime.now())
228232

229-
lines = filter(None, (line.strip() for line in proc.stdout.readlines()))
230-
all_output = console_to_str(b"\n".join(lines))
233+
if proc.stdout is None:
234+
raise RuntimeError(PIPE_NOT_SET_ERR)
235+
236+
stdout_lines: list[bytes] = [
237+
line.strip() for line in proc.stdout.readlines() if line
238+
]
239+
all_output = [console_to_str(b"\n".join(stdout_lines))]
240+
231241
if code:
232-
stderr_lines = filter(None, (line.strip() for line in proc.stderr.readlines()))
233-
all_output = console_to_str(b"".join(stderr_lines))
242+
if proc.stderr is None:
243+
raise RuntimeError(PIPE_NOT_SET_ERR)
244+
stderr_lines: list[bytes] = [
245+
line.strip() for line in proc.stderr.readlines() if line
246+
]
247+
all_output = [console_to_str(b"".join(stderr_lines))]
248+
234249
output = "".join(all_output)
235250
if code != 0 and check_returncode:
236-
raise exc.CommandError(output=output, returncode=code, cmd=args)
251+
raise exc.CommandError(output=output, returncode=code, cmd=str(args))
237252
return output

0 commit comments

Comments
 (0)