Use ruff to format and lint Python code#8684
Conversation
mcourteaux
left a comment
There was a problem hiding this comment.
In general the changes look good, except for a couple places I spotted. I never used ruff, so not sure if they can be fixed with formatting rules. Line length limit for the C++ part is nice: no strict limit, you can author it manually: either insert the newline, and clang-format will keep it, or delete it and clang-format will not insert one. Can ruff do this too?
One other strange thing I noticed is that a few single-letter variables became two letters, like l became lv. Is that a ruff automatic change?
| color_image[x, y, c] = hl.select(c == 0, 245, # Red value | ||
| c == 1, 42, # Green value | ||
| 132) # Blue value | ||
| color_image[x, y, c] = hl.select( | ||
| c == 0, | ||
| 245, # Red value | ||
| c == 1, | ||
| 42, # Green value | ||
| 132, # Blue value | ||
| ) |
There was a problem hiding this comment.
This is unfortunate. # fmt: off and # fmt: on would be the equivalent for ruff.
There was a problem hiding this comment.
It is... I'd rather see this, but it requires a deeper code change:
color_image[x, y, c] = hl.select(
(c == 0, 245), # Red value
(c == 1, 42), # Green value
132, # Blue value
)
| assert "select() on Tuples requires all Tuples to have identical sizes." in str(e) | ||
| assert "select() on Tuples requires all Tuples to have identical sizes." in str( | ||
| e | ||
| ) |
There was a problem hiding this comment.
This is bad. Line too long?
There was a problem hiding this comment.
Yeah, the line is too long here. I think the built-in limit is 88.
| debugger.HandleCommand("type summary add Halide::RVar --python-function lldbhalide.call_name") | ||
| debugger.HandleCommand("type summary add Halide::Var --python-function lldbhalide.call_name") | ||
| debugger.HandleCommand( | ||
| "type summary add Halide::RVar --python-function lldbhalide.call_name" | ||
| ) | ||
| debugger.HandleCommand( | ||
| "type summary add Halide::Var --python-function lldbhalide.call_name" | ||
| ) |
There was a problem hiding this comment.
Not nice. Line length setting might be too strict?
There was a problem hiding this comment.
These I can live with, but maybe the solution is to increase the width to 120
Ruff doesn't support having no limit and it will try to pack things onto one line if the limit is made very large. The default is 88. We can see what happens at, say, 96 or 120. https://docs.astral.sh/ruff/settings/#line-length
No, that's not automatic (thankfully). It doesn't like lowercase |
steven-johnson
left a comment
There was a problem hiding this comment.
Yeah, overall I definitely like this more than the status quo
|
I don't know where these C++ errors are coming from (LLVM 20 injection?) as no C++ files are modified here. |
|
LLVM 20 failure is unrelated |
Ruff is a formatter and linter for Python. This PR shows what would happen to our codebase if we were to adopt it. I believe this is a useful tool, and it does not mangle our formatting too much. There are a few stylistic pain points:
selectexpression looks awkward when all its arguments get their own lines. We could adjust the API to allow taking(cond, val)tuples, which would at least pair these up.Using the linter produced a few classes of changes:
python_correctness_externas skipped until we implement it.GitHub Actions has been set to enforce these rules.
To run locally in a standard venv, you can install ruff via
pip:If you have
uvinstalled, prefer to useuvx ruff checkanduvx ruff format(no need to pip install).