Skip to content

Commit 3ba540f

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 3ba540f

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/libvcs/_internal/run.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import sys
1717
import typing as t
1818
from collections.abc import Iterable, Mapping, MutableMapping, Sequence
19+
from typing import IO, Any
1920

2021
from libvcs import exc
2122
from libvcs._internal.types import StrOrBytesPath
@@ -24,6 +25,10 @@
2425

2526
console_encoding = sys.stdout.encoding
2627

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

2833
def console_to_str(s: bytes) -> str:
2934
"""From pypa/pip project, pip.backwardwardcompat. License MIT."""
@@ -104,7 +109,7 @@ def __call__(self, output: t.AnyStr, timestamp: datetime.datetime) -> None:
104109
]
105110

106111
_CMD = t.Union[StrOrBytesPath, Sequence[StrOrBytesPath]]
107-
_FILE: TypeAlias = t.Optional[t.Union[int, t.IO[t.Any]]]
112+
_FILE: TypeAlias = t.Optional[t.Union[int, IO[Any]]]
108113

109114

110115
def run(
@@ -226,12 +231,23 @@ def progress_cb(output: t.AnyStr, timestamp: datetime.datetime) -> None:
226231
if callback and callable(callback):
227232
callback(output="\r", timestamp=datetime.datetime.now())
228233

229-
lines = filter(None, (line.strip() for line in proc.stdout.readlines()))
230-
all_output = console_to_str(b"\n".join(lines))
234+
if proc.stdout is None:
235+
raise RuntimeError(PIPE_NOT_SET_ERR)
236+
237+
stdout_lines: list[bytes] = [
238+
line.strip() for line in proc.stdout.readlines() if line
239+
]
240+
all_output = [console_to_str(b"\n".join(stdout_lines))]
241+
231242
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))
243+
if proc.stderr is None:
244+
raise RuntimeError(PIPE_NOT_SET_ERR)
245+
stderr_lines: list[bytes] = [
246+
line.strip() for line in proc.stderr.readlines() if line
247+
]
248+
all_output = [console_to_str(b"".join(stderr_lines))]
249+
234250
output = "".join(all_output)
235251
if code != 0 and check_returncode:
236-
raise exc.CommandError(output=output, returncode=code, cmd=args)
252+
raise exc.CommandError(output=output, returncode=code, cmd=str(args))
237253
return output

0 commit comments

Comments
 (0)