Skip to content

Commit 33e020f

Browse files
committed
add warning for deprecated stages values in default_stages
1 parent e7cfc0d commit 33e020f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

pre_commit/clientlib.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ def remove_default(self, dct: dict[str, Any]) -> None:
125125
raise NotImplementedError
126126

127127

128+
class DeprecatedDefaultStagesWarning(NamedTuple):
129+
key: str
130+
131+
def check(self, dct: dict[str, Any]) -> None:
132+
if self.key not in dct:
133+
return
134+
135+
val = dct[self.key]
136+
cfgv.check_array(cfgv.check_any)(val)
137+
138+
legacy_stages = [stage for stage in val if stage in _STAGES]
139+
if legacy_stages:
140+
logger.warning(
141+
f'top-level `default_stages` uses deprecated stage names '
142+
f'({", ".join(legacy_stages)}) which will be removed in a '
143+
f'future version. '
144+
f'run: `pre-commit migrate-config` to automatically fix this.',
145+
)
146+
147+
def apply_default(self, dct: dict[str, Any]) -> None:
148+
pass
149+
150+
def remove_default(self, dct: dict[str, Any]) -> None:
151+
raise NotImplementedError
152+
153+
128154
MANIFEST_HOOK_DICT = cfgv.Map(
129155
'Hook', 'id',
130156

@@ -398,6 +424,7 @@ def check(self, dct: dict[str, Any]) -> None:
398424
'default_language_version', DEFAULT_LANGUAGE_VERSION, {},
399425
),
400426
StagesMigration('default_stages', STAGES),
427+
DeprecatedDefaultStagesWarning('default_stages'),
401428
cfgv.Optional('files', check_string_regex, ''),
402429
cfgv.Optional('exclude', check_string_regex, '^$'),
403430
cfgv.Optional('fail_fast', cfgv.check_bool, False),

tests/clientlib_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,30 @@ def test_no_warning_for_non_deprecated_stages(caplog):
335335
assert caplog.record_tuples == []
336336

337337

338+
def test_warning_for_deprecated_default_stages(caplog):
339+
cfg = {'default_stages': ['commit', 'push'], 'repos': []}
340+
341+
cfgv.validate(cfg, CONFIG_SCHEMA)
342+
343+
assert caplog.record_tuples == [
344+
(
345+
'pre_commit',
346+
logging.WARNING,
347+
'top-level `default_stages` uses deprecated stage names '
348+
'(commit, push) which will be removed in a future version. '
349+
'run: `pre-commit migrate-config` to automatically fix this.',
350+
),
351+
]
352+
353+
354+
def test_no_warning_for_non_deprecated_default_stages(caplog):
355+
cfg = {'default_stages': ['pre-commit', 'pre-push'], 'repos': []}
356+
357+
cfgv.validate(cfg, CONFIG_SCHEMA)
358+
359+
assert caplog.record_tuples == []
360+
361+
338362
@pytest.mark.parametrize(
339363
'manifest_obj',
340364
(

0 commit comments

Comments
 (0)