Skip to content

Commit fa536a8

Browse files
committed
mypy passes with check_untyped_defs
1 parent ab19b94 commit fa536a8

25 files changed

+161
-89
lines changed

.gitignore

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
*.egg-info
2-
*.iml
32
*.py[co]
4-
.*.sw[a-z]
5-
.coverage
6-
.idea
7-
.project
8-
.pydevproject
9-
.tox
10-
.venv.touch
3+
/.coverage
4+
/.mypy_cache
5+
/.pytest_cache
6+
/.tox
7+
/dist
118
/venv*
12-
coverage-html
13-
dist
14-
.pytest_cache

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ repos:
4242
rev: v1.6.0
4343
hooks:
4444
- id: setup-cfg-fmt
45+
- repo: https://github.com/pre-commit/mirrors-mypy
46+
rev: v0.761
47+
hooks:
48+
- id: mypy
49+
exclude: ^testing/resources/
4550
- repo: meta
4651
hooks:
4752
- id: check-hooks-apply

pre_commit/color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
terminal_supports_color = True
5-
if os.name == 'nt': # pragma: no cover (windows)
5+
if sys.platform == 'win32': # pragma: no cover (windows)
66
from pre_commit.color_windows import enable_virtual_terminal_processing
77
try:
88
enable_virtual_terminal_processing()

pre_commit/color_windows.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from ctypes import POINTER
2-
from ctypes import windll
3-
from ctypes import WinError
4-
from ctypes import WINFUNCTYPE
5-
from ctypes.wintypes import BOOL
6-
from ctypes.wintypes import DWORD
7-
from ctypes.wintypes import HANDLE
1+
import sys
2+
assert sys.platform == 'win32'
3+
4+
from ctypes import POINTER # noqa: E402
5+
from ctypes import windll # noqa: E402
6+
from ctypes import WinError # noqa: E402
7+
from ctypes import WINFUNCTYPE # noqa: E402
8+
from ctypes.wintypes import BOOL # noqa: E402
9+
from ctypes.wintypes import DWORD # noqa: E402
10+
from ctypes.wintypes import HANDLE # noqa: E402
11+
812

913
STD_OUTPUT_HANDLE = -11
1014
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4

pre_commit/commands/autoupdate.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import collections
22
import os.path
33
import re
4+
from typing import List
5+
from typing import Optional
46

57
from aspy.yaml import ordered_dump
68
from aspy.yaml import ordered_load
@@ -121,7 +123,7 @@ def autoupdate(config_file, store, tags_only, freeze, repos=()):
121123
"""Auto-update the pre-commit config to the latest versions of repos."""
122124
migrate_config(config_file, quiet=True)
123125
retv = 0
124-
rev_infos = []
126+
rev_infos: List[Optional[RevInfo]] = []
125127
changed = False
126128

127129
config = load_config(config_file)

pre_commit/envcontext.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
import collections
21
import contextlib
2+
import enum
33
import os
4+
from typing import NamedTuple
5+
from typing import Tuple
6+
from typing import Union
47

58

6-
UNSET = collections.namedtuple('UNSET', ())()
9+
class _Unset(enum.Enum):
10+
UNSET = 1
711

812

9-
Var = collections.namedtuple('Var', ('name', 'default'))
10-
Var.__new__.__defaults__ = ('',)
13+
UNSET = _Unset.UNSET
14+
15+
16+
class Var(NamedTuple):
17+
name: str
18+
default: str = ''
19+
20+
21+
SubstitutionT = Tuple[Union[str, Var], ...]
22+
ValueT = Union[str, _Unset, SubstitutionT]
23+
PatchesT = Tuple[Tuple[str, ValueT], ...]
1124

1225

1326
def format_env(parts, env):

pre_commit/error_handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os.path
33
import sys
44
import traceback
5+
from typing import Union
56

67
import pre_commit.constants as C
78
from pre_commit import five
@@ -32,8 +33,8 @@ def _log_and_exit(msg, exc, formatted):
3233
output.write_line(f'Check the log at {log_path}')
3334

3435
with open(log_path, 'wb') as log:
35-
def _log_line(*s): # type: (*str) -> None
36-
output.write_line(*s, stream=log)
36+
def _log_line(s: Union[None, str, bytes] = None) -> None:
37+
output.write_line(s, stream=log)
3738

3839
_log_line('### version information')
3940
_log_line()

pre_commit/file_lock.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import contextlib
22
import errno
3+
import os
34

45

5-
try: # pragma: no cover (windows)
6+
if os.name == 'nt': # pragma: no cover (windows)
67
import msvcrt
78

89
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/locking
@@ -14,12 +15,14 @@
1415
@contextlib.contextmanager
1516
def _locked(fileno, blocked_cb):
1617
try:
17-
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region)
18+
# TODO: https://github.com/python/typeshed/pull/3607
19+
msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region) # type: ignore
1820
except OSError:
1921
blocked_cb()
2022
while True:
2123
try:
22-
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)
24+
# TODO: https://github.com/python/typeshed/pull/3607
25+
msvcrt.locking(fileno, msvcrt.LK_LOCK, _region) # type: ignore # noqa: E501
2326
except OSError as e:
2427
# Locking violation. Returned when the _LK_LOCK or _LK_RLCK
2528
# flag is specified and the file cannot be locked after 10
@@ -37,8 +40,9 @@ def _locked(fileno, blocked_cb):
3740
# The documentation however states:
3841
# "Regions should be locked only briefly and should be unlocked
3942
# before closing a file or exiting the program."
40-
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region)
41-
except ImportError: # pragma: windows no cover
43+
# TODO: https://github.com/python/typeshed/pull/3607
44+
msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region) # type: ignore
45+
else: # pramga: windows no cover
4246
import fcntl
4347

4448
@contextlib.contextmanager

pre_commit/languages/all.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Any
2+
from typing import Dict
3+
14
from pre_commit.languages import conda
25
from pre_commit.languages import docker
36
from pre_commit.languages import docker_image
@@ -13,6 +16,7 @@
1316
from pre_commit.languages import swift
1417
from pre_commit.languages import system
1518

19+
1620
# A language implements the following constant and functions in its module:
1721
#
1822
# # Use None for no environment
@@ -49,7 +53,7 @@
4953
# (returncode, output)
5054
# """
5155

52-
languages = {
56+
languages: Dict[str, Any] = {
5357
'conda': conda,
5458
'docker': docker,
5559
'docker_image': docker_image,

pre_commit/languages/conda.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
from pre_commit.envcontext import envcontext
5+
from pre_commit.envcontext import SubstitutionT
56
from pre_commit.envcontext import UNSET
67
from pre_commit.envcontext import Var
78
from pre_commit.languages import helpers
@@ -18,7 +19,7 @@ def get_env_patch(env):
1819
# they can be in $CONDA_PREFIX/bin, $CONDA_PREFIX/Library/bin,
1920
# $CONDA_PREFIX/Scripts and $CONDA_PREFIX. Whereas the latter only
2021
# seems to be used for python.exe.
21-
path = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))
22+
path: SubstitutionT = (os.path.join(env, 'bin'), os.pathsep, Var('PATH'))
2223
if os.name == 'nt': # pragma: no cover (platform specific)
2324
path = (env, os.pathsep) + path
2425
path = (os.path.join(env, 'Scripts'), os.pathsep) + path

0 commit comments

Comments
 (0)