Skip to content

Commit 08478ec

Browse files
committed
python 3.9+: use removeprefix
1 parent 23a2b73 commit 08478ec

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

pre_commit/languages/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _find_by_py_launcher(
6565
version: str,
6666
) -> str | None: # pragma: no cover (windows only)
6767
if version.startswith('python'):
68-
num = version[len('python'):]
68+
num = version.removeprefix('python')
6969
cmd = ('py', f'-{num}', '-c', 'import sys; print(sys.executable)')
7070
env = dict(os.environ, PYTHONIOENCODING='UTF-8')
7171
try:
@@ -124,7 +124,7 @@ def _sys_executable_matches(version: str) -> bool:
124124
return False
125125

126126
try:
127-
info = tuple(int(p) for p in version[len('python'):].split('.'))
127+
info = tuple(int(p) for p in version.removeprefix('python').split('.'))
128128
except ValueError:
129129
return False
130130

pre_commit/languages/rust.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def install_environment(
134134

135135
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
136136
for cli_dep in cli_deps:
137-
cli_dep = cli_dep[len('cli:'):]
137+
cli_dep = cli_dep.removeprefix('cli:')
138138
package, _, crate_version = cli_dep.partition(':')
139139
if crate_version != '':
140140
packages_to_install.add((package, '--version', crate_version))

tests/commands/install_uninstall_test.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ def test_install_existing_hooks_no_overwrite(tempdir_factory, store):
349349
# We should run both the legacy and pre-commit hooks
350350
ret, output = _get_commit_output(tempdir_factory)
351351
assert ret == 0
352-
assert output.startswith('legacy hook\n')
353-
NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
352+
legacy = 'legacy hook\n'
353+
assert output.startswith(legacy)
354+
NORMAL_PRE_COMMIT_RUN.assert_matches(output.removeprefix(legacy))
354355

355356

356357
def test_legacy_overwriting_legacy_hook(tempdir_factory, store):
@@ -375,8 +376,9 @@ def test_install_existing_hook_no_overwrite_idempotent(tempdir_factory, store):
375376
# We should run both the legacy and pre-commit hooks
376377
ret, output = _get_commit_output(tempdir_factory)
377378
assert ret == 0
378-
assert output.startswith('legacy hook\n')
379-
NORMAL_PRE_COMMIT_RUN.assert_matches(output[len('legacy hook\n'):])
379+
legacy = 'legacy hook\n'
380+
assert output.startswith(legacy)
381+
NORMAL_PRE_COMMIT_RUN.assert_matches(output.removeprefix(legacy))
380382

381383

382384
def test_install_with_existing_non_utf8_script(tmpdir, store):

tests/store_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_db_repo_name(store):
185185

186186
def test_local_resources_reflects_reality():
187187
on_disk = {
188-
res[len('empty_template_'):]
188+
res.removeprefix('empty_template_')
189189
for res in os.listdir('pre_commit/resources')
190190
if res.startswith('empty_template_')
191191
}

0 commit comments

Comments
 (0)