X Tutup
Skip to content

Commit aefbe71

Browse files
committed
Clean up calls to .encode() / .decode()
1 parent b2faf33 commit aefbe71

File tree

11 files changed

+17
-18
lines changed

11 files changed

+17
-18
lines changed

pre_commit/error_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FatalError(RuntimeError):
1616

1717

1818
def _to_bytes(exc: BaseException) -> bytes:
19-
return str(exc).encode('UTF-8')
19+
return str(exc).encode()
2020

2121

2222
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:

pre_commit/five.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33

44
def to_text(s: Union[str, bytes]) -> str:
5-
return s if isinstance(s, str) else s.decode('UTF-8')
5+
return s if isinstance(s, str) else s.decode()
66

77

88
def to_bytes(s: Union[str, bytes]) -> bytes:
9-
return s if isinstance(s, bytes) else s.encode('UTF-8')
9+
return s if isinstance(s, bytes) else s.encode()
1010

1111

1212
n = to_text

pre_commit/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def is_in_merge_conflict() -> bool:
6969
def parse_merge_msg_for_conflicts(merge_msg: bytes) -> List[str]:
7070
# Conflicted files start with tabs
7171
return [
72-
line.lstrip(b'#').strip().decode('UTF-8')
72+
line.lstrip(b'#').strip().decode()
7373
for line in merge_msg.splitlines()
7474
# '#\t' for git 2.4.1
7575
if line.startswith((b'\t', b'#\t'))

pre_commit/languages/fail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ def run_hook(
1818
file_args: Sequence[str],
1919
color: bool,
2020
) -> Tuple[int, bytes]:
21-
out = hook.entry.encode('UTF-8') + b'\n\n'
22-
out += b'\n'.join(f.encode('UTF-8') for f in file_args) + b'\n'
21+
out = hook.entry.encode() + b'\n\n'
22+
out += b'\n'.join(f.encode() for f in file_args) + b'\n'
2323
return 1, out

pre_commit/parse_shebang.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class ExecutableNotFoundError(OSError):
1111
def to_output(self) -> Tuple[int, bytes, None]:
12-
return (1, self.args[0].encode('UTF-8'), None)
12+
return (1, self.args[0].encode(), None)
1313

1414

1515
def parse_filename(filename: str) -> Tuple[str, ...]:

pre_commit/resources/hook-tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _norm_exe(exe: str) -> Tuple[str, ...]:
3939
if f.read(2) != b'#!':
4040
return ()
4141
try:
42-
first_line = f.readline().decode('UTF-8')
42+
first_line = f.readline().decode()
4343
except UnicodeDecodeError:
4444
return ()
4545

@@ -77,7 +77,7 @@ def _run_legacy() -> Tuple[int, bytes]:
7777

7878
def _validate_config() -> None:
7979
cmd = ('git', 'rev-parse', '--show-toplevel')
80-
top_level = subprocess.check_output(cmd).decode('UTF-8').strip()
80+
top_level = subprocess.check_output(cmd).decode().strip()
8181
cfg = os.path.join(top_level, CONFIG)
8282
if os.path.isfile(cfg):
8383
pass
@@ -127,7 +127,7 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
127127
remote = sys.argv[1]
128128

129129
opts: Tuple[str, ...] = ()
130-
for line in stdin.decode('UTF-8').splitlines():
130+
for line in stdin.decode().splitlines():
131131
_, local_sha, _, remote_sha = line.split()
132132
if local_sha == Z40:
133133
continue

pre_commit/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ def _indent_or_none(part: Optional[bytes]) -> bytes:
109109
'return code: {}\n'
110110
'expected return code: {}\n'.format(
111111
self.cmd, self.returncode, self.expected_returncode,
112-
).encode('UTF-8'),
112+
).encode(),
113113
b'stdout:', _indent_or_none(self.stdout), b'\n',
114114
b'stderr:', _indent_or_none(self.stderr),
115115
))
116116

117117
def __str__(self) -> str:
118-
return self.__bytes__().decode('UTF-8')
118+
return self.__bytes__().decode()
119119

120120

121121
def _cmd_kwargs(
@@ -157,8 +157,8 @@ def cmd_output_b(
157157

158158
def cmd_output(*cmd: str, **kwargs: Any) -> Tuple[int, str, Optional[str]]:
159159
returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
160-
stdout = stdout_b.decode('UTF-8') if stdout_b is not None else None
161-
stderr = stderr_b.decode('UTF-8') if stderr_b is not None else None
160+
stdout = stdout_b.decode() if stdout_b is not None else None
161+
stderr = stderr_b.decode() if stderr_b is not None else None
162162
return returncode, stdout, stderr
163163

164164

pre_commit/xargs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ def _command_length(*cmd: str) -> int:
4949
# win32 uses the amount of characters, more details at:
5050
# https://github.com/pre-commit/pre-commit/pull/839
5151
if sys.platform == 'win32':
52-
# the python2.x apis require bytes, we encode as UTF-8
5352
return len(full_cmd.encode('utf-16le')) // 2
5453
else:
5554
return len(full_cmd.encode(sys.getfilesystemencoding()))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
# Intentionally write mixed encoding to the output. This should not crash
33
# pre-commit and should write bytes to the output.
4-
# '☃'.encode('UTF-8') + '²'.encode('latin1')
4+
# '☃'.encode() + '²'.encode('latin1')
55
echo -e '\xe2\x98\x83\xb2'
66
# exit 1 to trigger printing
77
exit 1

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def get_bytes(self):
249249

250250
def get(self):
251251
"""Get the output assuming it was written as UTF-8 bytes"""
252-
return self.get_bytes().decode('UTF-8')
252+
return self.get_bytes().decode()
253253

254254

255255
@pytest.fixture

0 commit comments

Comments
 (0)
X Tutup