Repository: pre-commit/pre-commit
Pull request #1048 (2019-05-27 20:38:17)

fixes for cfgv>=2
See https://github.com/asottile/cfgv/pull/18 for context

I wanted to improve the warning messages here to include some context


Patch:
diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py
index c16a3ac..14a22b9 100644
--- a/pre_commit/clientlib.py
+++ b/pre_commit/clientlib.py
@@ -149,10 +149,16 @@ def _entry(modname):
     )
 
 
-def warn_unknown_keys(extra, orig_keys):
+def warn_unknown_keys_root(extra, orig_keys, dct):
     logger.warning(
-        'Unexpected config key(s): {}'.format(
-            ', '.join(sorted(extra)),
+        'Unexpected key(s) present at root: {}'.format(', '.join(extra)),
+    )
+
+
+def warn_unknown_keys_repo(extra, orig_keys, dct):
+    logger.warning(
+        'Unexpected key(s) present on {}: {}'.format(
+            dct['repo'], ', '.join(extra),
         ),
     )
 
@@ -235,10 +241,7 @@ CONFIG_REPO_DICT = cfgv.Map(
     ),
 
     MigrateShaToRev(),
-    cfgv.WarnAdditionalKeys(
-        ('repo', 'rev', 'hooks'),
-        warn_unknown_keys,
-    ),
+    cfgv.WarnAdditionalKeys(('repo', 'rev', 'hooks'), warn_unknown_keys_repo),
 )
 DEFAULT_LANGUAGE_VERSION = cfgv.Map(
     'DefaultLanguageVersion', None,
@@ -273,7 +276,7 @@ CONFIG_SCHEMA = cfgv.Map(
             'fail_fast',
             'minimum_pre_commit_version',
         ),
-        warn_unknown_keys,
+        warn_unknown_keys_root,
     ),
 )
 
diff --git a/pre_commit/repository.py b/pre_commit/repository.py
index 1d92d75..5b12a98 100644
--- a/pre_commit/repository.py
+++ b/pre_commit/repository.py
@@ -105,7 +105,7 @@ class Hook(collections.namedtuple('Hook', ('src', 'prefix') + _KEYS)):
         extra_keys = set(dct) - set(_KEYS)
         if extra_keys:
             logger.warning(
-                'Unexpected keys present on {} => {}: '
+                'Unexpected key(s) present on {} => {}: '
                 '{}'.format(src, dct['id'], ', '.join(sorted(extra_keys))),
             )
         return cls(src=src, prefix=prefix, **{k: dct[k] for k in _KEYS})
diff --git a/setup.cfg b/setup.cfg
index a87108d..eca74cc 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -25,7 +25,7 @@ classifiers =
 packages = find:
 install_requires =
     aspy.yaml
-    cfgv>=1.4.0
+    cfgv>=2.0.0
     identify>=1.0.0
     importlib-metadata
     nodeenv>=0.11.1


Test patch:
diff --git a/tests/clientlib_test.py b/tests/clientlib_test.py
index 13b42a5..6174889 100644
--- a/tests/clientlib_test.py
+++ b/tests/clientlib_test.py
@@ -133,7 +133,8 @@ def test_validate_warn_on_unknown_keys_at_repo_level(tmpdir, caplog):
         (
             'pre_commit',
             logging.WARNING,
-            'Unexpected config key(s): args',
+            'Unexpected key(s) present on https://gitlab.com/pycqa/flake8: '
+            'args',
         ),
     ]
 
@@ -155,7 +156,7 @@ def test_validate_warn_on_unknown_keys_at_top_level(tmpdir, caplog):
         (
             'pre_commit',
             logging.WARNING,
-            'Unexpected config key(s): foo',
+            'Unexpected key(s) present at root: foo',
         ),
     ]
 
diff --git a/tests/repository_test.py b/tests/repository_test.py
index a2a9bb5..97fcba0 100644
--- a/tests/repository_test.py
+++ b/tests/repository_test.py
@@ -588,7 +588,7 @@ def test_unknown_keys(store, fake_log_handler):
         }],
     }
     _get_hook(config, store, 'too-much')
-    expected = 'Unexpected keys present on local => too-much: foo, hello'
+    expected = 'Unexpected key(s) present on local => too-much: foo, hello'
     assert fake_log_handler.handle.call_args[0][0].msg == expected