Skip to content

Commit 9a9bbc0

Browse files
committed
Rewrite, fixes issue#43
* Added type annotations to hooks * Removed hardcoded regexes identified by qq7te * Use types: in .pre-commit-config.yaml to filter for correct files instead of files: regex
1 parent 093614d commit 9a9bbc0

14 files changed

+84
-90
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ repos:
1919
- id: check-yaml
2020
- id: debug-statements
2121
- id: requirements-txt-fixer
22-
- repo: https://github.com/pre-commit/pre-commit
23-
rev: b8300268bf2d8797f28edf9063d7e5659ad4d535
24-
hooks:
25-
- id: validate_manifest
2622
- repo: https://github.com/asottile/reorder_python_imports
2723
rev: bc7b5b2f0fde191c9d0121588ef9bbb79f8e5e21
2824
hooks:
2925
- id: reorder-python-imports
3026
language_version: python3
27+
- repo: https://github.com/pre-commit/pre-commit
28+
rev: v2.16.0
29+
hooks:
30+
- id: validate_manifest
3131
- repo: https://github.com/asottile/pyupgrade
3232
rev: v1.16.1
3333
hooks:
@@ -37,12 +37,6 @@ repos:
3737
hooks:
3838
- id: black
3939
args: ["-l", "120"]
40-
- repo: https://github.com/jumanjihouse/pre-commit-hooks
41-
rev: 1.11.0
42-
hooks:
43-
- id: shellcheck
44-
args: ["--color=always", "--shell=bash", "-x", "-a", "--exclude=SC1090"]
45-
additional_dependencies: [shellcheck]
4640
- repo: local
4741
hooks:
4842
- id: tests

