mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 19:26:52 +00:00
Merge branch 'main' into empty-object-with-newline
This commit is contained in:
commit
a23cc57a37
44 changed files with 321 additions and 97 deletions
|
|
@ -4,7 +4,7 @@ import argparse
|
|||
import math
|
||||
import os
|
||||
import subprocess
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
from pre_commit_hooks.util import added_files
|
||||
from pre_commit_hooks.util import zsplit
|
||||
|
|
@ -46,7 +46,7 @@ def find_large_added_files(
|
|||
filenames_filtered &= added_files()
|
||||
|
||||
for filename in filenames_filtered:
|
||||
kb = int(math.ceil(os.stat(filename).st_size / 1024))
|
||||
kb = math.ceil(os.stat(filename).st_size / 1024)
|
||||
if kb > maxkb:
|
||||
print(f'{filename} ({kb} KB) exceeds {maxkb} KB.')
|
||||
retv = 1
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import ast
|
|||
import platform
|
||||
import sys
|
||||
import traceback
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import ast
|
||||
from collections.abc import Sequence
|
||||
from typing import NamedTuple
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
BUILTIN_TYPES = {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from typing import Iterable
|
||||
from typing import Iterator
|
||||
from typing import Sequence
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Iterator
|
||||
from collections.abc import Sequence
|
||||
|
||||
from pre_commit_hooks.util import added_files
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import io
|
||||
import tokenize
|
||||
from collections.abc import Sequence
|
||||
from tokenize import tokenize as tokenize_tokenize
|
||||
from typing import Sequence
|
||||
|
||||
NON_CODE_TOKENS = frozenset((
|
||||
tokenize.COMMENT, tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL,
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import shlex
|
||||
import sys
|
||||
from typing import Generator
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Sequence
|
||||
from typing import NamedTuple
|
||||
from typing import Sequence
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
from pre_commit_hooks.util import zsplit
|
||||
|
|
@ -35,7 +35,7 @@ class GitLsFile(NamedTuple):
|
|||
filename: str
|
||||
|
||||
|
||||
def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile, None, None]:
|
||||
def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile]:
|
||||
outs = cmd_output('git', 'ls-files', '-z', '--stage', '--', *paths)
|
||||
for out in zsplit(outs):
|
||||
metadata, filename = out.split('\t')
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import json
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def raise_duplicate_keys(
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import os.path
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import shlex
|
||||
import sys
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
from pre_commit_hooks.check_executables_have_shebangs import EXECUTABLE_VALUES
|
||||
from pre_commit_hooks.check_executables_have_shebangs import git_ls_files
|
||||
|
|
@ -36,7 +36,7 @@ def _message(path: str) -> None:
|
|||
f'`chmod +x {shlex.quote(path)}`\n'
|
||||
f' If on Windows, you may also need to: '
|
||||
f'`git add --chmod=+x {shlex.quote(path)}`\n'
|
||||
f' If it not supposed to be executable, double-check its shebang '
|
||||
f' If it is not supposed to be executable, double-check its shebang '
|
||||
f'is wanted.\n',
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import os.path
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import sys
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
if sys.version_info >= (3, 11): # pragma: >=3.11 cover
|
||||
import tomllib
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import re
|
||||
import sys
|
||||
from typing import Pattern
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
from re import Pattern
|
||||
|
||||
|
||||
def _get_pattern(domain: str) -> Pattern[bytes]:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import xml.sax.handler
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -1,17 +1,17 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections.abc import Generator
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
from typing import Generator
|
||||
from typing import NamedTuple
|
||||
from typing import Sequence
|
||||
|
||||
import ruamel.yaml
|
||||
|
||||
yaml = ruamel.yaml.YAML(typ='safe')
|
||||
|
||||
|
||||
def _exhaust(gen: Generator[str, None, None]) -> None:
|
||||
def _exhaust(gen: Generator[str]) -> None:
|
||||
for _ in gen:
|
||||
pass
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
'--unsafe', action='store_true',
|
||||
help=(
|
||||
'Instead of loading the files, simply parse them for syntax. '
|
||||
'A syntax-only check enables extensions and unsafe contstructs '
|
||||
'A syntax-only check enables extensions and unsafe constructs '
|
||||
'which would otherwise be forbidden. Using this option removes '
|
||||
'all guarantees of portability to other yaml implementations. '
|
||||
'Implies --allow-multiple-documents'
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import ast
|
||||
import traceback
|
||||
from collections.abc import Sequence
|
||||
from typing import NamedTuple
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
DEBUG_STATEMENTS = {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import shlex
|
||||
import subprocess
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
from pre_commit_hooks.util import zsplit
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import configparser
|
||||
import os
|
||||
from collections.abc import Sequence
|
||||
from typing import NamedTuple
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
class BadFile(NamedTuple):
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
BLACKLIST = [
|
||||
b'BEGIN RSA PRIVATE KEY',
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import os
|
||||
from collections.abc import Sequence
|
||||
from typing import IO
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def fix_file(file_obj: IO[bytes]) -> int:
|
||||
|
|
|
|||
|
|
@ -12,11 +12,11 @@ conflicts and keep the file nicely ordered.
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import IO
|
||||
from typing import Iterable
|
||||
from typing import Sequence
|
||||
|
||||
PASS = 0
|
||||
FAIL = 1
|
||||
|
|
@ -54,18 +54,21 @@ def sort_file_contents(
|
|||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('filenames', nargs='+', help='Files to sort')
|
||||
parser.add_argument(
|
||||
|
||||
mutex = parser.add_mutually_exclusive_group(required=False)
|
||||
mutex.add_argument(
|
||||
'--ignore-case',
|
||||
action='store_const',
|
||||
const=bytes.lower,
|
||||
default=None,
|
||||
help='fold lower case to upper case characters',
|
||||
)
|
||||
parser.add_argument(
|
||||
mutex.add_argument(
|
||||
'--unique',
|
||||
action='store_true',
|
||||
help='ensure each line is unique',
|
||||
)
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
retv = PASS
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from collections.abc import Sequence
|
||||
from typing import IO
|
||||
from typing import NamedTuple
|
||||
from typing import Sequence
|
||||
|
||||
DEFAULT_PRAGMA = b'# -*- coding: utf-8 -*-'
|
||||
|
||||
|
|
@ -107,6 +108,13 @@ def _normalize_pragma(pragma: str) -> bytes:
|
|||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
print(
|
||||
'warning: this hook is deprecated and will be removed in a future '
|
||||
'release because py2 is EOL. instead, use '
|
||||
'https://github.com/asottile/pyupgrade',
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
'Fixes the encoding pragma of python files',
|
||||
)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import os
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import collections
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
CRLF = b'\r\n'
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import re
|
||||
from collections.abc import Sequence
|
||||
from typing import AbstractSet
|
||||
from typing import Sequence
|
||||
|
||||
from pre_commit_hooks.util import CalledProcessError
|
||||
from pre_commit_hooks.util import cmd_output
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ import argparse
|
|||
import json
|
||||
import re
|
||||
import sys
|
||||
from collections.abc import Mapping
|
||||
from collections.abc import Sequence
|
||||
from difflib import unified_diff
|
||||
from typing import Mapping
|
||||
from typing import Sequence
|
||||
|
||||
def _insert_linebreaks(json_str: str) -> str:
|
||||
return re.sub(
|
||||
|
|
@ -135,17 +135,22 @@ def main(argv: Sequence[str] | None = None) -> int:
|
|||
f'Input File {json_file} is not a valid JSON, consider using '
|
||||
f'check-json',
|
||||
)
|
||||
return 1
|
||||
|
||||
if contents != pretty_contents:
|
||||
status = 1
|
||||
if args.autofix:
|
||||
_autofix(json_file, pretty_contents)
|
||||
if args.empty_object_with_newline:
|
||||
status = 0
|
||||
else:
|
||||
diff_output = get_diff(contents, pretty_contents, json_file)
|
||||
sys.stdout.buffer.write(diff_output.encode())
|
||||
else:
|
||||
if contents != pretty_contents:
|
||||
if args.autofix:
|
||||
_autofix(json_file, pretty_contents)
|
||||
if args.empty_object_with_newline:
|
||||
status = 0
|
||||
else:
|
||||
diff_output = get_diff(
|
||||
contents,
|
||||
pretty_contents,
|
||||
json_file
|
||||
)
|
||||
sys.stdout.buffer.write(diff_output.encode())
|
||||
|
||||
status = 1
|
||||
|
||||
return status
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import re
|
||||
from collections.abc import Sequence
|
||||
from typing import IO
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
PASS = 0
|
||||
|
|
@ -45,6 +45,11 @@ class Requirement:
|
|||
elif requirement.value == b'\n':
|
||||
return False
|
||||
else:
|
||||
# if 2 requirements have the same name, the one with comments
|
||||
# needs to go first (so that when removing duplicates, the one
|
||||
# with comments is kept)
|
||||
if self.name == requirement.name:
|
||||
return bool(self.comments) > bool(requirement.comments)
|
||||
return self.name < requirement.name
|
||||
|
||||
def is_complete(self) -> bool:
|
||||
|
|
@ -110,13 +115,20 @@ def fix_requirements(f: IO[bytes]) -> int:
|
|||
# which is automatically added by broken pip package under Debian
|
||||
requirements = [
|
||||
req for req in requirements
|
||||
if req.value != b'pkg-resources==0.0.0\n'
|
||||
if req.value not in [
|
||||
b'pkg-resources==0.0.0\n',
|
||||
b'pkg_resources==0.0.0\n',
|
||||
]
|
||||
]
|
||||
|
||||
# sort the requirements and remove duplicates
|
||||
prev = None
|
||||
for requirement in sorted(requirements):
|
||||
after.extend(requirement.comments)
|
||||
assert requirement.value, requirement.value
|
||||
after.append(requirement.value)
|
||||
if prev is None or requirement.value != prev.value:
|
||||
after.append(requirement.value)
|
||||
prev = requirement
|
||||
after.extend(rest)
|
||||
|
||||
after_string = b''.join(after)
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ complicated YAML files.
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
QUOTES = ["'", '"']
|
||||
|
|
|
|||
|
|
@ -3,8 +3,15 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import io
|
||||
import re
|
||||
import sys
|
||||
import tokenize
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
|
||||
FSTRING_START = tokenize.FSTRING_START
|
||||
FSTRING_END = tokenize.FSTRING_END
|
||||
else: # pragma: <3.12 cover
|
||||
FSTRING_START = FSTRING_END = -1
|
||||
|
||||
START_QUOTE_RE = re.compile('^[a-zA-Z]*"')
|
||||
|
||||
|
|
@ -40,11 +47,17 @@ def fix_strings(filename: str) -> int:
|
|||
# Basically a mutable string
|
||||
splitcontents = list(contents)
|
||||
|
||||
fstring_depth = 0
|
||||
|
||||
# Iterate in reverse so the offsets are always correct
|
||||
tokens_l = list(tokenize.generate_tokens(io.StringIO(contents).readline))
|
||||
tokens = reversed(tokens_l)
|
||||
for token_type, token_text, (srow, scol), (erow, ecol), _ in tokens:
|
||||
if token_type == tokenize.STRING:
|
||||
if token_type == FSTRING_START: # pragma: >=3.12 cover
|
||||
fstring_depth += 1
|
||||
elif token_type == FSTRING_END: # pragma: >=3.12 cover
|
||||
fstring_depth -= 1
|
||||
elif fstring_depth == 0 and token_type == tokenize.STRING:
|
||||
new_text = handle_match(token_text)
|
||||
splitcontents[
|
||||
line_offsets[srow] + scol:
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import os.path
|
||||
import re
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import argparse
|
||||
import os
|
||||
from typing import Sequence
|
||||
from collections.abc import Sequence
|
||||
|
||||
|
||||
def _fix_file(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue