nitpick.plugins.pre_commit module

Enforce configuration for .pre-commit-config.yaml.

class nitpick.plugins.pre_commit.PreCommitHook(repo: str, hook_id: str, yaml: nitpick.formats.YAMLFormat)[source]

Bases: object

A pre-commit hook.

Method generated by attrs for class PreCommitHook.

classmethod get_all_hooks_from(str_or_yaml: Union[str, ruamel.yaml.comments.CommentedSeq, ruamel.yaml.comments.CommentedMap])[source]

Get all hooks from a YAML string. Split the string in hooks and copy the repo info for each.

property key_value_pair: Tuple[str, nitpick.plugins.pre_commit.PreCommitHook]

Key/value pair to be used as a dict item.

property single_hook: Dict[str, Any]

Return only a single hook instead of a list.

property unique_key: str

Unique key of this hook, to be used in a dict.

class nitpick.plugins.pre_commit.PreCommitPlugin(info:, expected_config: Dict[str, Any], fix=False)[source]

Bases: nitpick.plugins.base.NitpickPlugin

Enforce configuration for .pre-commit-config.yaml.

Style example: the default pre-commit hooks.

actual_hooks: Dict[str, nitpick.plugins.pre_commit.PreCommitHook] = {}
actual_hooks_by_index: List[str] = []
actual_hooks_by_key: Dict[str, int] = {}
actual_yaml: nitpick.formats.YAMLFormat
can_fix: bool = False

Can this plugin modify files directly?

dirty: bool
enforce_hooks() Iterator[nitpick.violations.Fuss][source]

Enforce the repositories configured in pre-commit.

enforce_repo_block(expected_repo_block: collections.OrderedDict) Iterator[nitpick.violations.Fuss][source]

Enforce a repo with a YAML string configuration.

enforce_repo_old_format(index: int, repo_data: collections.OrderedDict) Iterator[nitpick.violations.Fuss][source]

Enforce repos using the old deprecated format with hooks and repo keys.

enforce_rules() Iterator[nitpick.violations.Fuss][source]

Enforce rules for the pre-commit hooks.

entry_point() Iterator[nitpick.violations.Fuss]

Entry point of the Nitpick plugin.

expected_config: JsonDict
file_path: Path
filename = '.pre-commit-config.yaml'
static format_hook(expected_dict) str[source]

Format the hook so it’s easy to copy and paste it to the .yaml file: ID goes first, indent with spaces.

classmethod get_compiled_jmespath_filenames()

Return a compiled JMESPath expression for file names, using the class name as part of the key.

identify_tags: Set[str] = {}

Which identify tags this nitpick.plugins.base.NitpickPlugin child recognises.


Hook for plugin initialization after the instance was created.

property initial_contents: str

Suggest the initial content for this missing file.

property nitpick_file_dict: Dict[str, Any]

Nitpick configuration for this file as a TOML dict, taken from the style file.

skip_empty_suggestion = False
validation_schema: Optional[Schema] = None

Nested validation field for this file, to be applied in runtime when the validation schema is rebuilt. Useful when you have a strict configuration for a file type (e.g. nitpick.plugins.json.JSONPlugin).

violation_base_code: int = 330
warn_missing_different(comparison: nitpick.formats.Comparison, prefix: str = '') Iterator[nitpick.violations.Fuss]

Warn about missing and different keys.

write_file(file_exists: bool) Optional[nitpick.violations.Fuss]

Hook to write the new file when fix mode is on. Should be used by inherited classes.

class nitpick.plugins.pre_commit.Violations(value)[source]

Bases: nitpick.violations.ViolationEnum

Violations for this plugin.

HOOK_NOT_FOUND = (332, ': hook {id!r} not found. Use this:')
MISSING_HOOK_WITH_ID = (337, ': missing hook with id {id!r}:')
MISSING_KEY_IN_HOOK = (336, ': style file is missing {key!r} in hook:')
MISSING_KEY_IN_REPO = (334, ': missing {key!r} in repo {repo!r}')
NO_ROOT_KEY = (331, " doesn't have the 'repos' root key")
REPO_DOES_NOT_EXIST = (333, ': repo {repo!r} does not exist under {key!r}')
STYLE_FILE_MISSING_NAME = (335, ': style file is missing {key!r} in repo {repo!r}')
STYLE_MISSING_INDEX = (332, ': style file is missing {key!r} key in repo #{index}')
nitpick.plugins.pre_commit.can_handle(info: Optional[Type[nitpick.plugins.base.NitpickPlugin]][source]

Handle pre-commit config file.

nitpick.plugins.pre_commit.plugin_class() Type[nitpick.plugins.base.NitpickPlugin][source]

Handle pre-commit config file.