Skip to content

Commit c33b910

Browse files
committed
Apply typing to all of pre-commit-hooks
1 parent 63cc341 commit c33b910

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+397
-262
lines changed

.gitignore

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
*.egg-info
2-
*.iml
32
*.py[co]
43
.*.sw[a-z]
5-
.pytest_cache
64
.coverage
7-
.idea
8-
.project
9-
.pydevproject
105
.tox
116
.venv.touch
7+
/.mypy_cache
8+
/.pytest_cache
129
/venv*
1310
coverage-html
1411
dist
15-
# SublimeText project/workspace files
16-
*.sublime-*

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ repos:
2727
rev: v1.3.5
2828
hooks:
2929
- id: reorder-python-imports
30-
language_version: python2.7
30+
language_version: python3
3131
- repo: https://github.com/asottile/pyupgrade
3232
rev: v1.11.1
3333
hooks:
@@ -36,3 +36,8 @@ repos:
3636
rev: v0.7.1
3737
hooks:
3838
- id: add-trailing-comma
39+
- repo: https://github.com/pre-commit/mirrors-mypy
40+
rev: v0.660
41+
hooks:
42+
- id: mypy
43+
language_version: python3

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
dist: xenial
12
language: python
23
matrix:
34
include: # These should match the tox env list
@@ -6,7 +7,6 @@ matrix:
67
python: 3.6
78
- env: TOXENV=py37
89
python: 3.7
9-
dist: xenial
1010
- env: TOXENV=pypy
1111
python: pypy-5.7.1
1212
install: pip install coveralls tox

get-git-lfs.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os.path
55
import shutil
66
import tarfile
7-
from urllib.request import urlopen
7+
import urllib.request
88

