-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
all: GitHub Action to lint Python code with ruff. #10977
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python | ||
name: Python code lint with ruff | ||
on: [push, pull_request] | ||
jobs: | ||
ruff: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: pip install --user ruff | ||
- run: ruff --format=github . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't this match black's line length (99) ?
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.
The 99 is aspirational whereas the 337 is actual.
psf/black is unwilling to wrap all lines (comments? docstrings? strings?) so those lines need to be wrapped manually.
Ideally, both should be 99 once some manual work is done.
%
ruff --select=E501 --line-length=99 --show-source .
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.
Hmm then why ruff says "Ruff is compatible with Black out-of-the-box, as long as the line-length setting is consistent between the two." Can you point to an example where ruff complains? There's no way to make it ignore comments for instance (should that be the culprit)?
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.
@charliermarsh, @dhruvmanila This question has come up a few times in my travels...
Do you have a better explanation? Should I create a PR to the ruff docs to improve the explanation there?
I am OK with the behavior of the two tools as they are because I believe that the goals of a formatter are different than the goals of a linter.
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.
Hmm, yeah, I'd explain it as: Black makes a best-effort attempt to wrap lines under the specified line length, but doesn't guarantee that all lines will be under the specified line length. But Ruff's rule doesn't discriminate, it just flags all lines over the limit.
You're right that in this particular case, we do arguably "disagree" with Black, depending on how you look at it. Projects with Black enabled could choose to disable this rule entirely if they're happy with Black's line-length handling, though in my personal opinion it still makes sense to enable it. A few examples of code that Black won't wrap, but Ruff will flag:
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.
Thanks @charliermarsh for the explanation.
Sounds like we need to keep this at 337, for now. Maybe in the future we can tidy up those scripts that go over the 99 length Black limit, so this number here can be reduced.
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.
Ok clear.