mypy passes with check_untyped_defs

This commit is contained in:
Anthony Sottile 2020-01-10 19:12:56 -08:00
parent ab19b94811
commit fa536a8693
25 changed files with 161 additions and 89 deletions

View file

@ -2,7 +2,7 @@ import os
import sys
terminal_supports_color = True
if os.name == 'nt': # pragma: no cover (windows)
if sys.platform == 'win32': # pragma: no cover (windows)
from pre_commit.color_windows import enable_virtual_terminal_processing
try:
enable_virtual_terminal_processing()

View file

@ -1,10 +1,14 @@
from ctypes import POINTER
from ctypes import windll
from ctypes import WinError
from ctypes import WINFUNCTYPE
from ctypes.wintypes import BOOL
from ctypes.wintypes import DWORD
from ctypes.wintypes import HANDLE
import sys
assert sys.platform == 'win32'
from ctypes import POINTER # noqa: E402
from ctypes import windll # noqa: E402
from ctypes import WinError # noqa: E402
from ctypes import WINFUNCTYPE # noqa: E402
from ctypes.wintypes import BOOL # noqa: E402
from ctypes.wintypes import DWORD # noqa: E402
from ctypes.wintypes import HANDLE # noqa: E402
STD_OUTPUT_HANDLE = -11
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4

View file

@ -1,6 +1,8 @@
import collections
import os.path
import re
from typing import List
from typing import Optional
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
@ -121,7 +123,7 @@ def autoupdate(config_file, store, tags_only, freeze, repos=()):
"""Auto-update the pre-commit config to the latest versions of repos."""
migrate_config(config_file, quiet=True)
retv = 0
rev_infos = []
rev_infos: List[Optional[RevInfo]] = []
changed = False
config = load_config(config_file)

View file

@ -1,13 +1,26 @@
import collections
import contextlib
import enum
import os
from typing import NamedTuple
from typing import Tuple
from typing import Union
UNSET = collections.namedtuple('UNSET', ())()
class _Unset(enum.Enum):
UNSET = 1
Var = collections.namedtuple('Var', ('name', 'default'))
Var.__new__.__defaults__ = ('',)
UNSET = _Unset.UNSET
class Var(NamedTuple):
name: str
default: str = ''
SubstitutionT = Tuple[Union[str, Var], ...]
ValueT = Union[str, _Unset, SubstitutionT]
PatchesT = Tuple[Tuple[str, ValueT], ...]
def format_env(parts, env):

View file

@ -2,6 +2,7 @@ import contextlib
import os.path
import sys
import traceback
from typing import Union
import pre_commit.constants as C
from pre_commit import five
@ -32,8 +33,8 @@ def _log_and_exit(msg, exc, formatted):
output.write_line(f'Check the log at {log_path}')
with open(log_path, 'wb') as log:
def _log_line(*s): # type: (*str) -> None
output.write_line(*s, stream=log)
def _log_line(s: Union[None, str, bytes] = None) -> None:
output.write_line(s, stream=log)
_log_line('### version information')
_log_line()

View file

@ -1,8 +1,9 @@
import contextlib
import errno
import os
try: # pragma: no cover (windows)
if os.name == 'nt': # pragma: no cover (windows)
import msvcrt
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/locking
@ -14,12 +15,14 @@ try: # pragma: no cover (windows)
@contextlib.contextmanager
def _locked(fileno, blocked_cb):
try:
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region)
# TODO: https://github.com/python/typeshed/pull/3607
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region) # type: ignore
except OSError:
blocked_cb()
while True:
try:
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)
# TODO: https://github.com/python/typeshed/pull/3607
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region) # type: ignore # noqa: E501
except OSError as e:
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
# flag is specified and the file cannot be locked after 10
@ -37,8 +40,9 @@ try: # pragma: no cover (windows)
# The documentation however states:
# "Regions should be locked only briefly and should be unlocked
# before closing a file or exiting the program."
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region)
except ImportError: # pragma: windows no cover
# TODO: https://github.com/python/typeshed/pull/3607
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region) # type: ignore
else: # pramga: windows no cover
import fcntl
@contextlib.contextmanager