99
DOWNLOAD_PATH = (
1010
'https://github.com/github/git-lfs/releases/download/'
@@ -15,7 +15,7 @@
1515
DEST_DIR = os.path.dirname(DEST_PATH)
1616

1717

18-
def main():
18+
def main(): # type: () -> int
1919
if (
2020
os.path.exists(DEST_PATH) and
2121
os.path.isfile(DEST_PATH) and
@@ -27,12 +27,13 @@ def main():
2727
shutil.rmtree(DEST_DIR, ignore_errors=True)
2828
os.makedirs(DEST_DIR, exist_ok=True)
2929

30-
contents = io.BytesIO(urlopen(DOWNLOAD_PATH).read())
30+
contents = io.BytesIO(urllib.request.urlopen(DOWNLOAD_PATH).read())
3131
with tarfile.open(fileobj=contents) as tar:
32-
with tar.extractfile(PATH_IN_TAR) as src_file:
32+
with tar.extractfile(PATH_IN_TAR) as src_file: # type: ignore
3333
with open(DEST_PATH, 'wb') as dest_file:
3434
shutil.copyfileobj(src_file, dest_file)
3535
os.chmod(DEST_PATH, 0o755)
36+
return 0
3637

3738

3839
if __name__ == '__main__':

mypy.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[mypy]
2+
check_untyped_defs = true
3+
disallow_any_generics = true
4+
disallow_incomplete_defs = true
5+
disallow_untyped_defs = true
6+
no_implicit_optional = true
7+
8+
[mypy-testing.*]
9+
disallow_untyped_defs = false
10+
11+
[mypy-tests.*]
12+
disallow_untyped_defs = false

pre_commit_hooks/autopep8_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import unicode_literals
44

55

6-
def main(argv=None):
6+
def main(): # type: () -> int
77
raise SystemExit(
88
'autopep8-wrapper is deprecated. Instead use autopep8 directly via '
99
'https://github.com/pre-commit/mirrors-autopep8',

pre_commit_hooks/check_added_large_files.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import json
88
import math
99
import os
10+
from typing import Iterable
11+
from typing import Optional
12+
from typing import Sequence
13+
from typing import Set
1014

1115
from pre_commit_hooks.util import added_files
1216
from pre_commit_hooks.util import CalledProcessError
1317
from pre_commit_hooks.util import cmd_output
1418

1519

16-
def lfs_files():
20+
def lfs_files(): # type: () -> Set[str]
1721
try:
1822
# Introduced in git-lfs 2.2.0, first working in 2.2.1
1923
lfs_ret = cmd_output('git', 'lfs', 'status', '--json')
@@ -24,6 +28,7 @@ def lfs_files():
2428

2529

2630
def find_large_added_files(filenames, maxkb):
31+
# type: (Iterable[str], int) -> int
2732
# Find all added files that are also in the list of files pre-commit tells
2833
# us about
2934
filenames = (added_files() & set(filenames)) - lfs_files()
@@ -38,7 +43,7 @@ def find_large_added_files(filenames, maxkb):
3843
return retv
3944

4045

41-
def main(argv=None):
46+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
4247
parser = argparse.ArgumentParser()
4348
parser.add_argument(
4449
'filenames', nargs='*',

pre_commit_hooks/check_ast.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import platform
88
import sys
99
import traceback
10+
from typing import Optional
11+
from typing import Sequence
1012

1113

12-
def check_ast(argv=None):
14+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
1315
parser = argparse.ArgumentParser()
1416
parser.add_argument('filenames', nargs='*')
1517
args = parser.parse_args(argv)
@@ -34,4 +36,4 @@ def check_ast(argv=None):
3436

3537

3638
if __name__ == '__main__':
37-
exit(check_ast())
39+
exit(main())

pre_commit_hooks/check_builtin_literals.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import ast
55
import collections
66
import sys
7+
from typing import List
8+
from typing import Optional
9+
from typing import Sequence
10+
from typing import Set
711

812

913
BUILTIN_TYPES = {
@@ -22,14 +26,17 @@
2226

2327
class BuiltinTypeVisitor(ast.NodeVisitor):
2428
def __init__(self, ignore=None, allow_dict_kwargs=True):
25-
self.builtin_type_calls = []
29+
# type: (Optional[Sequence[str]], bool) -> None
30+
self.builtin_type_calls = [] # type: List[BuiltinTypeCall]
2631
self.ignore = set(ignore) if ignore else set()
2732
self.allow_dict_kwargs = allow_dict_kwargs
2833

29-
def _check_dict_call(self, node):
34+
def _check_dict_call(self, node): # type: (ast.Call) -> bool
35+
3036
return self.allow_dict_kwargs and (getattr(node, 'kwargs', None) or getattr(node, 'keywords', None))
3137

32-
def visit_Call(self, node):
38+
def visit_Call(self, node): # type: (ast.Call) -> None
39+
3340
if not isinstance(node.func, ast.Name):
3441
# Ignore functions that are object attributes (`foo.bar()`).
3542
# Assume that if the user calls `builtins.list()`, they know what
@@ -47,31 +54,30 @@ def visit_Call(self, node):
4754

4855

4956
def check_file_for_builtin_type_constructors(filename, ignore=None, allow_dict_kwargs=True):
57+
# type: (str, Optional[Sequence[str]], bool) -> List[BuiltinTypeCall]
5058
with open(filename, 'rb') as f:
5159
tree = ast.parse(f.read(), filename=filename)
5260
visitor = BuiltinTypeVisitor(ignore=ignore, allow_dict_kwargs=allow_dict_kwargs)
5361
visitor.visit(tree)
5462
return visitor.builtin_type_calls
5563

5664

57-
def parse_args(argv):
58-
def parse_ignore(value):
59-
return set(value.split(','))
65+
def parse_ignore(value): # type: (str) -> Set[str]
66+
return set(value.split(','))
6067

68+
69+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
6170
parser = argparse.ArgumentParser()
6271
parser.add_argument('filenames', nargs='*')
6372
parser.add_argument('--ignore', type=parse_ignore, default=set())
6473

65-
allow_dict_kwargs = parser.add_mutually_exclusive_group(required=False)
66-
allow_dict_kwargs.add_argument('--allow-dict-kwargs', action='store_true')
67-
allow_dict_kwargs.add_argument('--no-allow-dict-kwargs', dest='allow_dict_kwargs', action='store_false')
68-
allow_dict_kwargs.set_defaults(allow_dict_kwargs=True)
69-
70-
return parser.parse_args(argv)
74+
mutex = parser.add_mutually_exclusive_group(required=False)
75+
mutex.add_argument('--allow-dict-kwargs', action='store_true')
76+
mutex.add_argument('--no-allow-dict-kwargs', dest='allow_dict_kwargs', action='store_false')
77+
mutex.set_defaults(allow_dict_kwargs=True)
7178

79+
args = parser.parse_args(argv)
7280

73-
def main(argv=None):
74-
args = parse_args(argv)
7581
rc = 0
7682
for filename in args.filenames:
7783
calls = check_file_for_builtin_type_constructors(

pre_commit_hooks/check_byte_order_marker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from __future__ import unicode_literals
44

55
import argparse
6+
from typing import Optional
7+
from typing import Sequence
68

79

8-
def main(argv=None):
10+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
911
parser = argparse.ArgumentParser()
1012
parser.add_argument('filenames', nargs='*', help='Filenames to check')
1113
args = parser.parse_args(argv)

pre_commit_hooks/check_case_conflict.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
from __future__ import unicode_literals
44

55
import argparse
6+
from typing import Iterable
7+
from typing import Optional
8+
from typing import Sequence
9+
from typing import Set
610

711
from pre_commit_hooks.util import added_files
812
from pre_commit_hooks.util import cmd_output
913

1014

11-
def lower_set(iterable):
15+
def lower_set(iterable): # type: (Iterable[str]) -> Set[str]
1216
return {x.lower() for x in iterable}
1317

1418

15-
def find_conflicting_filenames(filenames):
19+
def find_conflicting_filenames(filenames): # type: (Sequence[str]) -> int
1620
repo_files = set(cmd_output('git', 'ls-files').splitlines())
1721
relevant_files = set(filenames) | added_files()
1822
repo_files -= relevant_files
@@ -41,7 +45,7 @@ def find_conflicting_filenames(filenames):
4145
return retv
4246

4347

44-
def main(argv=None):
48+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
4549
parser = argparse.ArgumentParser()
4650
parser.add_argument(
4751
'filenames', nargs='*',

pre_commit_hooks/check_docstring_first.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import argparse
66
import io
77
import tokenize
8+
from typing import Optional
9+
from typing import Sequence
810

911

1012
NON_CODE_TOKENS = frozenset((
@@ -13,6 +15,7 @@
1315

1416

1517
def check_docstring_first(src, filename='<unknown>'):
18+
# type: (str, str) -> int
1619
"""Returns nonzero if the source has what looks like a docstring that is
1720
not at the beginning of the source.
1821
@@ -50,7 +53,7 @@ def check_docstring_first(src, filename='<unknown>'):
5053
return 0
5154

5255

53-
def main(argv=None):
56+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
5457
parser = argparse.ArgumentParser()
5558
parser.add_argument('filenames', nargs='*')
5659
args = parser.parse_args(argv)

pre_commit_hooks/check_executables_have_shebangs.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import argparse
77
import pipes
88
import sys
9+
from typing import Optional
10+
from typing import Sequence
911

1012

11-
def check_has_shebang(path):
13+
def check_has_shebang(path): # type: (str) -> int
1214
with open(path, 'rb') as f:
1315
first_bytes = f.read(2)
1416

@@ -27,7 +29,7 @@ def check_has_shebang(path):
2729
return 0
2830

2931

30-
def main(argv=None):
32+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
3133
parser = argparse.ArgumentParser(description=__doc__)
3234
parser.add_argument('filenames', nargs='*')
3335
args = parser.parse_args(argv)
@@ -38,3 +40,7 @@ def main(argv=None):
3840
retv |= check_has_shebang(filename)
3941

4042
return retv
43+
44+
45+
if __name__ == '__main__':
46+
exit(main())

pre_commit_hooks/check_json.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import io
55
import json
66
import sys
7+
from typing import Optional
8+
from typing import Sequence
79

810

9-
def check_json(argv=None):
11+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
1012
parser = argparse.ArgumentParser()
1113
parser.add_argument('filenames', nargs='*', help='JSON filenames to check.')
1214
args = parser.parse_args(argv)
@@ -22,4 +24,4 @@ def check_json(argv=None):
2224

2325

2426
if __name__ == '__main__':
25-
sys.exit(check_json())
27+
sys.exit(main())

pre_commit_hooks/check_merge_conflict.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import argparse
44
import os.path
5+
from typing import Optional
6+
from typing import Sequence
7+
58

69
CONFLICT_PATTERNS = [
710
b'<<<<<<< ',
@@ -12,7 +15,7 @@
1215
WARNING_MSG = 'Merge conflict string "{0}" found in {1}:{2}'
1316

1417

15-
def is_in_merge():
18+
def is_in_merge(): # type: () -> int
1619
return (
1720
os.path.exists(os.path.join('.git', 'MERGE_MSG')) and
1821
(
@@ -23,7 +26,7 @@ def is_in_merge():
2326
)
2427

2528

26-
def detect_merge_conflict(argv=None):
29+
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
2730
parser = argparse.ArgumentParser()
2831
parser.add_argument('filenames', nargs='*')
2932
parser.add_argument('--assume-in-merge', action='store_true')
@@ -47,4 +50,4 @@ def detect_merge_conflict(argv=None):
4750

4851

4952
if __name__ == '__main__':
50-
exit(detect_merge_conflict())
53+
exit(main())

0 commit comments

Comments
 (0)