Skip to content

Replace Flake8 with Ruff? #11534

Closed
Closed

Description

@cbrnr

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?

Activity

  1. agramfort commented on Mar 7, 2023

    @agramfort
    SponsorMember

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

  2. cbrnr commented on Mar 7, 2023

    @cbrnr
    ContributorAuthor

    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.

  3. cbrnr commented on Mar 7, 2023

    @cbrnr
    ContributorAuthor

    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.

  4. agramfort commented on Mar 7, 2023

    @agramfort
    SponsorMember

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

  5. cbrnr commented on Mar 7, 2023

    @cbrnr
    ContributorAuthor

    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".

  6. larsoner commented on Mar 7, 2023

    @larsoner
    Member

    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

  7. drammock commented on Mar 7, 2023

    @drammock
    Member
  8. drammock commented on Mar 7, 2023

    @drammock
    Member

    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

  9. cbrnr commented on Mar 8, 2023

    @cbrnr
    ContributorAuthor

    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?

  10. cbrnr commented on Mar 8, 2023

    @cbrnr
    ContributorAuthor

    See #11541.

  11. linked a pull request that will close this issueAdd pre-commit #11541on Mar 8, 2023
  12. larsoner commented on Mar 8, 2023

    @larsoner
    Member

    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...

  13. added this to the 1.4 milestone on Mar 8, 2023
  14. cbrnr commented on Mar 8, 2023

    @cbrnr
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions