-
Notifications
You must be signed in to change notification settings - Fork 347
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
Add Python formatting with Ruff #2573
Conversation
ef9bd17
to
46a9b4d
Compare
I don't see a ruff config file in the PR. Shouldn't we add your config? |
Make sure to run the formatter and commit the changed files |
Well I want to see the CI fail first. |
I think the configuration I suggested before is all configuring the lints for |
46a9b4d
to
e63c83f
Compare
I think I'll add ruff linting in a separate PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to add the relevant commit to .git-blame-ignore-revs
@@ -13,6 +13,9 @@ jobs: | |||
- uses: actions/checkout@v4 | |||
with: | |||
show-progress: false | |||
- uses: actions/setup-python@v5 | |||
with: | |||
python-version: 3.12 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a .python-version
file in the root of this project, so we don't have to use python-version: CONSTANT
in everywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I only see 3.12
repeated in two places, so I'm not sure this would be helpful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW the Ubuntu 24.04 dev image defaults to python 3.12, so this is currently a no-op (https://github.com/actions/runner-images/blob/main/images/ubuntu/Ubuntu2404-Readme.md#language-and-runtime). There's a case for keeping it though to use a fixed python version even if we update the build image, or if we want to update to python 3.13 early (comes out in 6 weeks).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also an indicator for IDEs to automatically use a specific version of Python whenever the interpreter is not configured. I wish this change was included in this pull-request.
@@ -27,6 +30,9 @@ jobs: | |||
- name: Install project deps with pnpm | |||
run: | | |||
pnpm i | |||
- name: Install Ruff | |||
run: | | |||
pip install ruff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use ruff-action? https://github.com/ChartBoost/ruff-action
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well with the current approach it works locally for people to do python ./tools/cross/format.py
if they have ruff installed, whereas if we used the action there wouldn't be a good local approach. I think @danlapid said he'd prefer to modify format.py
over using ci-specific automation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ruff-action
is used to install ruff. it is not used to execute ruff. right now, we will always use the latest ruff version.
# formatting they can install ruff and run again. | ||
logging.warning(msg) | ||
return True | ||
cmd = [RUFF, "format"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have any kind of "verbose" flag for ruff so we see what files it formats or checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I was just about to open a pull-request to remove it, since it reduces formatting performance. Any particular reason to enable it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it convenient for local use to know which files were checked, and if they needed their formatting corrected or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @npaun also, formatting shouldn't take too long for regular runs where a user just has a few files staged for commits.
The CI run might take a while but it's gonna be faster than build&test anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anonrig What kind of time difference did you see between with verbose vs without?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a --verbose
flag but it is so noisy that you would only use it if you are debugging ruff. I think the standard verbosity is reasonable. Seems like we want to use --diff
and not --check
though:
--check
Avoid writing any formatted files back; instead, exit with a non-zero status code if any files would have been modified, and zero otherwise
--diff
Avoid writing any formatted files back; instead, exit with a non-zero status code and the difference between the current file and how the formatted file would look like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea sounds right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find it convenient for local use to know which files were checked, and if they needed their formatting corrected or not.
Unfortunately I don't think that ruff has a mode that does this without printing way too much output.
I don't have any opinion on the following matter but I think it should be brought up and discussed: |
PEP 8 recommends 4 spaces for Python, so that's my personal preference |
Yeah the vast majority of Python code in the wild uses 4 spaces. |
e56427d
to
6c32afb
Compare
Commenting just in case: Make sure to merge this PR via "Merge" and not "Squash". Otherwise, .git-blame-ignore-revs will not work properly. |
I wish it were possible to disable squash merge ahead of time on a particular PR, I have a tendency to accidentally squash merge patch release PRs and things like that. |
Yeah, same here. |
No description provided.