View file

@ -1,3 +1,6 @@
from typing import Any
from typing import Dict
from pre_commit.languages import conda
from pre_commit.languages import docker
from pre_commit.languages import docker_image
@ -13,6 +16,7 @@ from pre_commit.languages import script
from pre_commit.languages import swift
from pre_commit.languages import system
# A language implements the following constant and functions in its module:
#
# # Use None for no environment
@ -49,7 +53,7 @@ from pre_commit.languages import system
# (returncode, output)
# """
languages = {
languages: Dict[str, Any] = {
'conda': conda,
'docker': docker,
'docker_image': docker_image,

View file

@ -2,6 +2,7 @@ import contextlib
import os
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import SubstitutionT
from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
@ -18,7 +19,7 @@ def get_env_patch(env):
# they can be in $CONDA_PREFIX/bin, $CONDA_PREFIX/Library/bin,
# $CONDA_PREFIX/Scripts and $CONDA_PREFIX. Whereas the latter only
# seems to be used for python.exe.
path = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))
path: SubstitutionT = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))
if os.name == 'nt': # pragma: no cover (platform specific)
path = (env, os.pathsep) + path
path = (os.path.join(env, 'Scripts'), os.pathsep) + path

View file

@ -1,5 +1,6 @@
import hashlib
import os
from typing import Tuple
import pre_commit.constants as C
from pre_commit import five
@ -42,7 +43,7 @@ def assert_docker_available(): # pragma: windows no cover
def build_docker_image(prefix, **kwargs): # pragma: windows no cover
pull = kwargs.pop('pull')
assert not kwargs, kwargs
cmd = (
cmd: Tuple[str, ...] = (
'docker', 'build',
'--tag', docker_tag(prefix),
'--label', PRE_COMMIT_LABEL,

View file

@ -1,4 +1,5 @@
import contextlib
import functools
import os
import sys
@ -64,7 +65,8 @@ def _find_by_sys_executable():
return None
def _get_default_version(): # pragma: no cover (platform dependent)
@functools.lru_cache(maxsize=1)
def get_default_version(): # pragma: no cover (platform dependent)
# First attempt from `sys.executable` (or the realpath)
exe = _find_by_sys_executable()
if exe:
@ -86,15 +88,6 @@ def _get_default_version(): # pragma: no cover (platform dependent)
return C.DEFAULT
def get_default_version():
# TODO: when dropping python2, use `functools.lru_cache(maxsize=1)`
try:
return get_default_version.cached_version
except AttributeError:
get_default_version.cached_version = _get_default_version()
return get_default_version()
def _sys_executable_matches(version):
if version == 'python':
return True

View file

@ -5,6 +5,7 @@ import tarfile
import pre_commit.constants as C
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import Var
from pre_commit.languages import helpers
from pre_commit.util import CalledProcessError
@ -18,7 +19,7 @@ healthy = helpers.basic_healthy
def get_env_patch(venv, language_version): # pragma: windows no cover
patches = (
patches: PatchesT = (
('GEM_HOME', os.path.join(venv, 'gems')),
('RBENV_ROOT', venv),
('BUNDLE_IGNORE_CONFIG', '1'),

View file

@ -1,5 +1,7 @@
import contextlib
import os.path
from typing import Set
from typing import Tuple
import toml
@ -71,7 +73,7 @@ def install_environment(prefix, version, additional_dependencies):
_add_dependencies(prefix.path('Cargo.toml'), lib_deps)
with clean_path_on_failure(directory):
packages_to_install = {('--path', '.')}
packages_to_install: Set[Tuple[str, ...]] = {('--path', '.')}
for cli_dep in cli_deps:
cli_dep = cli_dep[len('cli:'):]
package, _, version = cli_dep.partition(':')

View file

@ -1,8 +1,8 @@
import contextlib
import sys
from pre_commit import color
from pre_commit import five
from pre_commit.util import noop_context
def get_hook_message(
@ -71,14 +71,12 @@ def write(s, stream=stdout_byte_stream):
def write_line(s=None, stream=stdout_byte_stream, logfile_name=None):
output_streams = [stream]
if logfile_name:
ctx = open(logfile_name, 'ab')
output_streams.append(ctx)
else:
ctx = noop_context()
with contextlib.ExitStack() as exit_stack:
output_streams = [stream]
if logfile_name:
stream = exit_stack.enter_context(open(logfile_name, 'ab'))
output_streams.append(stream)
with ctx:
for output_stream in output_streams:
if s is not None:
output_stream.write(five.to_bytes(s))

View file

@ -1,8 +1,10 @@
import collections
import json
import logging
import os
import shlex
from typing import NamedTuple
from typing import Sequence
from typing import Set
import pre_commit.constants as C
from pre_commit import five
@ -49,8 +51,29 @@ def _write_state(prefix, venv, state):
_KEYS = tuple(item.key for item in MANIFEST_HOOK_DICT.items)
class Hook(collections.namedtuple('Hook', ('src', 'prefix') + _KEYS)):
__slots__ = ()
class Hook(NamedTuple):
src: str
prefix: Prefix
id: str
name: str
entry: str
language: str
alias: str
files: str
exclude: str
types: Sequence[str]
exclude_types: Sequence[str]
additional_dependencies: Sequence[str]
args: Sequence[str]
always_run: bool
pass_filenames: bool
description: str
language_version: str
log_file: str
minimum_pre_commit_version: str
require_serial: bool
stages: Sequence[str]
verbose: bool
@property
def cmd(self):
@ -201,7 +224,7 @@ def _repository_hooks(repo_config, store, root_config):
def install_hook_envs(hooks, store):
def _need_installed():
seen = set()
seen: Set[Hook] = set()
ret = []
for hook in hooks:
if hook.install_key not in seen and not hook.installed():

View file

@ -4,6 +4,7 @@ import distutils.spawn
import os
import subprocess
import sys
from typing import Tuple
# work around https://github.com/Homebrew/homebrew-core/issues/30445
os.environ.pop('__PYVENV_LAUNCHER__', None)
@ -12,10 +13,10 @@ HERE = os.path.dirname(os.path.abspath(__file__))
Z40 = '0' * 40
ID_HASH = '138fd403232d2ddd5efb44317e38bf03'
# start templated
CONFIG = None
HOOK_TYPE = None
INSTALL_PYTHON = None
SKIP_ON_MISSING_CONFIG = None
CONFIG = ''
HOOK_TYPE = ''
INSTALL_PYTHON = ''
SKIP_ON_MISSING_CONFIG = False
# end templated
@ -123,7 +124,7 @@ def _rev_exists(rev):
def _pre_push(stdin):
remote = sys.argv[1]
opts = ()
opts: Tuple[str, ...] = ()
for line in stdin.decode('UTF-8').splitlines():
_, local_sha, _, remote_sha = line.split()
if local_sha == Z40:
@ -146,8 +147,8 @@ def _pre_push(stdin):
# pushing the whole tree including root commit
opts = ('--all-files',)
else:
cmd = ('git', 'rev-parse', f'{first_ancestor}^')
source = subprocess.check_output(cmd).decode().strip()
rev_cmd = ('git', 'rev-parse', f'{first_ancestor}^')
source = subprocess.check_output(rev_cmd).decode().strip()
opts = ('--origin', local_sha, '--source', source)
if opts:

View file

@ -152,6 +152,7 @@ if os.name != 'nt': # pragma: windows no cover
# tty flags normally change \n to \r\n
attrs = termios.tcgetattr(self.r)
assert isinstance(attrs[1], int)
attrs[1] &= ~(termios.ONLCR | termios.OPOST)
termios.tcsetattr(self.r, termios.TCSANOW, attrs)

View file

@ -4,6 +4,7 @@ import math
import os
import subprocess
import sys
from typing import List
from pre_commit import parse_shebang
from pre_commit.util import cmd_output_b
@ -56,7 +57,7 @@ def partition(cmd, varargs, target_concurrency, _max_length=None):
cmd = tuple(cmd)
ret = []
ret_cmd = []
ret_cmd: List[str] = []
# Reversed so arguments are in order
varargs = list(reversed(varargs))