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

GitHub Action to lint Python code with ruff #1771

Merged
merged 4 commits into from Mar 26, 2023
Merged

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Mar 9, 2023

Ruff supports over 500 lint rules and can be used to replace Flake8 (plus dozens of plugins), isort, pydocstyle, yesqa, eradicate, pyupgrade, and autoflake, all while executing (in Rust) tens or hundreds of times faster than any individual tool.

The ruff Action uses minimal steps to run in ~10 seconds, rapidly providing intuitive GitHub Annotations to contributors.

image

Describe the changes

The related issue or a description of the bug or feature that this PR addresses.

Tasks

  • Unit tests added that reproduce the issue or prove feature is working
  • Fix or feature added
  • Code typed and mypy shows no errors
  • Documentation updated (if relevant)
    • No warnings during build
    • Preview link (CircleCI -> Artifacts -> doc/_build/html/index.html)
  • Unit tests passing and overall coverage the same or better

@cclauss cclauss marked this pull request as draft March 9, 2023 23:01
@cclauss cclauss marked this pull request as ready for review March 9, 2023 23:14
@codecov
Copy link

codecov bot commented Mar 10, 2023

Codecov Report

Merging #1771 (8a4c4d1) into master (2709ff6) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1771      +/-   ##
==========================================
+ Coverage   97.41%   97.43%   +0.01%     
==========================================
  Files          66       66              
  Lines       10839    10839              
==========================================
+ Hits        10559    10561       +2     
+ Misses        280      278       -2     
Impacted Files Coverage Δ
pydicom/sr/_snomed_dict.py 100.00% <ø> (ø)
pydicom/util/leanread.py 88.63% <100.00%> (ø)

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@darcymason
Copy link
Member

I want be able to take a good look at this for a couple of days, probably. But a quick question - can you elaborate a bit on what this does better or differently than what we have currently? You mentioned speed and the intuitive annotations, but does this report on new/different categories of linting problems than our existing setup?

@cclauss
Copy link
Contributor Author

cclauss commented Mar 10, 2023

I believe that the front page of https://beta.ruff.rs explains the advantages quite succinctly. If we did ruff --select=ALL . then yes, it would report on many new/different categories of lining problems than your existing setup.

@darcymason
Copy link
Member

I believe that the front page of https://beta.ruff.rs explains the advantages quite succinctly. If we did ruff --select=ALL . then yes, it would report on many new/different categories of lining problems than your existing setup.

Okay, thanks for those links. I'll review them when I get a chance.

- uses: actions/checkout@v3
- run: pip install --user ruff
- run: ruff --ignore=E402,F401,F403,F405,F541,F601,F811,F841
--format=github --line-length=214 --target-version=py37 .
Copy link
Member

Choose a reason for hiding this comment

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

As far as I understand, this replaces flake8, so it probably makes sense to remove the flake8 check if this will be used.

I wonder about that line length of 214 requirement. We also use black, which uses 88 as the maximum line length, which ruff also seems to use, so why this setting? In case it doesn't understand the noqa comment, it would be better to switch off the check for line length, as it is already done by black anyway.

Also, why all the exclusions? Apart from E402 (module-import-not-at-top-of-file) I see no reason for the rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I don't understand what you mean.
I can see that this makefile is quite outdated and should probably be adapted or removed, though :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's leverage --format=github to show where the errors are.

Copy link
Contributor Author

@cclauss cclauss Mar 12, 2023

Choose a reason for hiding this comment

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

Please review the first ten errors in the code listing... https://github.com/pydicom/pydicom/pull/1771/files

Let's agree that line-length is currently different under psf/black (best effort formatter) and ruff (rules-based linter) but our goal is to make them the same.

  • The difference because the former is guidance for a tool that formats code but not comments, docstrings, etc., and is deliberately timid about reformatting strings while the latter can be used as a trigger for one or more good-first-issues for interested contributors to shorten long lines that psf/black does not autofix.

Copy link
Member

Choose a reason for hiding this comment

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

About the first ten errors: I think these are all things that can be either easily fixed, or these lines should get an exclusion comment. I just think it makes more sense to fix them instead of disabling the warnings.
As for line-length: as far as I could see, these are from generated code. It would make sense just to exclude these files from the check by a file-wide exclusion comment, if that is possible. The same is true for unused imports in __init__.py.

Anyway, I'm not saying that you have to do all of it now - that is something that can also be handled later by somebody else. In this case I would like to at least remove the redundant flake8 check (it is redundant, right?), and write a new issue for enabling and fixing these warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pyproject.toml added with appropriate settings and #1776 add to cover follow-on tasks.

@cclauss cclauss force-pushed the patch-1 branch 2 times, most recently from b1e8dbf to 4912ef5 Compare March 24, 2023 13:23
@cclauss cclauss mentioned this pull request Mar 24, 2023
8 tasks
[Ruff](https://beta.ruff.rs/) supports [over 500 lint rules](https://beta.ruff.rs/docs/rules) including bandit, isort, pylint, pyupgrade, and flake8 plus its plugins and is written in Rust for speed.

The `ruff` Action uses minimal steps to run in ~10 seconds, rapidly providing intuitive GitHub Annotations to contributors.

![image](https://user-images.githubusercontent.com/3709715/223758136-afc386d2-70aa-4eff-953a-2c2d82ceea23.png)
steps:
- uses: actions/checkout@v3
- run: pip install --user ruff
- run: ruff --format=github .
Copy link
Member

Choose a reason for hiding this comment

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

You left in the flake8 workflow - do you think it is still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is wired into https://github.com/reviewdog/action-flake8 and I cannot tell if you need reviewdog.

Copy link
Member

Choose a reason for hiding this comment

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

Well, reviewdoc does the same as ruff, e.g. create annotation in the code, so if ruff is better suited, we don't need it anymore. I see that you have removed it already.
I may have a look at the differences between the two later, but your changes look good to me so far.
Will wait for @darcymason for his opinion on the change.

Copy link
Member

Choose a reason for hiding this comment

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

Hi all, I have limited time right now, but I'll try to take a better look later today.

Copy link
Member

Choose a reason for hiding this comment

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

@mrbean-bremen, I wasn't sure if there was anything else you wanted to review, but I'll merge in and we can adapt in other PR(s) as needed...

Copy link
Member

Choose a reason for hiding this comment

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

That's ok, I'm happy with that. There is still #1776 for improvements.

Copy link
Member

@darcymason darcymason left a comment

Choose a reason for hiding this comment

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

Okay, had a pretty good look through. Thanks for this - it does make all this much leaner and looks easier to fine-tune as well.

@darcymason darcymason merged commit f7fdf0f into pydicom:master Mar 26, 2023
25 checks passed
@cclauss cclauss deleted the patch-1 branch March 26, 2023 13:13
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.

None yet

3 participants