.pre-commit-hooks.yaml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,42 @@
77
name: clang-format
88
entry: clang-format-hook
99
description: Formats C, C++, Objective-C, and Java code
10-
files: \.(c|cc|cxx|cpp|h|hpp|hxx|m|mm|java)$
10+
types_or: [c, c++, c#, objective-c, java]
1111
language: python
1212
- id: clang-tidy
1313
name: clang-tidy
1414
entry: clang-tidy-hook
1515
description: Find warnings/errors in C, C++, and Objective-C code
16-
files: \.(c|cc|cxx|cpp|h|hpp|hxx|m)$
16+
types_or: [c, c++, c#, objective-c]
1717
language: python
1818
- id: oclint
1919
name: oclint
2020
entry: oclint-hook
2121
description: Find warnings/errors in C, C++, and Objective-C code
22-
files: \.(c|cc|cxx|cpp|h|hpp|hxx|m)$
22+
types_or: [c, c++, c#, objective-c]
2323
language: python
2424
- id: uncrustify
2525
name: uncrustify
2626
entry: uncrustify-hook
2727
description: Formats C, C++, Objective-C, Java, D, and Vala code
28-
files: \.(c|cc|cxx|cpp|h|hpp|hxx|m|mm|d|java|vala)$
28+
# D, vala currently aren't valid file types. See https://github.com/pre-commit/identify/issues/258
29+
types_or: [c, c++, c#, objective-c, java]
2930
language: python
3031
- id: cppcheck
3132
name: cppcheck
3233
entry: cppcheck-hook
3334
description: Find warnings/errors in C, C++, and Objective-C code
34-
files: \.(c|cc|cxx|cpp|h|hpp|hxx|m)$
35+
types_or: [c, c++, c#, objective-c]
3536
language: python
3637
- id: cpplint
3738
name: cpplint
3839
entry: cpplint-hook
3940
description: Find warnings/errors in C/CPP code
40-
files: \.(c|cc|cpp|cu|cuh|cxx|h|hh|hpp|hxx)$
41+
types_or: [c, c++, c#, objective-c, cuda]
4142
language: python
4243
- id: include-what-you-use
4344
name: include-what-you-use
4445
entry: include-what-you-use-hook
4546
description: Runs Include-What-You-Use (iwyu) in C/CPP code
46-
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
47+
types_or: [c, c++, c#, objective-c, cuda]
4748
language: python

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
clean:
55
rm -rf dist *.egg-info
66

7-
test:
8-
rm ~/.local/bin/*-hook && pip install . && pytest -x -vvv
7+
install:
8+
rm ~/.local/bin/*-hook && pip3 install . --user
9+
10+
test: install
11+
pytest -x -vvv --pdb
912

1013
# Test with fresh installs of downloaded utilities
1114
test_fresh_installs:
1215
./tests/run_tests.sh
1316

14-
install:
15-
pip3 install . --user
16-
1717
upload: clean
1818
python3 setup.py sdist
1919
twine upload dist/* --verbose

hooks/clang_format.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
"""Wrapper script for clang-format"""
33
import sys
4+
from typing import List
45

56
from hooks.utils import FormatterCmd
67

@@ -11,7 +12,7 @@ class ClangFormatCmd(FormatterCmd):
1112
command = "clang-format"
1213
lookbehind = "clang-format version "
1314

14-
def __init__(self, args):
15+
def __init__(self, args: List[str]):
1516
super().__init__(self.command, self.lookbehind, args)
1617
self.check_installed()
1718
self.parse_args(args)
@@ -27,7 +28,7 @@ def run(self):
2728
sys.exit(self.returncode)
2829

2930

30-
def main(argv=None):
31+
def main(argv: List[str] = sys.argv):
3132
cmd = ClangFormatCmd(argv)
3233
cmd.run()
3334

hooks/clang_tidy.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""Wrapper script for clang-tidy."""
33
import re
44
import sys
5+
from typing import List
56

67
from hooks.utils import StaticAnalyzerCmd
78

@@ -12,7 +13,7 @@ class ClangTidyCmd(StaticAnalyzerCmd):
1213
command = "clang-tidy"
1314
lookbehind = "LLVM version "
1415

15-
def __init__(self, args):
16+
def __init__(self, args: List[str]):
1617
super().__init__(self.command, self.lookbehind, args)
1718
self.parse_args(args)
1819
self.edit_in_place = "-fix" in self.args or "--fix-errors" in self.args
@@ -28,7 +29,7 @@ def run(self):
2829
self.exit_on_error()
2930

3031

31-
def main(argv=None):
32+
def main(argv: List[str] = sys.argv):
3233
cmd = ClangTidyCmd(argv)
3334
cmd.run()
3435

hooks/cppcheck.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
"""Wrapper script for cppcheck."""
33
import sys
4+
from typing import List
45

56
from hooks.utils import StaticAnalyzerCmd
67

@@ -11,7 +12,7 @@ class CppcheckCmd(StaticAnalyzerCmd):
1112
command = "cppcheck"
1213
lookbehind = "Cppcheck "
1314

14-
def __init__(self, args):
15+
def __init__(self, args: List[str]):
1516
super().__init__(self.command, self.lookbehind, args)
1617
self.parse_args(args)
1718
# quiet for stdout purposes
@@ -32,7 +33,7 @@ def run(self):
3233
self.exit_on_error()
3334

3435

35-
def main(argv=None):
36+
def main(argv: List[str] = sys.argv):
3637
cmd = CppcheckCmd(argv)
3738
cmd.run()
3839

hooks/cpplint.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
"""Wrapper script for cpplint."""
33
import sys
4+
from typing import List
45

56
from hooks.utils import StaticAnalyzerCmd
67

@@ -11,7 +12,7 @@ class CpplintCmd(StaticAnalyzerCmd):
1112
command = "cpplint"
1213
lookbehind = "cpplint "
1314

14-
def __init__(self, args):
15+
def __init__(self, args: List[str]):
1516
super().__init__(self.command, self.lookbehind, args)
1617
self.parse_args(args)
1718
self.add_if_missing(["--verbose=0"])
@@ -23,7 +24,7 @@ def run(self):
2324
self.exit_on_error()
2425

2526

26-
def main(argv=None):
27+
def main(argv: List[str] = sys.argv):
2728
cmd = CpplintCmd(argv)
2829
cmd.run()
2930

hooks/include_what_you_use.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33
"""Wrapper for include-what-you-use"""
44
import sys
5+
from typing import List
56

67
from hooks.utils import StaticAnalyzerCmd
78

@@ -12,7 +13,7 @@ class IncludeWhatYouUseCmd(StaticAnalyzerCmd):
1213
command = "include-what-you-use"
1314
lookbehind = "include-what-you-use "
1415

15-
def __init__(self, args):
16+
def __init__(self, args: List[str]):
1617
super().__init__(self.command, self.lookbehind, args)
1718
self.check_installed()
1819
self.parse_args(args)
@@ -32,7 +33,7 @@ def run(self):
3233
sys.exit(self.returncode)
3334

3435

35-
def main(argv=None):
36+
def main(argv: List[str] = sys.argv):
3637
cmd = IncludeWhatYouUseCmd(argv)
3738
cmd.run()
3839

hooks/oclint.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
"""Wrapper script for oclint"""
33
import os
4+
import sys
5+
from typing import List
46

57
from hooks.utils import StaticAnalyzerCmd
68

@@ -11,7 +13,7 @@ class OCLintCmd(StaticAnalyzerCmd):
1113
command = "oclint"
1214
lookbehind = "OCLint version "
1315

14-
def __init__(self, args):
16+
def __init__(self, args: List[str]):
1517
super().__init__(self.command, self.lookbehind, args)
1618
self.version = self.get_version_str()
1719
self.parse_args(args)
@@ -44,15 +46,15 @@ def run(self):
4446
self.cleanup_files(current_files)
4547

4648
@staticmethod
47-
def cleanup_files(existing_files):
49+
def cleanup_files(existing_files: List[str]):
4850
"""Delete the plist files that oclint generates."""
4951
new_files = os.listdir(os.getcwd())
5052
for filename in new_files:
5153
if filename not in existing_files and filename[-6:] == ".plist":
5254
os.remove(filename)
5355

5456

55-
def main(argv=None):
57+
def main(argv: List[str] = sys.argv):
5658
cmd = OCLintCmd(argv)
5759
cmd.run()
5860

hooks/uncrustify.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import re
55
import subprocess as sp
66
import sys
7+
from typing import List
78

89
from hooks.utils import FormatterCmd
910

@@ -14,7 +15,7 @@ class UncrustifyCmd(FormatterCmd):
1415
command = "uncrustify"
1516
lookbehind = "[uU]ncrustify[- ]"
1617

17-
def __init__(self, args):
18+
def __init__(self, args: List[str]):
1819
super().__init__(self.command, self.lookbehind, args)
1920
self.check_installed()
2021
self.parse_args(args)
@@ -49,7 +50,7 @@ def run(self):
4950
sys.exit(self.returncode)
5051

5152

52-
def main(argv=None):
53+
def main(argv: List[str] = sys.argv):
5354
cmd = UncrustifyCmd(argv)
5455
cmd.run()
5556

0 commit comments

Comments
 (0)