Skip to content

pre-commit: Add pre-commit-hooks and auto-walrus #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@
# Then in the project root directory run `pre-commit install`

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-builtin-literals
- id: check-case-conflict
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- id: check-toml
- id: check-vcs-permalinks
- id: check-xml
- id: check-yaml
- id: debug-statements
- id: destroyed-symlinks
- id: detect-private-key
# - id: end-of-file-fixer # TODO(cclauss): Causes some tests to fail.
- id: file-contents-sorter
- id: fix-byte-order-marker
- id: forbid-new-submodules
- id: forbid-submodules
# - id: mixed-line-ending # TODO(cclauss): Causes some tests to fail.
# args:
# - --fix=lf
- id: name-tests-test
- id: pretty-format-json
- id: requirements-txt-fixer
- id: sort-simple-yaml
# - id: trailing-whitespace # TODO(cclauss): Causes some tests to fail.

- repo: https://github.com/MarcoGorelli/auto-walrus
rev: 0.3.4
hooks:
- id: auto-walrus

- repo: https://github.com/codespell-project/codespell
rev: v2.4.1
hooks:
Expand Down
27 changes: 9 additions & 18 deletions cpplint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,7 @@ def ParseNolintSuppressions(filename, raw_line, linenum, error):
linenum: int, the number of the current line.
error: function, an error handler.
"""
matched = re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line)
if matched:
if matched := re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line):
no_lint_type = matched.group(1)
if no_lint_type == "NEXTLINE":

Expand Down Expand Up @@ -2460,8 +2459,7 @@ def GetIndentLevel(line):
Returns:
An integer count of leading spaces, possibly zero.
"""
indent = re.match(r"^( *)\S", line)
if indent:
if indent := re.match(r"^( *)\S", line):
return len(indent.group(1))
return 0

Expand Down Expand Up @@ -3935,8 +3933,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum, function_state, erro

starting_func = False
regexp = r"(\w(\w|::|\*|\&|\s)*)\(" # decls * & space::name( ...
match_result = re.match(regexp, line)
if match_result:
if match_result := re.match(regexp, line):
# If the name is all caps and underscores, figure it's a macro and
# ignore it, unless it's TEST or TEST_F.
function_name = match_result.group(1).split()[-1]
Expand Down Expand Up @@ -4410,8 +4407,7 @@ def _IsType(clean_lines, nesting_state, expr):
True, if token looks like a type.
"""
# Keep only the last token in the expression
last_word = re.match(r"^.*(\b\S+)$", expr)
if last_word:
if last_word := re.match(r"^.*(\b\S+)$", expr):
token = last_word.group(1)
else:
token = expr
Expand Down Expand Up @@ -4479,9 +4475,8 @@ def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
# And since you should never have braces at the beginning of a line,
# this is an easy test. Except that braces used for initialization don't
# follow the same rule; we often don't want spaces before those.
match = re.match(r"^(.*[^ ({>]){", line)

if match:
if match := re.match(r"^(.*[^ ({>]){", line):
# Try a bit harder to check for brace initialization. This
# happens in one of the following forms:
# Constructor() : initializer_list_{} { ... }
Expand Down Expand Up @@ -5017,8 +5012,7 @@ def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
# We also check "if" blocks here, since an empty conditional block
# is likely an error.
line = clean_lines.elided[linenum]
matched = re.match(r"\s*(for|while|if)\s*\(", line)
if matched:
if matched := re.match(r"\s*(for|while|if)\s*\(", line):
# Find the end of the conditional expression.
(end_line, end_linenum, end_pos) = CloseExpression(clean_lines, linenum, line.find("("))

Expand Down Expand Up @@ -5911,8 +5905,7 @@ def CheckLanguage(
# convention of the whole function to process multiple line to handle it.
# printf(
# boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
printf_args = _GetTextInside(line, r"(?i)\b(string)?printf\s*\(")
if printf_args:
if printf_args := _GetTextInside(line, r"(?i)\b(string)?printf\s*\("):
match = re.match(r"([\w.\->()]+)$", printf_args)
if match and match.group(1) != "__VA_ARGS__":
function_name = re.search(r"\b((?:string)?printf)\s*\(", line, re.IGNORECASE).group(1)
Expand Down Expand Up @@ -6858,8 +6851,7 @@ def FilesBelongToSameModule(filename_cc, filename_h):
return (False, "")

filename_cc = filename_cc[: -(len(fileinfo_cc.Extension()))]
matched_test_suffix = re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName())
if matched_test_suffix:
if matched_test_suffix := re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName()):
filename_cc = filename_cc[: -len(matched_test_suffix.group(1))]

filename_cc = filename_cc.replace("/public/", "/")
Expand Down Expand Up @@ -7067,8 +7059,7 @@ def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
# the declarator ends and where the virt-specifier starts to avoid
# false positives.
line = clean_lines.elided[linenum]
declarator_end = line.rfind(")")
if declarator_end >= 0:
if (declarator_end := line.rfind(")")) >= 0:
fragment = line[declarator_end:]
else:
if linenum > 1 and clean_lines.elided[linenum - 1].rfind(")") >= 0:
Expand Down
3 changes: 1 addition & 2 deletions cpplint_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5727,8 +5727,7 @@ def testRuntimePrintfFormat(self):

def TestLintLogCodeOnError(self, code, expected_message):
# Special TestLint which logs the input code on error.
result = self.PerformSingleLineLint(code)
assert result == expected_message, (
assert (result := self.PerformSingleLineLint(code)) == expected_message, (
f'For code: "{code}"\nGot: "{result}"\nExpected: "{expected_message}"'
)

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy