Skip to content

Commit e7cfc0d

Browse files
authored
Merge pull request #3312 from pre-commit/warning-for-old-stage-names
add warning for deprecated stages names
2 parents eec11bd + 7441a62 commit e7cfc0d

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

pre_commit/clientlib.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,32 @@ def apply_default(self, dct: dict[str, Any]) -> None:
9999
super().apply_default(dct)
100100

101101

102+
class DeprecatedStagesWarning(NamedTuple):
103+
key: str
104+
105+
def check(self, dct: dict[str, Any]) -> None:
106+
if self.key not in dct:
107+
return
108+
109+
val = dct[self.key]
110+
cfgv.check_array(cfgv.check_any)(val)
111+
112+
legacy_stages = [stage for stage in val if stage in _STAGES]
113+
if legacy_stages:
114+
logger.warning(
115+
f'hook id `{dct["id"]}` uses deprecated stage names '
116+
f'({", ".join(legacy_stages)}) which will be removed in a '
117+
f'future version. '
118+
f'run: `pre-commit migrate-config` to automatically fix this.',
119+
)
120+
121+
def apply_default(self, dct: dict[str, Any]) -> None:
122+
pass
123+
124+
def remove_default(self, dct: dict[str, Any]) -> None:
125+
raise NotImplementedError
126+
127+
102128
MANIFEST_HOOK_DICT = cfgv.Map(
103129
'Hook', 'id',
104130

@@ -267,6 +293,12 @@ def check(self, dct: dict[str, Any]) -> None:
267293
raise cfgv.ValidationError(f'{self.key!r} cannot be overridden')
268294

269295

296+
_COMMON_HOOK_WARNINGS = (
297+
OptionalSensibleRegexAtHook('files', cfgv.check_string),
298+
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
299+
DeprecatedStagesWarning('stages'),
300+
)
301+
270302
META_HOOK_DICT = cfgv.Map(
271303
'Hook', 'id',
272304
cfgv.Required('id', cfgv.check_string),
@@ -289,8 +321,7 @@ def check(self, dct: dict[str, Any]) -> None:
289321
item
290322
for item in MANIFEST_HOOK_DICT.items
291323
),
292-
OptionalSensibleRegexAtHook('files', cfgv.check_string),
293-
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
324+
*_COMMON_HOOK_WARNINGS,
294325
)
295326
CONFIG_HOOK_DICT = cfgv.Map(
296327
'Hook', 'id',
@@ -308,16 +339,13 @@ def check(self, dct: dict[str, Any]) -> None:
308339
if item.key != 'stages'
309340
),
310341
StagesMigrationNoDefault('stages', []),
311-
OptionalSensibleRegexAtHook('files', cfgv.check_string),
312-
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
342+
*_COMMON_HOOK_WARNINGS,
313343
)
314344
LOCAL_HOOK_DICT = cfgv.Map(
315345
'Hook', 'id',
316346

317347
*MANIFEST_HOOK_DICT.items,
318-
319-
OptionalSensibleRegexAtHook('files', cfgv.check_string),
320-
OptionalSensibleRegexAtHook('exclude', cfgv.check_string),
348+
*_COMMON_HOOK_WARNINGS,
321349
)
322350
CONFIG_REPO_DICT = cfgv.Map(
323351
'Repository', 'repo',

tests/clientlib_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,32 @@ def test_validate_optional_sensible_regex_at_top_level(caplog, regex, warning):
309309
assert caplog.record_tuples == [('pre_commit', logging.WARNING, warning)]
310310

311311

312+
def test_warning_for_deprecated_stages(caplog):
313+
config_obj = sample_local_config()
314+
config_obj['hooks'][0]['stages'] = ['commit', 'push']
315+
316+
cfgv.validate(config_obj, CONFIG_REPO_DICT)
317+
318+
assert caplog.record_tuples == [
319+
(
320+
'pre_commit',
321+
logging.WARNING,
322+
'hook id `do_not_commit` uses deprecated stage names '
323+
'(commit, push) which will be removed in a future version. '
324+
'run: `pre-commit migrate-config` to automatically fix this.',
325+
),
326+
]
327+
328+
329+
def test_no_warning_for_non_deprecated_stages(caplog):
330+
config_obj = sample_local_config()
331+
config_obj['hooks'][0]['stages'] = ['pre-commit', 'pre-push']
332+
333+
cfgv.validate(config_obj, CONFIG_REPO_DICT)
334+
335+
assert caplog.record_tuples == []
336+
337+
312338
@pytest.mark.parametrize(
313339
'manifest_obj',
314340
(

0 commit comments

Comments
 (0)