Skip to content

Commit fd9d9d2

Browse files
committed
Add warning to additional keys in config
1 parent 75651dc commit fd9d9d2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

pre_commit/clientlib.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import argparse
55
import functools
6+
import logging
67
import pipes
78
import sys
89

@@ -15,6 +16,8 @@
1516
from pre_commit.languages.all import all_languages
1617
from pre_commit.util import parse_version
1718

19+
logger = logging.getLogger('pre_commit')
20+
1821

1922
def check_type_tag(tag):
2023
if tag not in ALL_TAGS:
@@ -144,6 +147,16 @@ def _entry(modname):
144147
)
145148

146149

150+
def warn_on_unknown_keys_at_top_level(extra, orig_keys):
151+
logger.warning(
152+
'Your pre-commit-config contain these extra keys: {}. '
153+
'while the only valid keys are: {}.'.format(
154+
', '.join(extra),
155+
', '.join(sorted(orig_keys)),
156+
),
157+
),
158+
159+
147160
_meta = (
148161
(
149162
'check-hooks-apply', (
@@ -222,6 +235,10 @@ def _entry(modname):
222235
),
223236

224237
MigrateShaToRev(),
238+
cfgv.WarnAdditionalKeys(
239+
{'repo', 'rev', 'hooks'},
240+
warn_on_unknown_keys_at_top_level,
241+
),
225242
)
226243
DEFAULT_LANGUAGE_VERSION = cfgv.Map(
227244
'DefaultLanguageVersion', None,

tests/clientlib_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import unicode_literals
22

3+
import logging
4+
35
import cfgv
46
import pytest
57

@@ -116,6 +118,27 @@ def test_validate_config_old_list_format_ok(tmpdir):
116118
assert not validate_config_main((f.strpath,))
117119

118120

121+
def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
122+
f = tmpdir.join('cfg.yaml')
123+
f.write(
124+
'- repo: https://gitlab.com/pycqa/flake8\n'
125+
' rev: 3.7.7\n'
126+
' hooks:\n'
127+
' - id: flake8\n'
128+
' args: [--some-args]\n',
129+
)
130+
ret_val = validate_config_main((f.strpath,))
131+
assert not ret_val
132+
assert caplog.record_tuples == [
133+
(
134+
'pre_commit',
135+
logging.WARNING,
136+
'Your pre-commit-config contain these extra keys: args. '
137+
'while the only valid keys are: hooks, repo, rev.',
138+
),
139+
]
140+
141+
119142
@pytest.mark.parametrize('fn', (validate_config_main, validate_manifest_main))
120143
def test_mains_not_ok(tmpdir, fn):
121144
not_yaml = tmpdir.join('f.notyaml')

0 commit comments

Comments
 (0)