mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 18:16:52 +00:00
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/setup-cfg-fmt: v2.1.0 → v2.2.0](https://github.com/asottile/setup-cfg-fmt/compare/v2.1.0...v2.2.0) - [github.com/asottile/reorder_python_imports: v3.8.5 → v3.9.0](https://github.com/asottile/reorder_python_imports/compare/v3.8.5...v3.9.0) - [github.com/asottile/pyupgrade: v3.1.0 → v3.2.0](https://github.com/asottile/pyupgrade/compare/v3.1.0...v3.2.0) - [github.com/pre-commit/mirrors-autopep8: v1.7.0 → v2.0.0](https://github.com/pre-commit/mirrors-autopep8/compare/v1.7.0...v2.0.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/pyupgrade: v3.2.0 → v3.2.2](https://github.com/asottile/pyupgrade/compare/v3.2.0...v3.2.2) - [github.com/pre-commit/mirrors-mypy: v0.982 → v0.990](https://github.com/pre-commit/mirrors-mypy/compare/v0.982...v0.990) * remove no_implicit_optional this is the default in mypy 0.990 Committed via https://github.com/asottile/all-repos * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v0.990 → v0.991](https://github.com/pre-commit/mirrors-mypy/compare/v0.990...v0.991) * v4.4.0 * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/PyCQA/flake8: 5.0.4 → 6.0.0](https://github.com/PyCQA/flake8/compare/5.0.4...6.0.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/pyupgrade: v3.2.2 → v3.3.0](https://github.com/asottile/pyupgrade/compare/v3.2.2...v3.3.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/add-trailing-comma: v2.3.0 → v2.4.0](https://github.com/asottile/add-trailing-comma/compare/v2.3.0...v2.4.0) - [github.com/asottile/pyupgrade: v3.3.0 → v3.3.1](https://github.com/asottile/pyupgrade/compare/v3.3.0...v3.3.1) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-autopep8: v2.0.0 → v2.0.1](https://github.com/pre-commit/mirrors-autopep8/compare/v2.0.0...v2.0.1) * azure pipelines -> github actions * add constraints file to requirements-txt-fixer files * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v0.991 → v1.0.0](https://github.com/pre-commit/mirrors-mypy/compare/v0.991...v1.0.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.0.0 → v1.0.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.0.0...v1.0.1) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-autopep8: v2.0.1 → v2.0.2](https://github.com/pre-commit/mirrors-autopep8/compare/v2.0.1...v2.0.2) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.0.1 → v1.1.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.0.1...v1.1.1) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.1.1 → v1.2.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.1.1...v1.2.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/pyupgrade: v3.3.1 → v3.3.2](https://github.com/asottile/pyupgrade/compare/v3.3.1...v3.3.2) * [pre-commit.ci] pre-commit autoupdate updates: - https://github.com/asottile/reorder_python_imports → https://github.com/asottile/reorder-python-imports - [github.com/asottile/pyupgrade: v3.3.2 → v3.4.0](https://github.com/asottile/pyupgrade/compare/v3.3.2...v3.4.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.2.0 → v1.3.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.2.0...v1.3.0) * fix tags trigger for github actions the old syntax worked for azure pipelines but not GHA Committed via https://github.com/asottile/all-repos * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/setup-cfg-fmt: v2.2.0 → v2.3.0](https://github.com/asottile/setup-cfg-fmt/compare/v2.2.0...v2.3.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/add-trailing-comma: v2.4.0 → v2.5.1](https://github.com/asottile/add-trailing-comma/compare/v2.4.0...v2.5.1) - [github.com/asottile/pyupgrade: v3.4.0 → v3.6.0](https://github.com/asottile/pyupgrade/compare/v3.4.0...v3.6.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/reorder-python-imports: v3.9.0 → v3.10.0](https://github.com/asottile/reorder-python-imports/compare/v3.9.0...v3.10.0) - [github.com/asottile/pyupgrade: v3.6.0 → v3.7.0](https://github.com/asottile/pyupgrade/compare/v3.6.0...v3.7.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.3.0 → v1.4.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.3.0...v1.4.1) * py38-plus Committed via https://github.com/asottile/all-repos * shlex.join is always available in 3.8+ * Fix missing file-contents-sorter options in the README * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/pyupgrade: v3.8.0 → v3.9.0](https://github.com/asottile/pyupgrade/compare/v3.8.0...v3.9.0) * Fix a typo in check_yaml * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/add-trailing-comma: v3.0.0 → v3.0.1](https://github.com/asottile/add-trailing-comma/compare/v3.0.0...v3.0.1) - [github.com/asottile/pyupgrade: v3.9.0 → v3.10.1](https://github.com/asottile/pyupgrade/compare/v3.9.0...v3.10.1) - [github.com/PyCQA/flake8: 6.0.0 → 6.1.0](https://github.com/PyCQA/flake8/compare/6.0.0...6.1.0) * Add bpdb to debug_statements_hook This patch adds the bpython debugger import `bpdb` to the debug_statements_hook. * Fix blank lines As stated in the documentation: `Note that this hook WILL remove blank lines` Previously this hook would always add blank a blank line. With this fix, if the file is empty, a newline will not be added, and multpline blanklines in a file will be removed. Fixes #935 * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.4.1 → v1.5.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.4.1...v1.5.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.5.0 → v1.5.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.5.0...v1.5.1) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-autopep8: v2.0.2 → v2.0.4](https://github.com/pre-commit/mirrors-autopep8/compare/v2.0.2...v2.0.4) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/add-trailing-comma: v3.0.1 → v3.1.0](https://github.com/asottile/add-trailing-comma/compare/v3.0.1...v3.1.0) - https://github.com/pre-commit/mirrors-autopep8 → https://github.com/hhatto/autopep8 * Document newline behavior of `file-contents-sorter` * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/reorder-python-imports: v3.10.0 → v3.11.0](https://github.com/asottile/reorder-python-imports/compare/v3.10.0...v3.11.0) - [github.com/asottile/pyupgrade: v3.10.1 → v3.11.0](https://github.com/asottile/pyupgrade/compare/v3.10.1...v3.11.0) * Remove redundant int() conversion * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/pyupgrade: v3.11.0 → v3.13.0](https://github.com/asottile/pyupgrade/compare/v3.11.0...v3.13.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/setup-cfg-fmt: v2.4.0 → v2.5.0](https://github.com/asottile/setup-cfg-fmt/compare/v2.4.0...v2.5.0) - [github.com/asottile/reorder-python-imports: v3.11.0 → v3.12.0](https://github.com/asottile/reorder-python-imports/compare/v3.11.0...v3.12.0) - [github.com/asottile/pyupgrade: v3.13.0 → v3.14.0](https://github.com/asottile/pyupgrade/compare/v3.13.0...v3.14.0) * don't rewrite string quotes inside f-strings * v4.5.0 * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/asottile/pyupgrade: v3.14.0 → v3.15.0](https://github.com/asottile/pyupgrade/compare/v3.14.0...v3.15.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.5.1 → v1.6.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.5.1...v1.6.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.6.0 → v1.6.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.6.0...v1.6.1) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.6.1 → v1.7.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.6.1...v1.7.0) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/mirrors-mypy: v1.7.0 → v1.7.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.7.0...v1.7.1) --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Anthony Sottile <asottile@umich.edu> Co-authored-by: Lev Blit <levblt1@gmail.com> Co-authored-by: Abel Soares Siqueira <abel.s.siqueira@gmail.com> Co-authored-by: Viktor Szépe <viktor@szepe.net> Co-authored-by: mwip <matthias.weigand@protonmail.com> Co-authored-by: Roel Adriaans <roel@adriaans.org> Co-authored-by: Max R <mxr@users.noreply.github.com> Co-authored-by: Mészáros Gergely <maetveis@gmail.com> Co-authored-by: Miroslav Šedivý <6774676+eumiro@users.noreply.github.com>
93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import io
|
|
import re
|
|
import sys
|
|
import tokenize
|
|
from typing 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]*"')
|
|
|
|
|
|
def handle_match(token_text: str) -> str:
|
|
if '"""' in token_text or "'''" in token_text:
|
|
return token_text
|
|
|
|
match = START_QUOTE_RE.match(token_text)
|
|
if match is not None:
|
|
meat = token_text[match.end():-1]
|
|
if '"' in meat or "'" in meat:
|
|
return token_text
|
|
else:
|
|
return match.group().replace('"', "'") + meat + "'"
|
|
else:
|
|
return token_text
|
|
|
|
|
|
def get_line_offsets_by_line_no(src: str) -> list[int]:
|
|
# Padded so we can index with line number
|
|
offsets = [-1, 0]
|
|
for line in src.splitlines(True):
|
|
offsets.append(offsets[-1] + len(line))
|
|
return offsets
|
|
|
|
|
|
def fix_strings(filename: str) -> int:
|
|
with open(filename, encoding='UTF-8', newline='') as f:
|
|
contents = f.read()
|
|
line_offsets = get_line_offsets_by_line_no(contents)
|
|
|
|
# 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 == 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:
|
|
line_offsets[erow] + ecol
|
|
] = new_text
|
|
|
|
new_contents = ''.join(splitcontents)
|
|
if contents != new_contents:
|
|
with open(filename, 'w', encoding='UTF-8', newline='') as f:
|
|
f.write(new_contents)
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
|
|
def main(argv: Sequence[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
|
args = parser.parse_args(argv)
|
|
|
|
retv = 0
|
|
|
|
for filename in args.filenames:
|
|
return_value = fix_strings(filename)
|
|
if return_value != 0:
|
|
print(f'Fixing strings in {filename}')
|
|
retv |= return_value
|
|
|
|
return retv
|
|
|
|
|
|
if __name__ == '__main__':
|
|
raise SystemExit(main())
|