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

Replace Flake8 with Ruff? #11534

Closed
cbrnr opened this issue Mar 7, 2023 · 12 comments · Fixed by #11541
Closed

Replace Flake8 with Ruff? #11534

cbrnr opened this issue Mar 7, 2023 · 12 comments · Fixed by #11541
Milestone

Comments

@cbrnr
Copy link
Contributor

cbrnr commented Mar 7, 2023

I recently discovered Ruff, a replacement for Flake8 written in Rust (but available on PyPI), which is 10x–100x faster. Do you think we can/should use it in our CI?

@agramfort
Copy link
Member

have you tried to run it locally on mne? does it lead to many different errors than flake8?

@cbrnr
Copy link
Contributor Author

cbrnr commented Mar 7, 2023

Yes, I get almost 1000 errors, most of them (>99%) F401 (imported but unused). I tried passing the same config that we use, but I'm not sure if that worked (probably not, because we ignore __init__.py, where these F401 occur). I think it should be doable, but we should also investigate if Ruff supports all the checks that we currently use.

@cbrnr
Copy link
Contributor Author

cbrnr commented Mar 7, 2023

I managed to provide our config, and now the errors are down to 7:

doc/conf.py:225:5: F601 [*] Dictionary key literal `'SourceMorph'` repeated
doc/conf.py:227:29: F601 [*] Dictionary key literal `'Forward'` repeated
mne/beamformer/tests/test_resolution_matrix.py:89:13: E741 Ambiguous variable name: `l`
mne/chpi.py:1217:28: E741 Ambiguous variable name: `l`
mne/io/base.py:579:34: E741 Ambiguous variable name: `l`
mne/viz/evoked.py:914:12: E713 [*] Test for membership should be `not in`
mne/viz/topo.py:661:12: E713 [*] Test for membership should be `not in`
Found 7 errors.
[*] 4 potentially fixable with the --fix option.

I still don't know how many rules that we use are not checked.

@agramfort
Copy link
Member

ok it seems it's a doable transition. Feel free to give it a try in a PR

@cbrnr
Copy link
Contributor Author

cbrnr commented Mar 7, 2023

Currently, Ruff does not include PEP8 style checks, but these will be available very soon.

As a quick reminder to the person implementing the PR (probably me), we run Flake8 in codespell_and_flake.yml, and our Flake8 settings are in setup.cfg:

[flake8]
exclude = __init__.py,constants.py,fixes.py,resources.py,*doc/auto_*,*doc/_build*,build/*
ignore = W503,W504,I100,I101,I201,N806,E201,E202,E221,E222,E241
# We add A for the array-spacing plugin, and ignore the E ones it covers above
select = A,E,F,W,C
# 10_spectrum_class.py has a wide rST table
per-file-ignores =
    tutorials/time-freq/10_spectrum_class.py:E501

[pydocstyle]
convention = pep257
match_dir = ^(?!\.|doc|tutorials|examples|logo|icons).*$
match = (?!tests/__init__\.py|fixes).*\.py
add-ignore = D100,D104,D107,D413
add-select = D214,D215,D404,D405,D406,D407,D408,D409,D410,D411
ignore-decorators = ^(copy_.*_doc_to_|on_trait_change|cached_property|deprecated|property|.*setter).*

This has to be translated to Ruff. Is it possible to get a list of rules that Flake8 checks with our config? We are only explicitly stating ignores, but I'd like to get a list of "not ignores".

@larsoner
Copy link
Member

larsoner commented Mar 7, 2023

Is it possible to get a list of rules that Flake8 checks with our config?

Don't know offhand, IIRC somewhere in the flake8 docs it say what the defaults are

@drammock
Copy link
Member

drammock commented Mar 7, 2023

crossref to mne-tools/mne-bids-pipeline#686 and mne-tools/mne-bids-pipeline#688 in case it's helpful

@drammock
Copy link
Member

drammock commented Mar 7, 2023

I'm especially keen on adding Ruff as a pre-commit hook, so that style errors (once they're supported) are caught before pushing to the remote

@cbrnr
Copy link
Contributor Author

cbrnr commented Mar 8, 2023

Thanks for the crossrefs, I didn't know that you had already discussed this! I'm also a fan of precommit, and because we're already using black we don't even have to wait for Ruff to support style checks. Black, Ruff, and Codespell should be all we need, right?

@cbrnr
Copy link
Contributor Author

cbrnr commented Mar 8, 2023

See #11541.

@cbrnr cbrnr linked a pull request Mar 8, 2023 that will close this issue
5 tasks
@larsoner
Copy link
Member

larsoner commented Mar 8, 2023

I didn't know that you had already discussed this! I'm also a fan of precommit, and because we're already using black we don't even have to wait for Ruff to support style checks. Black, Ruff, and Codespell should be all we need, right?

Keep in mind those links are to mne-bids-pipeline where we have black+pre-commit IIRC. MNE-Python itself does not. We plan to go to black, but haven't done it yet: #10641

It would be good to use black for 1.4, maybe now is the time...

@larsoner larsoner added this to the 1.4 milestone Mar 8, 2023
@cbrnr
Copy link
Contributor Author

cbrnr commented Mar 8, 2023

Yes, in #11541 only Codespell and Ruff are currently working, and I can only enable Black once someone has gone over the entire codebase.

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 a pull request may close this issue.

4 participants