Skip to content

Commit

Permalink
Add ruff.toml with Python lint configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Aug 22, 2024
1 parent 320b28d commit 4c6968b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
target-version = "py312"

[lint]
select = [
"B0", # bugbear (all B0* checks enabled by default)
"B904", # bugbear (Within an except clause, raise exceptions with raise ... from err)
"B905", # bugbear (zip() without an explicit strict= parameter set.)
"E", # pycodestyles
"W", # pycodestyles
"F", # pyflakes
"I", # isort
"PGH", # pygrep-hooks
"PLC", # pylint conventions
"PLE", # pylint errors
"UP", # pyupgrade
]
ignore = [
"E402", # module import not at top of file
"UP038", # Use X | Y in isinstance check instead of (X, Y)
]

[lint.per-file-ignores]
# Want to preserve compatibility for old Python versions in tools directory so
# disable pyupgrade. Oldest supported version is currently 3.9.
"tools/*" = ["UP"]
12 changes: 11 additions & 1 deletion tools/cross/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def buildifier(files: list[Path], check: bool = False) -> bool:


def ruff(files: list[Path], check: bool = False) -> bool:
if files and not shutil.which(RUFF):
if not files:
return True
if not shutil.which(RUFF):
msg = "Cannot find ruff, will not format Python"
if check:
# In ci, fail.
Expand All @@ -129,10 +131,18 @@ def ruff(files: list[Path], check: bool = False) -> bool:
# formatting they can install ruff and run again.
logging.warning(msg)
return True
# Format
cmd = [RUFF, "format"]
if check:
cmd.append("--diff")
result = subprocess.run(cmd + files)
if result.returncode != 0:
return False
# lint
cmd = [RUFF, "check"]
if not check:
cmd.append("--fix")
result = subprocess.run(cmd + files)
return result.returncode == 0


Expand Down

0 comments on commit 4c6968b

Please sign in to comment.