Skip to content

Commit

Permalink
Merge pull request pre-commit#3313 from pre-commit/default-stages-war…
Browse files Browse the repository at this point in the history
…ning

add warning for deprecated stages values in `default_stages`
  • Loading branch information
asottile authored Oct 1, 2024
2 parents e7cfc0d + 33e020f commit 05e365f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pre_commit/clientlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ def remove_default(self, dct: dict[str, Any]) -> None:
raise NotImplementedError


class DeprecatedDefaultStagesWarning(NamedTuple):
key: str

def check(self, dct: dict[str, Any]) -> None:
if self.key not in dct:
return

val = dct[self.key]
cfgv.check_array(cfgv.check_any)(val)

legacy_stages = [stage for stage in val if stage in _STAGES]
if legacy_stages:
logger.warning(
f'top-level `default_stages` uses deprecated stage names '
f'({", ".join(legacy_stages)}) which will be removed in a '
f'future version. '
f'run: `pre-commit migrate-config` to automatically fix this.',
)

def apply_default(self, dct: dict[str, Any]) -> None:
pass

def remove_default(self, dct: dict[str, Any]) -> None:
raise NotImplementedError


MANIFEST_HOOK_DICT = cfgv.Map(
'Hook', 'id',

Expand Down Expand Up @@ -398,6 +424,7 @@ def check(self, dct: dict[str, Any]) -> None:
'default_language_version', DEFAULT_LANGUAGE_VERSION, {},
),
StagesMigration('default_stages', STAGES),
DeprecatedDefaultStagesWarning('default_stages'),
cfgv.Optional('files', check_string_regex, ''),
cfgv.Optional('exclude', check_string_regex, '^$'),
cfgv.Optional('fail_fast', cfgv.check_bool, False),
Expand Down
24 changes: 24 additions & 0 deletions tests/clientlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,30 @@ def test_no_warning_for_non_deprecated_stages(caplog):
assert caplog.record_tuples == []


def test_warning_for_deprecated_default_stages(caplog):
cfg = {'default_stages': ['commit', 'push'], 'repos': []}

cfgv.validate(cfg, CONFIG_SCHEMA)

assert caplog.record_tuples == [
(
'pre_commit',
logging.WARNING,
'top-level `default_stages` uses deprecated stage names '
'(commit, push) which will be removed in a future version. '
'run: `pre-commit migrate-config` to automatically fix this.',
),
]


def test_no_warning_for_non_deprecated_default_stages(caplog):
cfg = {'default_stages': ['pre-commit', 'pre-push'], 'repos': []}

cfgv.validate(cfg, CONFIG_SCHEMA)

assert caplog.record_tuples == []


@pytest.mark.parametrize(
'manifest_obj',
(
Expand Down

0 comments on commit 05e365f

Please sign in to comment.