pre-commit-hooks: python3.6+

This commit is contained in:
Anthony Sottile
2020-02-05 11:10:42 -08:00
parent 551d1a07b3
commit f5c42a050b
60 changed files with 291 additions and 493 deletions

View File

@ -1,10 +1,5 @@
from __future__ import print_function
import argparse
import io
import json
import sys
from collections import OrderedDict
from difflib import unified_diff
from typing import List
from typing import Mapping
@ -13,38 +8,36 @@ from typing import Sequence
from typing import Tuple
from typing import Union
from six import text_type
def _get_pretty_format(
contents, indent, ensure_ascii=True, sort_keys=True, top_keys=(),
): # type: (str, str, bool, bool, Sequence[str]) -> str
def pairs_first(pairs):
# type: (Sequence[Tuple[str, str]]) -> Mapping[str, str]
contents: str,
indent: str,
ensure_ascii: bool = True,
sort_keys: bool = True,
top_keys: Sequence[str] = (),
) -> str:
def pairs_first(pairs: Sequence[Tuple[str, str]]) -> Mapping[str, str]:
before = [pair for pair in pairs if pair[0] in top_keys]
before = sorted(before, key=lambda x: top_keys.index(x[0]))
after = [pair for pair in pairs if pair[0] not in top_keys]
if sort_keys:
after = sorted(after, key=lambda x: x[0])
return OrderedDict(before + after)
after.sort()
return dict(before + after)
json_pretty = json.dumps(
json.loads(contents, object_pairs_hook=pairs_first),
indent=indent,
ensure_ascii=ensure_ascii,
# Workaround for https://bugs.python.org/issue16333
separators=(',', ': '),
)
# Ensure unicode (Py2) and add the newline that dumps does not end with.
return text_type(json_pretty) + '\n'
return f'{json_pretty}\n'
def _autofix(filename, new_contents): # type: (str, str) -> None
print('Fixing file {}'.format(filename))
with io.open(filename, 'w', encoding='UTF-8') as f:
def _autofix(filename: str, new_contents: str) -> None:
print(f'Fixing file {filename}')
with open(filename, 'w', encoding='UTF-8') as f:
f.write(new_contents)
def parse_num_to_int(s): # type: (str) -> Union[int, str]
def parse_num_to_int(s: str) -> Union[int, str]:
"""Convert string numbers to int, leaving strings as is."""
try:
return int(s)
@ -52,18 +45,18 @@ def parse_num_to_int(s): # type: (str) -> Union[int, str]
return s
def parse_topkeys(s): # type: (str) -> List[str]
def parse_topkeys(s: str) -> List[str]:
return s.split(',')
def get_diff(source, target, file): # type: (str, str, str) -> str
def get_diff(source: str, target: str, file: str) -> str:
source_lines = source.splitlines(True)
target_lines = target.splitlines(True)
diff = unified_diff(source_lines, target_lines, fromfile=file, tofile=file)
return ''.join(diff)
def main(argv=None): # type: (Optional[Sequence[str]]) -> int
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
'--autofix',
@ -110,7 +103,7 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
status = 0
for json_file in args.filenames:
with io.open(json_file, encoding='UTF-8') as f:
with open(json_file, encoding='UTF-8') as f:
contents = f.read()
try:
@ -131,8 +124,8 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
status = 1
except ValueError:
print(
'Input File {} is not a valid JSON, consider using check-json'
.format(json_file),
f'Input File {json_file} is not a valid JSON, consider using '
f'check-json',
)
return 1
@ -140,4 +133,4 @@ def main(argv=None): # type: (Optional[Sequence[str]]) -> int
if __name__ == '__main__':
sys.exit(main())
exit(main())