| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,6 +25,10 @@ exclude_lines = | |||
| 25 | 25 | ^\s*return NotImplemented\b | |
| 26 | 26 | ^\s*raise$ | |
| 27 | 27 | ||
| 28 | + # Ignore typing-related things | ||
| 29 | + ^if (False|TYPE_CHECKING): | ||
| 30 | + : \.\.\.$ | ||
| 31 | + | ||
| 28 | 32 | # Don't complain if non-runnable code isn't run: | |
| 29 | 33 | ^if __name__ == ['"]__main__['"]:$ | |
| 30 | 34 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,10 @@ | |||
| 3 | 3 | import logging | |
| 4 | 4 | import pipes | |
| 5 | 5 | import sys | |
| 6 | + from typing import Any | ||
| 7 | + from typing import Dict | ||
| 8 | + from typing import Optional | ||
| 9 | + from typing import Sequence | ||
| 6 | 10 | ||
| 7 | 11 | import cfgv | |
| 8 | 12 | from aspy.yaml import ordered_load | |
@@ -18,15 +22,15 @@ | |||
| 18 | 22 | check_string_regex = cfgv.check_and(cfgv.check_string, cfgv.check_regex) | |
| 19 | 23 | ||
| 20 | 24 | ||
| 21 | - def check_type_tag(tag): | ||
| 25 | + def check_type_tag(tag: str) -> None: | ||
| 22 | 26 | if tag not in ALL_TAGS: | |
| 23 | 27 | raise cfgv.ValidationError( | |
| 24 | 28 | 'Type tag {!r} is not recognized. ' | |
| 25 | 29 | 'Try upgrading identify and pre-commit?'.format(tag), | |
| 26 | 30 | ) | |
| 27 | 31 | ||
| 28 | 32 | ||
| 29 | - def check_min_version(version): | ||
| 33 | + def check_min_version(version: str) -> None: | ||
| 30 | 34 | if parse_version(version) > parse_version(C.VERSION): | |
| 31 | 35 | raise cfgv.ValidationError( | |
| 32 | 36 | 'pre-commit version {} is required but version {} is installed. ' | |
@@ -36,7 +40,7 @@ def check_min_version(version): | |||
| 36 | 40 | ) | |
| 37 | 41 | ||
| 38 | 42 | ||
| 39 | - def _make_argparser(filenames_help): | ||
| 43 | + def _make_argparser(filenames_help: str) -> argparse.ArgumentParser: | ||
| 40 | 44 | parser = argparse.ArgumentParser() | |
| 41 | 45 | parser.add_argument('filenames', nargs='*', help=filenames_help) | |
| 42 | 46 | parser.add_argument('-V', '--version', action='version', version=C.VERSION) | |
@@ -86,7 +90,7 @@ class InvalidManifestError(FatalError): | |||
| 86 | 90 | ) | |
| 87 | 91 | ||
| 88 | 92 | ||
| 89 | - def validate_manifest_main(argv=None): | ||
| 93 | + def validate_manifest_main(argv: Optional[Sequence[str]] = None) -> int: | ||
| 90 | 94 | parser = _make_argparser('Manifest filenames.') | |
| 91 | 95 | args = parser.parse_args(argv) | |
| 92 | 96 | ret = 0 | |
@@ -107,15 +111,15 @@ class MigrateShaToRev: | |||
| 107 | 111 | key = 'rev' | |
| 108 | 112 | ||
| 109 | 113 | @staticmethod | |
| 110 | - def _cond(key): | ||
| 114 | + def _cond(key: str) -> cfgv.Conditional: | ||
| 111 | 115 | return cfgv.Conditional( | |
| 112 | 116 | key, cfgv.check_string, | |
| 113 | 117 | condition_key='repo', | |
| 114 | 118 | condition_value=cfgv.NotIn(LOCAL, META), | |
| 115 | 119 | ensure_absent=True, | |
| 116 | 120 | ) | |
| 117 | 121 | ||
| 118 | - def check(self, dct): | ||
| 122 | + def check(self, dct: Dict[str, Any]) -> None: | ||
| 119 | 123 | if dct.get('repo') in {LOCAL, META}: | |
| 120 | 124 | self._cond('rev').check(dct) | |
| 121 | 125 | self._cond('sha').check(dct) | |
@@ -126,14 +130,14 @@ def check(self, dct): | |||
| 126 | 130 | else: | |
| 127 | 131 | self._cond('rev').check(dct) | |
| 128 | 132 | ||
| 129 | - def apply_default(self, dct): | ||
| 133 | + def apply_default(self, dct: Dict[str, Any]) -> None: | ||
| 130 | 134 | if 'sha' in dct: | |
| 131 | 135 | dct['rev'] = dct.pop('sha') | |
| 132 | 136 | ||
| 133 | 137 | remove_default = cfgv.Required.remove_default | |
| 134 | 138 | ||
| 135 | 139 | ||
| 136 | - def _entry(modname): | ||
| 140 | + def _entry(modname: str) -> str: | ||
| 137 | 141 | """the hook `entry` is passed through `shlex.split()` by the command | |
| 138 | 142 | runner, so to prevent issues with spaces and backslashes (on Windows) | |
| 139 | 143 | it must be quoted here. | |
@@ -143,13 +147,21 @@ def _entry(modname): | |||
| 143 | 147 | ) | |
| 144 | 148 | ||
| 145 | 149 | ||
| 146 | - def warn_unknown_keys_root(extra, orig_keys, dct): | ||
| 150 | + def warn_unknown_keys_root( | ||
| 151 | + extra: Sequence[str], | ||
| 152 | + orig_keys: Sequence[str], | ||
| 153 | + dct: Dict[str, str], | ||
| 154 | + ) -> None: | ||
| 147 | 155 | logger.warning( | |
| 148 | 156 | 'Unexpected key(s) present at root: {}'.format(', '.join(extra)), | |
| 149 | 157 | ) | |
| 150 | 158 | ||
| 151 | 159 | ||
| 152 | - def warn_unknown_keys_repo(extra, orig_keys, dct): | ||
| 160 | + def warn_unknown_keys_repo( | ||
| 161 | + extra: Sequence[str], | ||
| 162 | + orig_keys: Sequence[str], | ||
| 163 | + dct: Dict[str, str], | ||
| 164 | + ) -> None: | ||
| 153 | 165 | logger.warning( | |
| 154 | 166 | 'Unexpected key(s) present on {}: {}'.format( | |
| 155 | 167 | dct['repo'], ', '.join(extra), | |
@@ -281,7 +293,7 @@ class InvalidConfigError(FatalError): | |||
| 281 | 293 | pass | |
| 282 | 294 | ||
| 283 | 295 | ||
| 284 | - def ordered_load_normalize_legacy_config(contents): | ||
| 296 | + def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]: | ||
| 285 | 297 | data = ordered_load(contents) | |
| 286 | 298 | if isinstance(data, list): | |
| 287 | 299 | # TODO: Once happy, issue a deprecation warning and instructions | |
@@ -298,7 +310,7 @@ def ordered_load_normalize_legacy_config(contents): | |||
| 298 | 310 | ) | |
| 299 | 311 | ||
| 300 | 312 | ||
| 301 | - def validate_config_main(argv=None): | ||
| 313 | + def validate_config_main(argv: Optional[Sequence[str]] = None) -> int: | ||
| 302 | 314 | parser = _make_argparser('Config filenames.') | |
| 303 | 315 | args = parser.parse_args(argv) | |
| 304 | 316 | ret = 0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ class InvalidColorSetting(ValueError): | |||
| 21 | 21 | pass | |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | - def format_color(text, color, use_color_setting): | ||
| 24 | + def format_color(text: str, color: str, use_color_setting: bool) -> str: | ||
| 25 | 25 | """Format text with color. | |
| 26 | 26 | ||
| 27 | 27 | Args: | |
@@ -38,7 +38,7 @@ def format_color(text, color, use_color_setting): | |||
| 38 | 38 | COLOR_CHOICES = ('auto', 'always', 'never') | |
| 39 | 39 | ||
| 40 | 40 | ||
| 41 | - def use_color(setting): | ||
| 41 | + def use_color(setting: str) -> bool: | ||
| 42 | 42 | """Choose whether to use color based on the command argument. | |
| 43 | 43 | ||
| 44 | 44 | Args: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,12 @@ | |||
| 1 | - import collections | ||
| 2 | 1 | import os.path | |
| 3 | 2 | import re | |
| 3 | + from typing import Any | ||
| 4 | + from typing import Dict | ||
| 4 | 5 | from typing import List | |
| 6 | + from typing import NamedTuple | ||
| 5 | 7 | from typing import Optional | |
| 8 | + from typing import Sequence | ||
| 9 | + from typing import Tuple | ||
| 6 | 10 | ||
| 7 | 11 | from aspy.yaml import ordered_dump | |
| 8 | 12 | from aspy.yaml import ordered_load | |
@@ -16,20 +20,23 @@ | |||
| 16 | 20 | from pre_commit.clientlib import LOCAL | |
| 17 | 21 | from pre_commit.clientlib import META | |
| 18 | 22 | from pre_commit.commands.migrate_config import migrate_config | |
| 23 | + from pre_commit.store import Store | ||
| 19 | 24 | from pre_commit.util import CalledProcessError | |
| 20 | 25 | from pre_commit.util import cmd_output | |
| 21 | 26 | from pre_commit.util import cmd_output_b | |
| 22 | 27 | from pre_commit.util import tmpdir | |
| 23 | 28 | ||
| 24 | 29 | ||
| 25 | - class RevInfo(collections.namedtuple('RevInfo', ('repo', 'rev', 'frozen'))): | ||
| 26 | - __slots__ = () | ||
| 30 | + class RevInfo(NamedTuple): | ||
| 31 | + repo: str | ||
| 32 | + rev: str | ||
| 33 | + frozen: Optional[str] | ||
| 27 | 34 | ||
| 28 | 35 | @classmethod | |
| 29 | - def from_config(cls, config): | ||
| 36 | + def from_config(cls, config: Dict[str, Any]) -> 'RevInfo': | ||
| 30 | 37 | return cls(config['repo'], config['rev'], None) | |
| 31 | 38 | ||
| 32 | - def update(self, tags_only, freeze): | ||
| 39 | + def update(self, tags_only: bool, freeze: bool) -> 'RevInfo': | ||
| 33 | 40 | if tags_only: | |
| 34 | 41 | tag_cmd = ('git', 'describe', 'FETCH_HEAD', '--tags', '--abbrev=0') | |
| 35 | 42 | else: | |
@@ -57,7 +64,11 @@ class RepositoryCannotBeUpdatedError(RuntimeError): | |||
| 57 | 64 | pass | |
| 58 | 65 | ||
| 59 | 66 | ||
| 60 | - def _check_hooks_still_exist_at_rev(repo_config, info, store): | ||
| 67 | + def _check_hooks_still_exist_at_rev( | ||
| 68 | + repo_config: Dict[str, Any], | ||
| 69 | + info: RevInfo, | ||
| 70 | + store: Store, | ||
| 71 | + ) -> None: | ||
| 61 | 72 | try: | |
| 62 | 73 | path = store.clone(repo_config['repo'], info.rev) | |
| 63 | 74 | manifest = load_manifest(os.path.join(path, C.MANIFEST_FILE)) | |
@@ -78,7 +89,11 @@ def _check_hooks_still_exist_at_rev(repo_config, info, store): | |||
| 78 | 89 | REV_LINE_FMT = '{}rev:{}{}{}{}' | |
| 79 | 90 | ||
| 80 | 91 | ||
| 81 | - def _original_lines(path, rev_infos, retry=False): | ||
| 92 | + def _original_lines( | ||
| 93 | + path: str, | ||
| 94 | + rev_infos: List[Optional[RevInfo]], | ||
| 95 | + retry: bool = False, | ||
| 96 | + ) -> Tuple[List[str], List[int]]: | ||
| 82 | 97 | """detect `rev:` lines or reformat the file""" | |
| 83 | 98 | with open(path) as f: | |
| 84 | 99 | original = f.read() | |
@@ -95,7 +110,7 @@ def _original_lines(path, rev_infos, retry=False): | |||
| 95 | 110 | return _original_lines(path, rev_infos, retry=True) | |
| 96 | 111 | ||
| 97 | 112 | ||
| 98 | - def _write_new_config(path, rev_infos): | ||
| 113 | + def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None: | ||
| 99 | 114 | lines, idxs = _original_lines(path, rev_infos) | |
| 100 | 115 | ||
| 101 | 116 | for idx, rev_info in zip(idxs, rev_infos): | |
@@ -119,7 +134,13 @@ def _write_new_config(path, rev_infos): | |||
| 119 | 134 | f.write(''.join(lines)) | |
| 120 | 135 | ||
| 121 | 136 | ||
| 122 | - def autoupdate(config_file, store, tags_only, freeze, repos=()): | ||
| 137 | + def autoupdate( | ||
| 138 | + config_file: str, | ||
| 139 | + store: Store, | ||
| 140 | + tags_only: bool, | ||
| 141 | + freeze: bool, | ||
| 142 | + repos: Sequence[str] = (), | ||
| 143 | + ) -> int: | ||
| 123 | 144 | """Auto-update the pre-commit config to the latest versions of repos.""" | |
| 124 | 145 | migrate_config(config_file, quiet=True) | |
| 125 | 146 | retv = 0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,10 +1,11 @@ | |||
| 1 | 1 | import os.path | |
| 2 | 2 | ||
| 3 | 3 | from pre_commit import output | |
| 4 | + from pre_commit.store import Store | ||
| 4 | 5 | from pre_commit.util import rmtree | |
| 5 | 6 | ||
| 6 | 7 | ||
| 7 | - def clean(store): | ||
| 8 | + def clean(store: Store) -> int: | ||
| 8 | 9 | legacy_path = os.path.expanduser('~/.pre-commit') | |
| 9 | 10 | for directory in (store.directory, legacy_path): | |
| 10 | 11 | if os.path.exists(directory): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,8 @@ | |||
| 1 | 1 | import os.path | |
| 2 | + from typing import Any | ||
| 3 | + from typing import Dict | ||
| 4 | + from typing import Set | ||
| 5 | + from typing import Tuple | ||
| 2 | 6 | ||
| 3 | 7 | import pre_commit.constants as C | |
| 4 | 8 | from pre_commit import output | |
@@ -8,9 +12,15 @@ | |||
| 8 | 12 | from pre_commit.clientlib import load_manifest | |
| 9 | 13 | from pre_commit.clientlib import LOCAL | |
| 10 | 14 | from pre_commit.clientlib import META | |
| 15 | + from pre_commit.store import Store | ||
| 11 | 16 | ||
| 12 | 17 | ||
| 13 | - def _mark_used_repos(store, all_repos, unused_repos, repo): | ||
| 18 | + def _mark_used_repos( | ||
| 19 | + store: Store, | ||
| 20 | + all_repos: Dict[Tuple[str, str], str], | ||
| 21 | + unused_repos: Set[Tuple[str, str]], | ||
| 22 | + repo: Dict[str, Any], | ||
| 23 | + ) -> None: | ||
| 14 | 24 | if repo['repo'] == META: | |
| 15 | 25 | return | |
| 16 | 26 | elif repo['repo'] == LOCAL: | |
@@ -47,7 +57,7 @@ def _mark_used_repos(store, all_repos, unused_repos, repo): | |||
| 47 | 57 | )) | |
| 48 | 58 | ||
| 49 | 59 | ||
| 50 | - def _gc_repos(store): | ||
| 60 | + def _gc_repos(store: Store) -> int: | ||
| 51 | 61 | configs = store.select_all_configs() | |
| 52 | 62 | repos = store.select_all_repos() | |
| 53 | 63 | ||
@@ -73,7 +83,7 @@ def _gc_repos(store): | |||
| 73 | 83 | return len(unused_repos) | |
| 74 | 84 | ||
| 75 | 85 | ||
| 76 | - def gc(store): | ||
| 86 | + def gc(store: Store) -> int: | ||
| 77 | 87 | with store.exclusive_lock(): | |
| 78 | 88 | repos_removed = _gc_repos(store) | |
| 79 | 89 | output.write_line(f'{repos_removed} repo(s) removed.') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,21 @@ | |||
| 1 | 1 | import logging | |
| 2 | 2 | import os.path | |
| 3 | + from typing import Sequence | ||
| 3 | 4 | ||
| 4 | 5 | from pre_commit.commands.install_uninstall import install | |
| 6 | + from pre_commit.store import Store | ||
| 5 | 7 | from pre_commit.util import CalledProcessError | |
| 6 | 8 | from pre_commit.util import cmd_output | |
| 7 | 9 | ||
| 8 | 10 | logger = logging.getLogger('pre_commit') | |
| 9 | 11 | ||
| 10 | 12 | ||
| 11 | - def init_templatedir(config_file, store, directory, hook_types): | ||
| 13 | + def init_templatedir( | ||
| 14 | + config_file: str, | ||
| 15 | + store: Store, | ||
| 16 | + directory: str, | ||
| 17 | + hook_types: Sequence[str], | ||
| 18 | + ) -> int: | ||
| 12 | 19 | install( | |
| 13 | 20 | config_file, store, hook_types=hook_types, | |
| 14 | 21 | overwrite=True, skip_on_missing_config=True, git_dir=directory, | |
@@ -25,3 +32,4 @@ def init_templatedir(config_file, store, directory, hook_types): | |||
| 25 | 32 | logger.warning( | |
| 26 | 33 | f'maybe `git config --global init.templateDir {dest}`?', | |
| 27 | 34 | ) | |
| 35 | + return 0 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments