Skip to content

Commit

Permalink
rfe: Introduce ruff-pre-commit
Browse files Browse the repository at this point in the history
Ruff is a fast, extensible Python linter designed to catch common issues
in Python code, such as syntax errors, stylistic inconsistencies, and
potential bugs.

Signed-off-by: Yihuang Yu <[email protected]>
  • Loading branch information
PaulYuuu committed Aug 22, 2024
1 parent 78b80d0 commit d44041f
Show file tree
Hide file tree
Showing 1,053 changed files with 34,282 additions and 30,095 deletions.
32 changes: 16 additions & 16 deletions .ci/cfg-lint-check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@


def cfg_lint_check():
print('Running cfg lint check...')
print("Running cfg lint check...")
exit_code = 0
for file in sys.argv[1:]:
status_code = 0
blank_line = 0
cfg_path = os.path.abspath(os.path.join(os.path.dirname(__file__),
os.pardir, file))
with open(cfg_path, 'r') as f:
cfg_path = os.path.abspath(
os.path.join(os.path.dirname(__file__), os.pardir, file)
)
with open(cfg_path, "r") as f:
contents = f.read()
for num, line in enumerate(contents.splitlines(), 1):
# Only strip whitespaces, handle other blank characters below
stripped_line = line.lstrip(' ')
blank_line = (blank_line + 1
if re.search(r'^\s*$', stripped_line) else 0)
stripped_line = line.lstrip(" ")
blank_line = blank_line + 1 if re.search(r"^\s*$", stripped_line) else 0
if blank_line >= 2:
print(f'{file}:{num}: Too many blank lines')
print(f"{file}:{num}: Too many blank lines")
status_code = 1
if re.search(r'\s$', line):
print(f'{file}:{num}: Trailing whitespaces')
if re.search(r"\s$", line):
print(f"{file}:{num}: Trailing whitespaces")
status_code = 1
if re.search(r'^\s', stripped_line):
print(f'{file}:{num}: Wrong indent(Unexpected blank characters')
if re.search(r"^\s", stripped_line):
print(f"{file}:{num}: Wrong indent(Unexpected blank characters")
status_code = 1
if (len(line) - len(stripped_line)) % 4:
print(f'{file}:{num}: Wrong indent(4x spaces mismatch)')
print(f"{file}:{num}: Wrong indent(4x spaces mismatch)")
status_code = 1
if not contents.endswith('\n'):
print(f'{file} Missing final newline')
if not contents.endswith("\n"):
print(f"{file} Missing final newline")
status_code = 1
exit_code = exit_code or status_code
sys.exit(exit_code)


if __name__ == '__main__':
if __name__ == "__main__":
cfg_lint_check()
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ repos:
args: ["--fix=lf"]
- id: no-commit-to-branch
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.1
hooks:
- id: ruff
args: ["--fix"]
- id: ruff-format
38 changes: 38 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Same as Black.
line-length = 88
indent-width = 4

[lint]
select = [
# pycodestyle
"E",
"W",
# Pyflakes
"F",
# flake8-logging-format
"G",
# pylint Error
"PLE",
# pyupgrade
"UP",
# isort
"I",
]
ignore = ["E402", "E501", "E722", "E741", "UP015"]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

[format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"
Loading

0 comments on commit d44041f

Please sign in to comment.