Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1076b0

Browse files
committed
one more pass
1 parent f4ccaa6 commit a1076b0

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

IPython/utils/_process_common.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818
import shlex
1919
import subprocess
2020
import sys
21-
from typing import IO, Any, List, Union
21+
from typing import IO, List, TypeVar, Union
2222
from collections.abc import Callable
2323

24+
_T = TypeVar("_T")
25+
2426
from IPython.utils import py3compat
2527

2628
#-----------------------------------------------------------------------------
2729
# Function definitions
2830
#-----------------------------------------------------------------------------
2931

30-
def read_no_interrupt(stream: IO[Any]) -> bytes | None:
32+
def read_no_interrupt(stream: IO[bytes]) -> bytes | None:
3133
"""Read from a pipe ignoring EINTR errors.
3234
3335
This is necessary because when reading from pipes with GUI event loops
@@ -40,13 +42,14 @@ def read_no_interrupt(stream: IO[Any]) -> bytes | None:
4042
except IOError as err:
4143
if err.errno != errno.EINTR:
4244
raise
45+
return None
4346

4447

4548
def process_handler(
4649
cmd: Union[str, List[str]],
47-
callback: Callable[[subprocess.Popen], int | str | bytes],
50+
callback: Callable[[subprocess.Popen[bytes]], _T],
4851
stderr: int = subprocess.PIPE,
49-
) -> int | str | bytes | None:
52+
) -> _T | None:
5053
"""Open a command in a shell subprocess and execute a callback.
5154
5255
This function provides common scaffolding for creating subprocess.Popen()
@@ -137,7 +140,6 @@ def getoutput(cmd: str | list[str]) -> str:
137140
out = process_handler(cmd, lambda p: p.communicate()[0], subprocess.STDOUT)
138141
if out is None:
139142
return ''
140-
assert isinstance(out, bytes)
141143
return py3compat.decode(out)
142144

143145

@@ -177,10 +179,10 @@ def get_output_error_code(cmd: str | list[str]) -> tuple[str, str, int | None]:
177179
returncode: int
178180
"""
179181

180-
out_err, p = process_handler(cmd, lambda p: (p.communicate(), p))
181-
if out_err is None:
182-
return '', '', p.returncode
183-
out, err = out_err
182+
result = process_handler(cmd, lambda p: (p.communicate(), p))
183+
if result is None:
184+
return '', '', None
185+
(out, err), p = result
184186
return py3compat.decode(out), py3compat.decode(err), p.returncode
185187

186188
def arg_split(commandline: str, posix: bool = False, strict: bool = True) -> list[str]:
@@ -196,7 +198,7 @@ def arg_split(commandline: str, posix: bool = False, strict: bool = True) -> lis
196198
command-line args.
197199
"""
198200

199-
lex = shlex.shlex(s, posix=posix)
201+
lex = shlex.shlex(commandline, posix=posix)
200202
lex.whitespace_split = True
201203
# Extract tokens, ensuring that things like leaving open quotes
202204
# does not cause this to raise. This is important, because we

IPython/utils/_process_win32.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __exit__(
6565
os.chdir(self.path)
6666

6767

68-
def _system_body(p: subprocess.Popen) -> int:
68+
def _system_body(p: subprocess.Popen[bytes]) -> int:
6969
"""Callback for _system."""
7070
enc = DEFAULT_ENCODING
7171

@@ -136,9 +136,7 @@ def system(cmd: str) -> Optional[int]:
136136
if path is not None:
137137
cmd = '"pushd %s &&"%s' % (path, cmd)
138138
res = process_handler(cmd, _system_body)
139-
assert isinstance(res, int | type(None))
140139
return res
141-
return None
142140

143141

144142
def getoutput(cmd: str) -> str:

IPython/utils/py3compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .encoding import DEFAULT_ENCODING
1111

1212

13-
def decode(s, encoding=None):
13+
def decode(s: bytes, encoding: str | None = None) -> str:
1414
encoding = encoding or DEFAULT_ENCODING
1515
return s.decode(encoding, "replace")
1616

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
130130
module = [
131131
"IPython.core.events",
132132
"IPython.core.magic",
133+
"IPython.utils._process_common",
134+
"IPython.utils._process_win32",
133135
]
134136
# Strictest configuration, everything turned up to the max
135137
check_untyped_defs = true
@@ -302,7 +304,6 @@ module = [
302304
"IPython.terminal.shortcuts.auto_suggest",
303305
"IPython.terminal.shortcuts.filters",
304306
"IPython.utils._process_cli",
305-
"IPython.utils._process_common",
306307
"IPython.utils._process_emscripten",
307308
"IPython.utils.capture",
308309
"IPython.utils.coloransi",

0 commit comments

Comments
 (0)