Skip to content

Check unsupported TOP_LEVEL_KEYS - #980

Closed
mitzkia wants to merge 1 commit into
pre-commit:masterfrom
mitzkia:unexpected_top_level_key
Closed

mitzkia wants to merge 1 commit into
pre-commit:masterfrom
mitzkia:unexpected_top_level_key

Conversation

@mitzkia

@mitzkia mitzkia commented Mar 25, 2019

Copy link
Copy Markdown

This is a fix for: #965

Of course I will also add unit-tests here.

Signed-off-by: Andras Mitzki andras.mitzki@balabit.com

Signed-off-by: Andras Mitzki <andras.mitzki@balabit.com>
@mitzkia

mitzkia commented Mar 25, 2019 •

Copy link
Copy Markdown
Author

Maybe the warning message is not the best, not concrete.

@mitzkia

mitzkia commented Mar 25, 2019

Copy link
Copy Markdown
Author

I was also thinking about the exiting if there is an unexpected top level key.
Could you help me to decide?

Comment thread pre_commit/repository.py
)


_TOP_LEVEL_KEYS = ('repo', 'rev', 'hooks')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally this should be derived from CONFIG_REPO_SCHEMA in pre_commit.clientlib -- it probably also makes sense to implement this check as a cfgv checker in that module

also these aren't top level keys, just repository-level keys

Comment thread pre_commit/repository.py


def _check_extra_top_level_keys(repo_config):
extra_top_level_keys = set(dict(repo_config).keys()) - set(_TOP_LEVEL_KEYS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's 4 unnecessary copies happening here:

  1. dict(repo_config)
  2. .keys() (in python 2)
  3. set(dict...)
  4. set(_TOP_LEVEL_KEYS)

The module constant should be frozenset((...)), you can use set(repo_config) directly

Comment thread pre_commit/repository.py
if extra_top_level_keys:
logger.warning(
'Unexpected top level keys present: {}, only supported'
'top level keys: {}'.format(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this produces a not-great error message that contains python literals, use ', '.join(sorted(...)) when displaying lists

Comment thread pre_commit/repository.py
extra_top_level_keys = set(dict(repo_config).keys()) - set(_TOP_LEVEL_KEYS)
if extra_top_level_keys:
logger.warning(
'Unexpected top level keys present: {}, only supported'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use consistent messaging with the warning on hook-dict extra keys (it's in this module under create)

@mitzkia

mitzkia commented Mar 25, 2019

Copy link
Copy Markdown
Author

Thanks, for the review notes. I will fix them.
One more question: should not we exit if there is an unexpected key, or we can run forward?

@asottile

Copy link
Copy Markdown
Member

Thanks, for the review notes. I will fix them.
One more question: should not we exit if there is an unexpected key, or we can run forward?

can't error without warning first, don't want to break 5 years of behaviour

@mitzkia

mitzkia commented Mar 25, 2019 •

Copy link
Copy Markdown
Author

I like to add this check into pre_commit.clientlib
Just found cfgv.NoAdditionalKeys(): which is a checker and proper message also displayed, maybe this is the function what we need here.
But it does exit.

@asottile

Copy link
Copy Markdown
Member

yeah you'd want something like that but WarnAdditionalKeys instead

@asottile

asottile commented Apr 1, 2019

Copy link
Copy Markdown
Member

@mitzkia with cfgv 1.6 you should be able to finish this 👍

@asottile

Copy link
Copy Markdown
Member

via #1028

@asottile asottile closed this May 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants