mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Add types to pre-commit
This commit is contained in:
parent
fa536a8693
commit
327ed924a3
62 changed files with 911 additions and 411 deletions
|
|
@ -4,6 +4,8 @@ import distutils.spawn
|
|||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Callable
|
||||
from typing import Dict
|
||||
from typing import Tuple
|
||||
|
||||
# work around https://github.com/Homebrew/homebrew-core/issues/30445
|
||||
|
|
@ -28,7 +30,7 @@ class FatalError(RuntimeError):
|
|||
pass
|
||||
|
||||
|
||||
def _norm_exe(exe):
|
||||
def _norm_exe(exe: str) -> Tuple[str, ...]:
|
||||
"""Necessary for shebang support on windows.
|
||||
|
||||
roughly lifted from `identify.identify.parse_shebang`
|
||||
|
|
@ -47,7 +49,7 @@ def _norm_exe(exe):
|
|||
return tuple(cmd)
|
||||
|
||||
|
||||
def _run_legacy():
|
||||
def _run_legacy() -> Tuple[int, bytes]:
|
||||
if __file__.endswith('.legacy'):
|
||||
raise SystemExit(
|
||||
"bug: pre-commit's script is installed in migration mode\n"
|
||||
|
|
@ -59,9 +61,9 @@ def _run_legacy():
|
|||
)
|
||||
|
||||
if HOOK_TYPE == 'pre-push':
|
||||
stdin = getattr(sys.stdin, 'buffer', sys.stdin).read()
|
||||
stdin = sys.stdin.buffer.read()
|
||||
else:
|
||||
stdin = None
|
||||
stdin = b''
|
||||
|
||||
legacy_hook = os.path.join(HERE, f'{HOOK_TYPE}.legacy')
|
||||
if os.access(legacy_hook, os.X_OK):
|
||||
|
|
@ -73,7 +75,7 @@ def _run_legacy():
|
|||
return 0, stdin
|
||||
|
||||
|
||||
def _validate_config():
|
||||
def _validate_config() -> None:
|
||||
cmd = ('git', 'rev-parse', '--show-toplevel')
|
||||
top_level = subprocess.check_output(cmd).decode('UTF-8').strip()
|
||||
cfg = os.path.join(top_level, CONFIG)
|
||||
|
|
@ -97,7 +99,7 @@ def _validate_config():
|
|||
)
|
||||
|
||||
|
||||
def _exe():
|
||||
def _exe() -> Tuple[str, ...]:
|
||||
with open(os.devnull, 'wb') as devnull:
|
||||
for exe in (INSTALL_PYTHON, sys.executable):
|
||||
try:
|
||||
|
|
@ -117,11 +119,11 @@ def _exe():
|
|||
)
|
||||
|
||||
|
||||
def _rev_exists(rev):
|
||||
def _rev_exists(rev: str) -> bool:
|
||||
return not subprocess.call(('git', 'rev-list', '--quiet', rev))
|
||||
|
||||
|
||||
def _pre_push(stdin):
|
||||
def _pre_push(stdin: bytes) -> Tuple[str, ...]:
|
||||
remote = sys.argv[1]
|
||||
|
||||
opts: Tuple[str, ...] = ()
|
||||
|
|
@ -158,8 +160,8 @@ def _pre_push(stdin):
|
|||
raise EarlyExit()
|
||||
|
||||
|
||||
def _opts(stdin):
|
||||
fns = {
|
||||
def _opts(stdin: bytes) -> Tuple[str, ...]:
|
||||
fns: Dict[str, Callable[[bytes], Tuple[str, ...]]] = {
|
||||
'prepare-commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]),
|
||||
'commit-msg': lambda _: ('--commit-msg-filename', sys.argv[1]),
|
||||
'pre-merge-commit': lambda _: (),
|
||||
|
|
@ -171,13 +173,14 @@ def _opts(stdin):
|
|||
|
||||
|
||||
if sys.version_info < (3, 7): # https://bugs.python.org/issue25942
|
||||
def _subprocess_call(cmd): # this is the python 2.7 implementation
|
||||
# this is the python 2.7 implementation
|
||||
def _subprocess_call(cmd: Tuple[str, ...]) -> int:
|
||||
return subprocess.Popen(cmd).wait()
|
||||
else:
|
||||
_subprocess_call = subprocess.call
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> int:
|
||||
retv, stdin = _run_legacy()
|
||||
try:
|
||||
_validate_config()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue