mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-27 11:28:59 -06:00
Add types to pre-commit
This commit is contained in:
@@ -3,6 +3,10 @@ import functools
|
||||
import logging
|
||||
import pipes
|
||||
import sys
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
from typing import Optional
|
||||
from typing import Sequence
|
||||
|
||||
import cfgv
|
||||
from aspy.yaml import ordered_load
|
||||
@@ -18,7 +22,7 @@ logger = logging.getLogger('pre_commit')
|
||||
check_string_regex = cfgv.check_and(cfgv.check_string, cfgv.check_regex)
|
||||
|
||||
|
||||
def check_type_tag(tag):
|
||||
def check_type_tag(tag: str) -> None:
|
||||
if tag not in ALL_TAGS:
|
||||
raise cfgv.ValidationError(
|
||||
'Type tag {!r} is not recognized. '
|
||||
@@ -26,7 +30,7 @@ def check_type_tag(tag):
|
||||
)
|
||||
|
||||
|
||||
def check_min_version(version):
|
||||
def check_min_version(version: str) -> None:
|
||||
if parse_version(version) > parse_version(C.VERSION):
|
||||
raise cfgv.ValidationError(
|
||||
'pre-commit version {} is required but version {} is installed. '
|
||||
@@ -36,7 +40,7 @@ def check_min_version(version):
|
||||
)
|
||||
|
||||
|
||||
def _make_argparser(filenames_help):
|
||||
def _make_argparser(filenames_help: str) -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='*', help=filenames_help)
|
||||
parser.add_argument('-V', '--version', action='version', version=C.VERSION)
|
||||
@@ -86,7 +90,7 @@ load_manifest = functools.partial(
|
||||
)
|
||||
|
||||
|
||||
def validate_manifest_main(argv=None):
|
||||
def validate_manifest_main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = _make_argparser('Manifest filenames.')
|
||||
args = parser.parse_args(argv)
|
||||
ret = 0
|
||||
@@ -107,7 +111,7 @@ class MigrateShaToRev:
|
||||
key = 'rev'
|
||||
|
||||
@staticmethod
|
||||
def _cond(key):
|
||||
def _cond(key: str) -> cfgv.Conditional:
|
||||
return cfgv.Conditional(
|
||||
key, cfgv.check_string,
|
||||
condition_key='repo',
|
||||
@@ -115,7 +119,7 @@ class MigrateShaToRev:
|
||||
ensure_absent=True,
|
||||
)
|
||||
|
||||
def check(self, dct):
|
||||
def check(self, dct: Dict[str, Any]) -> None:
|
||||
if dct.get('repo') in {LOCAL, META}:
|
||||
self._cond('rev').check(dct)
|
||||
self._cond('sha').check(dct)
|
||||
@@ -126,14 +130,14 @@ class MigrateShaToRev:
|
||||
else:
|
||||
self._cond('rev').check(dct)
|
||||
|
||||
def apply_default(self, dct):
|
||||
def apply_default(self, dct: Dict[str, Any]) -> None:
|
||||
if 'sha' in dct:
|
||||
dct['rev'] = dct.pop('sha')
|
||||
|
||||
remove_default = cfgv.Required.remove_default
|
||||
|
||||
|
||||
def _entry(modname):
|
||||
def _entry(modname: str) -> str:
|
||||
"""the hook `entry` is passed through `shlex.split()` by the command
|
||||
runner, so to prevent issues with spaces and backslashes (on Windows)
|
||||
it must be quoted here.
|
||||
@@ -143,13 +147,21 @@ def _entry(modname):
|
||||
)
|
||||
|
||||
|
||||
def warn_unknown_keys_root(extra, orig_keys, dct):
|
||||
def warn_unknown_keys_root(
|
||||
extra: Sequence[str],
|
||||
orig_keys: Sequence[str],
|
||||
dct: Dict[str, str],
|
||||
) -> None:
|
||||
logger.warning(
|
||||
'Unexpected key(s) present at root: {}'.format(', '.join(extra)),
|
||||
)
|
||||
|
||||
|
||||
def warn_unknown_keys_repo(extra, orig_keys, dct):
|
||||
def warn_unknown_keys_repo(
|
||||
extra: Sequence[str],
|
||||
orig_keys: Sequence[str],
|
||||
dct: Dict[str, str],
|
||||
) -> None:
|
||||
logger.warning(
|
||||
'Unexpected key(s) present on {}: {}'.format(
|
||||
dct['repo'], ', '.join(extra),
|
||||
@@ -281,7 +293,7 @@ class InvalidConfigError(FatalError):
|
||||
pass
|
||||
|
||||
|
||||
def ordered_load_normalize_legacy_config(contents):
|
||||
def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]:
|
||||
data = ordered_load(contents)
|
||||
if isinstance(data, list):
|
||||
# TODO: Once happy, issue a deprecation warning and instructions
|
||||
@@ -298,7 +310,7 @@ load_config = functools.partial(
|
||||
)
|
||||
|
||||
|
||||
def validate_config_main(argv=None):
|
||||
def validate_config_main(argv: Optional[Sequence[str]] = None) -> int:
|
||||
parser = _make_argparser('Config filenames.')
|
||||
args = parser.parse_args(argv)
|
||||
ret = 0
|
||||
|
||||
Reference in New Issue
Block a user