Skip to content
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

Merged
merged 3 commits into from
Aug 21, 2024
Merged

Add Python formatting with Ruff #2573

merged 3 commits into from
Aug 21, 2024

Conversation

hoodmane
Copy link
Contributor

No description provided.

tools/cross/format.py Outdated Show resolved Hide resolved
@npaun
Copy link
Member

npaun commented Aug 21, 2024

I don't see a ruff config file in the PR. Shouldn't we add your config?

@danlapid
Copy link
Contributor

Make sure to run the formatter and commit the changed files

@hoodmane
Copy link
Contributor Author

Well I want to see the CI fail first.

@npaun npaun linked an issue Aug 21, 2024 that may be closed by this pull request
@hoodmane
Copy link
Contributor Author

Shouldn't we add your config?

I think the configuration I suggested before is all configuring the lints for ruff check, for ruff format I think we can just use the defaults and it will work fine. I looked through the list of ruff format configuration options and none of them look that interesting to me.

@hoodmane
Copy link
Contributor Author

I think I'll add ruff linting in a separate PR.

Copy link
Member

@npaun npaun left a 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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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).

Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copy link
Member

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"]
Copy link
Contributor

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?

Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened a pull-request about 2 changes that I think will fulfill everybody's needs. #2576

@anonrig What kind of time difference did you see between with verbose vs without?

I'm not sure. I have to benchmark it using a tool like hyperfine to came up with a scientific result.

Copy link
Contributor Author

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea sounds right.

Copy link
Contributor Author

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.

@danlapid
Copy link
Contributor

I don't have any opinion on the following matter but I think it should be brought up and discussed:
In c++ code we use indentation size of 2 spaces.
Should we be consistent with python intention size as well?

@npaun
Copy link
Member

npaun commented Aug 21, 2024

PEP 8 recommends 4 spaces for Python, so that's my personal preference

@hoodmane
Copy link
Contributor Author

Yeah the vast majority of Python code in the wild uses 4 spaces.

@npaun
Copy link
Member

npaun commented Aug 21, 2024

Commenting just in case: Make sure to merge this PR via "Merge" and not "Squash". Otherwise, .git-blame-ignore-revs will not work properly.

@hoodmane hoodmane merged commit a95796b into main Aug 21, 2024
9 of 10 checks passed
@hoodmane hoodmane deleted the hoodmane/python-format branch August 21, 2024 17:04
@hoodmane
Copy link
Contributor Author

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.

@npaun
Copy link
Member

npaun commented Aug 21, 2024

Yeah, same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code formatting: Python
5 participants