Skip to content

Commit b609368

Browse files
authored
Merge pull request pre-commit#2746 from pre-commit/deprecate-python-venv
deprecate python_venv language
2 parents 0359fae + 0c1267b commit b609368

File tree

8 files changed

+73
-29
lines changed

8 files changed

+73
-29
lines changed

pre_commit/commands/migrate_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def _migrate_sha_to_rev(contents: str) -> str:
4242
return re.sub(r'(\n\s+)sha:', r'\1rev:', contents)
4343

4444

45+
def _migrate_python_venv(contents: str) -> str:
46+
return re.sub(
47+
r'(\n\s+)language: python_venv\b',
48+
r'\1language: python',
49+
contents,
50+
)
51+
52+
4553
def migrate_config(config_file: str, quiet: bool = False) -> int:
4654
with open(config_file) as f:
4755
orig_contents = contents = f.read()
@@ -55,6 +63,7 @@ def migrate_config(config_file: str, quiet: bool = False) -> int:
5563

5664
contents = _migrate_map(contents)
5765
contents = _migrate_sha_to_rev(contents)
66+
contents = _migrate_python_venv(contents)
5867

5968
if contents != orig_contents:
6069
with open(config_file, 'w') as f:

pre_commit/repository.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import os
6+
import shlex
67
from typing import Any
78
from typing import Sequence
89

@@ -68,6 +69,14 @@ def _hook_install(hook: Hook) -> None:
6869
logger.info('Once installed this environment will be reused.')
6970
logger.info('This may take a few minutes...')
7071

72+
if hook.language == 'python_venv':
73+
logger.warning(
74+
f'`repo: {hook.src}` uses deprecated `language: python_venv`. '
75+
f'This is an alias for `language: python`. '
76+
f'Often `pre-commit autoupdate --repo {shlex.quote(hook.src)}` '
77+
f'will fix this.',
78+
)
79+
7180
lang = languages[hook.language]
7281
assert lang.ENVIRONMENT_DIR is not None
7382

testing/resources/python_venv_hooks_repo/.pre-commit-hooks.yaml

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

testing/resources/python_venv_hooks_repo/foo.py

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

testing/resources/python_venv_hooks_repo/setup.py

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

tests/commands/migrate_config_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,39 @@ def test_migrate_config_sha_to_rev(tmpdir):
134134
)
135135

136136

137+
def test_migrate_config_language_python_venv(tmp_path):
138+
src = '''\
139+
repos:
140+
- repo: local
141+
hooks:
142+
- id: example
143+
name: example
144+
entry: example
145+
language: python_venv
146+
- id: example
147+
name: example
148+
entry: example
149+
language: system
150+
'''
151+
expected = '''\
152+
repos:
153+
- repo: local
154+
hooks:
155+
- id: example
156+
name: example
157+
entry: example
158+
language: python
159+
- id: example
160+
name: example
161+
entry: example
162+
language: system
163+
'''
164+
cfg = tmp_path.joinpath('cfg.yaml')
165+
cfg.write_text(src)
166+
assert migrate_config(str(cfg)) == 0
167+
assert cfg.read_text() == expected
168+
169+
137170
def test_migrate_config_invalid_yaml(tmpdir):
138171
contents = '['
139172
cfg = tmpdir.join(C.CONFIG_FILE)

tests/languages/all_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from __future__ import annotations
2+
3+
from pre_commit.languages.all import languages
4+
5+
6+
def test_python_venv_is_an_alias_to_python():
7+
assert languages['python_venv'] is languages['python']

tests/repository_test.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,21 @@ def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store):
129129
)
130130

131131

132-
def test_python_venv(tempdir_factory, store):
133-
_test_hook_repo(
134-
tempdir_factory, store, 'python_venv_hooks_repo',
135-
'foo', [os.devnull],
136-
f'[{os.devnull!r}]\nHello World\n'.encode(),
132+
def test_python_venv_deprecation(store, caplog):
133+
config = {
134+
'repo': 'local',
135+
'hooks': [{
136+
'id': 'example',
137+
'name': 'example',
138+
'language': 'python_venv',
139+
'entry': 'echo hi',
140+
}],
141+
}
142+
_get_hook(config, store, 'example')
143+
assert caplog.messages[-1] == (
144+
'`repo: local` uses deprecated `language: python_venv`. '
145+
'This is an alias for `language: python`. '
146+
'Often `pre-commit autoupdate --repo local` will fix this.'
137147
)
138148

139149

0 commit comments

Comments
 (0)