Use ruff instead of flake8 to check PEP8 by dstansby · Pull Request #29762 · matplotlib/matplotlib · GitHub
Skip to content

Use ruff instead of flake8 to check PEP8 - #29762

Merged
timhoffm merged 2 commits into
matplotlib:mainfrom
dstansby:change-flake8-to-ruff
Apr 9, 2025
Merged

timhoffm merged 2 commits into
matplotlib:mainfrom
dstansby:change-flake8-to-ruff

Conversation

@dstansby

@dstansby dstansby commented Mar 17, 2025

Copy link
Copy Markdown
Member

PR summary

This switches the way we check PEP8 from using flake8 to ruff. There are two main motivations for this:

  1. It avoids having and maintaining duplicate config for both ruff and flake8 in the repository.
  2. Using ruff, PEP8 violations can in many instances be automatically fixed (fixes [ENH]: Add way to automatically fix flake8 errors #27588).

For me 2) is a big developer win - instead of having to manually fix many PEP8 violations, I can just run either ruff check --fix, or pre-commit --all-files to automatically make the required fixes.

The initial commit here raised a PEP8 error, to check if reviewdog is working correctly - it is 🎉 , and the second commit fixes the error.

PR checklist

@github-actions github-actions Bot added the Documentation: devdocs files in doc/devel label Mar 17, 2025
@dstansby
dstansby force-pushed the change-flake8-to-ruff branch 2 times, most recently from 83ec31d to accaf12 Compare March 17, 2025 17:29
@dstansby
dstansby marked this pull request as ready for review March 17, 2025 17:34
@tacaswell tacaswell added this to the v3.11.0 milestone Mar 18, 2025
@tacaswell

Copy link
Copy Markdown
Member

@timhoffm

Copy link
Copy Markdown
Member

I can see the case for leaving the .flake8 file around for a while to not break people's editor workflows.

Fair. We should then add a comment to the file that we don't use it anymore.

@QuLogic
QuLogic requested a review from ksunden March 18, 2025 23:48

@QuLogic QuLogic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should decide on whether to keep .flake8 or not. Also, IIRC, @ksunden added the ruff config and did not make it the primary thing because it didn't support everything yet. I suppose he can confirm whether this is true now or not.

@dstansby

Copy link
Copy Markdown
Member Author

Ruff implements

  • all pydocstyle rules (apart from the deprecated D302 rule)
  • all pyflakes rules
  • all pycodestyle rules, apaprt from E121 to E133, E704, E901, W503, W504.

Of the pycodestyle rules not implemented in ruff, we use (ie don't ignore, and they are in the default pycodestyle ruleset):

  • E122: continuation line missing indentation or outdented
  • E124: closing bracket does not match visual indentation
  • E125: continuation line with same indent as next logical line
  • E128: continuation line under-indented for visual indent
  • E129: visually indented line with same indent as next logical line
  • E901: SyntaxError or IndentationError

I'm not sure what E901 means, but I understand it's a Python syntax error that I'm 90% sure would be caught just by importing a file.

For the rest, I'm not sure how much we care about keeping strictly to these visual indentation rules, since we do not enforce a code formatter (e.g., black) at the moment anyway). My two cents is that it's worth sacrificing these rules for switching to ruff and providing the benefits of being able to autofix issues. Also worth noting that pydocstyle was last updated two years ago, and is now expclitly deprecated in favour of ruff.

On keeping .flake8, I'm 👍 to just 'tearing the plaster off' and removing it - if we put a release notes entry in, hopefully developers can adapt to use ruff fine? But also happy to leave a comment at the top of .flake8 saying it's deprecated and will be removed in 6(?) months, if that's what folks prefer?

@jklymak

jklymak commented Mar 19, 2025

Copy link
Copy Markdown
Member

It seems we should just remove flake8 if we don't use it? Developers who still need it for something could still copy one into their directory. Given that there is no way to deprecate its use, it seems the easiest way to let people know is to remove it.

@dstansby
dstansby requested a review from QuLogic March 21, 2025 19:59
@ksunden

ksunden commented Mar 27, 2025

Copy link
Copy Markdown
Member

The only two rules I saw as "missing" when I last checked was E122 and E302, which is why they are listed as the "external rules" (which prevents warnings for e.g. ignore comments on those rules)

Looking into it, looks like E302 is now implemented, so it really is just E122, which has to do with indentation on continuation lines, which is relatively niche to begin with.

Personally, I'd be good with swapping to ruff only at this point. I had been hoping for 100% parity, but we are so close it's probably good enough.

@dstansby

Copy link
Copy Markdown
Member Author

Consensus seems to be this is ready to go then. The last open question seems to be keeping the .flake8 file or not. I'm +1 just removing it to signal to developers that we're not actively using flake8 any more, but if there are enough voices in the other direction I'm happy to keep it for a bit.

@QuLogic
QuLogic dismissed their stale review April 4, 2025 07:18

Got a response from @ksunden.

@dstansby
dstansby force-pushed the change-flake8-to-ruff branch from a93d55e to d53121d Compare April 9, 2025 19:02
@timhoffm

timhoffm commented Apr 9, 2025

Copy link
Copy Markdown
Member

@timhoffm
timhoffm merged commit f80038c into matplotlib:main Apr 9, 2025
@dstansby
dstansby deleted the change-flake8-to-ruff branch April 10, 2025 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: Add way to automatically fix flake8 errors

6 participants