Skip to content

Commit 67c2dcd

Browse files
committed
Remove pre_commit.five
1 parent f33716c commit 67c2dcd

File tree

10 files changed

+33
-60
lines changed

10 files changed

+33
-60
lines changed

pre_commit/commands/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _run_single_hook(
173173

174174
if out.strip():
175175
output.write_line()
176-
output.write_line(out.strip(), logfile_name=hook.log_file)
176+
output.write_line_b(out.strip(), logfile_name=hook.log_file)
177177
output.write_line()
178178

179179
return files_modified or bool(retcode)

pre_commit/error_handler.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import sys
44
import traceback
55
from typing import Generator
6-
from typing import Union
6+
from typing import Optional
77

88
import pre_commit.constants as C
9-
from pre_commit import five
109
from pre_commit import output
1110
from pre_commit.store import Store
1211

@@ -15,25 +14,24 @@ class FatalError(RuntimeError):
1514
pass
1615

1716

18-
def _to_bytes(exc: BaseException) -> bytes:
19-
return str(exc).encode()
20-
21-
2217
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
2318
error_msg = b''.join((
24-
five.to_bytes(msg), b': ',
25-
five.to_bytes(type(exc).__name__), b': ',
26-
_to_bytes(exc),
19+
msg.encode(), b': ',
20+
type(exc).__name__.encode(), b': ',
21+
str(exc).encode(),
2722
))
28-
output.write_line(error_msg)
23+
output.write_line_b(error_msg)
2924
store = Store()
3025
log_path = os.path.join(store.directory, 'pre-commit.log')
3126
output.write_line(f'Check the log at {log_path}')
3227

3328
with open(log_path, 'wb') as log:
34-
def _log_line(s: Union[None, str, bytes] = None) -> None:
29+
def _log_line(s: Optional[str] = None) -> None:
3530
output.write_line(s, stream=log)
3631

32+
def _log_line_b(s: Optional[bytes] = None) -> None:
33+
output.write_line_b(s, stream=log)
34+
3735
_log_line('### version information')
3836
_log_line()
3937
_log_line('```')
@@ -50,7 +48,7 @@ def _log_line(s: Union[None, str, bytes] = None) -> None:
5048
_log_line('### error information')
5149
_log_line()
5250
_log_line('```')
53-
_log_line(error_msg)
51+
_log_line_b(error_msg)
5452
_log_line('```')
5553
_log_line()
5654
_log_line('```')

pre_commit/five.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

pre_commit/languages/pygrep.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _process_filename_by_line(pattern: Pattern[bytes], filename: str) -> int:
2727
if pattern.search(line):
2828
retv = 1
2929
output.write(f'{filename}:{line_no}:')
30-
output.write_line(line.rstrip(b'\r\n'))
30+
output.write_line_b(line.rstrip(b'\r\n'))
3131
return retv
3232

3333

@@ -44,7 +44,7 @@ def _process_filename_at_once(pattern: Pattern[bytes], filename: str) -> int:
4444
matched_lines = match[0].split(b'\n')
4545
matched_lines[0] = contents.split(b'\n')[line_no]
4646

47-
output.write_line(b'\n'.join(matched_lines))
47+
output.write_line_b(b'\n'.join(matched_lines))
4848
return retv
4949

5050

pre_commit/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import pre_commit.constants as C
1111
from pre_commit import color
12-
from pre_commit import five
1312
from pre_commit import git
1413
from pre_commit.commands.autoupdate import autoupdate
1514
from pre_commit.commands.clean import clean
@@ -155,7 +154,6 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
155154

156155
def main(argv: Optional[Sequence[str]] = None) -> int:
157156
argv = argv if argv is not None else sys.argv[1:]
158-
argv = [five.to_text(arg) for arg in argv]
159157
parser = argparse.ArgumentParser(prog='pre-commit')
160158

161159
# https://stackoverflow.com/a/8521644/812183

pre_commit/output.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import contextlib
22
import sys
3+
from typing import Any
34
from typing import IO
45
from typing import Optional
5-
from typing import Union
66

77
from pre_commit import color
8-
from pre_commit import five
98

109

1110
def get_hook_message(
@@ -60,12 +59,12 @@ def get_hook_message(
6059

6160

6261
def write(s: str, stream: IO[bytes] = sys.stdout.buffer) -> None:
63-
stream.write(five.to_bytes(s))
62+
stream.write(s.encode())
6463
stream.flush()
6564

6665

67-
def write_line(
68-
s: Union[None, str, bytes] = None,
66+
def write_line_b(
67+
s: Optional[bytes] = None,
6968
stream: IO[bytes] = sys.stdout.buffer,
7069
logfile_name: Optional[str] = None,
7170
) -> None:
@@ -77,6 +76,10 @@ def write_line(
7776

7877
for output_stream in output_streams:
7978
if s is not None:
80-
output_stream.write(five.to_bytes(s))
79+
output_stream.write(s)
8180
output_stream.write(b'\n')
8281
output_stream.flush()
82+
83+
84+
def write_line(s: Optional[str] = None, **kwargs: Any) -> None:
85+
write_line_b(s.encode() if s is not None else s, **kwargs)

pre_commit/repository.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from typing import Tuple
1313

1414
import pre_commit.constants as C
15-
from pre_commit import five
1615
from pre_commit.clientlib import load_manifest
1716
from pre_commit.clientlib import LOCAL
1817
from pre_commit.clientlib import MANIFEST_HOOK_DICT
@@ -49,7 +48,7 @@ def _write_state(prefix: Prefix, venv: str, state: object) -> None:
4948
state_filename = _state_filename(prefix, venv)
5049
staging = state_filename + 'staging'
5150
with open(staging, 'w') as state_file:
52-
state_file.write(five.to_text(json.dumps(state)))
51+
state_file.write(json.dumps(state))
5352
# Move the file into place atomically to indicate we've installed
5453
os.rename(staging, state_filename)
5554

pre_commit/util.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from typing import Type
1818
from typing import Union
1919

20-
from pre_commit import five
2120
from pre_commit import parse_shebang
2221

2322
if sys.version_info >= (3, 7): # pragma: no cover (PY37+)
@@ -116,27 +115,17 @@ def __str__(self) -> str:
116115
return self.__bytes__().decode()
117116

118117

119-
def _cmd_kwargs(
120-
*cmd: str,
121-
**kwargs: Any,
122-
) -> Tuple[Tuple[str, ...], Dict[str, Any]]:
123-
# py2/py3 on windows are more strict about the types here
124-
cmd = tuple(five.n(arg) for arg in cmd)
125-
kwargs['env'] = {
126-
five.n(key): five.n(value)
127-
for key, value in kwargs.pop('env', {}).items()
128-
} or None
118+
def _setdefault_kwargs(kwargs: Dict[str, Any]) -> None:
129119
for arg in ('stdin', 'stdout', 'stderr'):
130120
kwargs.setdefault(arg, subprocess.PIPE)
131-
return cmd, kwargs
132121

133122

134123
def cmd_output_b(
135124
*cmd: str,
136125
**kwargs: Any,
137126
) -> Tuple[int, bytes, Optional[bytes]]:
138127
retcode = kwargs.pop('retcode', 0)
139-
cmd, kwargs = _cmd_kwargs(*cmd, **kwargs)
128+
_setdefault_kwargs(kwargs)
140129

141130
try:
142131
cmd = parse_shebang.normalize_cmd(cmd)
@@ -205,7 +194,7 @@ def cmd_output_p(
205194
) -> Tuple[int, bytes, Optional[bytes]]:
206195
assert kwargs.pop('retcode') is None
207196
assert kwargs['stderr'] == subprocess.STDOUT, kwargs['stderr']
208-
cmd, kwargs = _cmd_kwargs(*cmd, **kwargs)
197+
_setdefault_kwargs(kwargs)
209198

210199
try:
211200
cmd = parse_shebang.normalize_cmd(cmd)

tests/conftest.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,9 @@ def get(self):
256256
def cap_out():
257257
stream = FakeStream()
258258
write = functools.partial(output.write, stream=stream)
259-
write_line = functools.partial(output.write_line, stream=stream)
260-
with mock.patch.object(output, 'write', write):
261-
with mock.patch.object(output, 'write_line', write_line):
262-
yield Fixture(stream)
259+
write_line_b = functools.partial(output.write_line_b, stream=stream)
260+
with mock.patch.multiple(output, write=write, write_line_b=write_line_b):
261+
yield Fixture(stream)
263262

264263

265264
@pytest.fixture

tests/repository_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111

1212
import pre_commit.constants as C
13-
from pre_commit import five
1413
from pre_commit.clientlib import CONFIG_SCHEMA
1514
from pre_commit.clientlib import load_manifest
1615
from pre_commit.envcontext import envcontext
@@ -119,7 +118,7 @@ def test_python_hook(tempdir_factory, store):
119118
_test_hook_repo(
120119
tempdir_factory, store, 'python_hooks_repo',
121120
'foo', [os.devnull],
122-
b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
121+
f'[{os.devnull!r}]\nHello World\n'.encode(),
123122
)
124123

125124

@@ -154,7 +153,7 @@ def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store):
154153
_test_hook_repo(
155154
tempdir_factory, store, 'python_hooks_repo',
156155
'foo', [os.devnull],
157-
b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
156+
f'[{os.devnull!r}]\nHello World\n'.encode(),
158157
)
159158

160159

@@ -163,7 +162,7 @@ def test_python_venv(tempdir_factory, store): # pragma: no cover (no venv)
163162
_test_hook_repo(
164163
tempdir_factory, store, 'python_venv_hooks_repo',
165164
'foo', [os.devnull],
166-
b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
165+
f'[{os.devnull!r}]\nHello World\n'.encode(),
167166
)
168167

169168

@@ -188,7 +187,7 @@ def test_versioned_python_hook(tempdir_factory, store):
188187
tempdir_factory, store, 'python3_hooks_repo',
189188
'python3-hook',
190189
[os.devnull],
191-
b"3\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n",
190+
f'3\n[{os.devnull!r}]\nHello World\n'.encode(),
192191
)
193192

194193

0 commit comments

Comments
 (0)