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

all: GitHub Action to lint Python code with ruff. #10977

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ruff.yml
@@ -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 .
24 changes: 24 additions & 0 deletions pyproject.toml
Expand Up @@ -22,3 +22,27 @@ skip = """
./tests,\
ACKNOWLEDGEMENTS,\
"""

[tool.ruff]
exclude = ["lib", "tests"]
extend-select = ["C9", "PLC"]
ignore = [
"E401",
"E402",
"E722",
"E731",
"E741",
"F401",
"F403",
"F405",
"F821",
"PLC1901",
]
line-length = 337
Copy link
Contributor

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

Copy link
Contributor Author

@cclauss cclauss May 2, 2023

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 .

[ ... ]
Found 96 errors.

Copy link
Contributor

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

Copy link
Contributor Author

@cclauss cclauss May 2, 2023

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.

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:

# Here's a really long comment. Black doesn't wrap comments at all. It wraps docstrings, but it
# doesn't wrap comments.


x = here_is_a_really_really_really_really_really_really_really_really_really_really_long_function_name(
    1, 2, 3
)

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok clear.

target-version = "py37"

[tool.ruff.mccabe]
max-complexity = 40

[tool.ruff.per-file-ignores]
"ports/cc3200/tools/uniflash.py" = ["E711"]