Skip to content

MAINT: Use black #10641

Closed
Closed

Description

@larsoner

black seems to be where the community is going -- NumPy is planning on using it I think (?), and from a quick look it looks like pandas and scikit-learn already do or are headed in that direction. I'm sure @agramfort has opinions/experience to share here :)

It's possible to make a single big commit that makes all the changes, then add this to a git blame ignore file, which removes the potential blame pollution which (to me) was a big barrier to adoption.

So overall +1 for moving to black from me using whatever pre-commit hooks or whatever pandas and scikit-learn have done (despite me actually not particularly liking how black code ends up looking sometimes!).

I can volunteer to help people rebase or merge PRs that are broken by the changes if they need help.

cc @cbrnr who mentioned wanting longer than 79 char line length, this would be one way to do it.

EDIT:

Activity

  1. welcome commented on May 17, 2022

    @welcome

    Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️

  2. cbrnr commented on May 17, 2022

    @cbrnr
    Contributor

    Thanks for opening your first issue here @larsoner 😆!

    I'm reluctant to go all Black. Of course this alleviates us from all style-related issues, but on the other hand we sometimes want to use custom formatting to improve readability. This is just not possible with Black.

    My vote is to just allow a line length of 92 characters for new code, so no changes are necessary for existing code. Of course, people are then free to rewrap a whole file as part of some other change.

  3. agramfort commented on May 17, 2022

    @agramfort
    SponsorMember
  4. cbrnr commented on May 17, 2022

    @cbrnr
    Contributor

    you're so European minded Clemens :)

    I hope this is a compliment 😄?

    If you really want all or nothing, I'm going to vote against Black.

    Can we maybe still do a quick poll just to see what others think?

    1. 😄 Leave everything as is (79 chars hard limit with # noqa as a last resort)
    2. 🎉 Use Black
    3. 🚀 Just increase our maximum line length to 92 characters
  5. larsoner commented on May 17, 2022

    @larsoner
    MemberAuthor

    +0 on 1
    +1 on 2
    -1 on 3

  6. agramfort commented on May 17, 2022

    @agramfort
    SponsorMember
  7. mmagnuski commented on May 18, 2022

    @mmagnuski
    Member

    I'm not familiar with Black but I have a vague recollection that I didn't like some of the formatting rules it applies. What would be the benefit of switching to Black (apart from the fact that it seems to be a trend now)?
    Currently I'm +1 on staying how it is.

  8. cbrnr commented on May 18, 2022

    @cbrnr
    Contributor

    @mmagnuski the benefit is that Black auto-formats everything. The issue is that Black auto-formats everything. So there is no in between if for some reason (readability) you would like to have some custom formatting.

  9. agramfort commented on May 18, 2022

    @agramfort
    SponsorMember
  10. cbrnr commented on May 18, 2022

    @cbrnr
    Contributor

    Black is not a standard, it's an opinionated formatter. Line lengths can be set even in Black (https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-length), so in my opinion we could set only a new line length (to 92 or 88). I'm happy with our style, it's just the line lengths that bother me.

  11. mmagnuski commented on May 18, 2022

    @mmagnuski
    Member

    @cbrnr @agramfort
    I'm also happy with our style (much more than what Black does). And I'm happy with current line limit - @cbrnr when is it an issue?

  12. cbrnr commented on May 18, 2022

    @cbrnr
    Contributor

    when is it an issue?

    Everytime. It's just not enough characters, I almost always have to break it up into multiple lines.

    Most recent example: https://github.com/mne-tools/mne-python/pull/10638/files

    With 92 characters, the line could have stayed one line:

    ch_data = many_chunk[:, ch_offsets[ci]:ch_offsets[ci + 1]].copy()

    Now it's this:

    ch_data = many_chunk[:,
                         ch_offsets[ci]:ch_offsets[ci + 1]].copy()
  13. mmagnuski commented on May 18, 2022

    @mmagnuski
    Member

    I'm neutral on changing the line limit - I rarely encounter such edge cases. And I'd be fine with:

    ch_data = (
        many_chunk[:, ch_offsets[ci]:ch_offsets[ci + 1]].copy())

    in this particular case. :)
    But I understand it can be a bit frustrating at times, when you are forced to perform line-length exercises.

  14. sappelhoff commented on May 18, 2022

    @sappelhoff
    Member

    I am in favor of black. I have adopted it for most of my projects and the few times that I have run into issues are heavily outweighed by the consistency, readability, and "ease of reading across projects" that black provides me with.

    The issues that i have run into are furthermore typically minor: For example "I want this list to be in one line!" and then black puts it into multiple lines. I can really live with that.

    I also use black together with isort to properly sort imports and with flake8 + flake8-docstrings to enforce a reasonable docstring formatting (numpy format).

  15. 6 remaining items

  16. cbrnr commented on May 19, 2022

    @cbrnr
    Contributor

    an opinionated formatter that tons and tons of people use

    I still think there are more people not using it.

    and that's also officially hosted on the org of the Python Software Foundation (https://github.com/psf/black) 😉

    That's not a good argument, especially not for MNE-Python. We recommend conda instead of pip, which would be the official tool hosted at PyPA (OK, it's not PSF, but it's basically the same thing).

    do you think these situations outweigh the situations where our custom formatting currently makes readability worse? (especially "cross project readability")

    I have not used Black extensively, so my experience is limited. I'm using a style which is already pretty close to Black, so maybe y'all are right and we should do it. It's probably really a lot better than our current style.

  17. mmagnuski commented on May 19, 2022

    @mmagnuski
    Member

    I looked more into what Black does and I think I'm fine with it. Things I don't like so much I can get used to. I'll test the vscode extension and will likely use it locally.

  18. agramfort commented on May 20, 2022

    @agramfort
    SponsorMember

    we discussed during the MNE office hours.

    I agreed that switching black is a way forward, although sticking with default black config.

    please put your 👍 below this comment if you are on board

  19. mmagnuski commented on May 20, 2022

    @mmagnuski
    Member

    Ah, I keep forgetting about the MNE hours. I will try to join you next time!

  20. drammock commented on May 21, 2022

    @drammock
    Member

    Is there something like pre-commit that will only run when you push instead of when you commit?

    To answer my own question, apparently you can put any executable file into .git/hooks/pre-push so presumably we could set it up so that black runs before pushes, and aborts the push if the check fails.

  21. drammock commented on Jun 15, 2022

    @drammock
    Member

    just a note that after we do this, we should also add a .git-blame-ignore-revs file with the commit hash of the black-and-isort commits. See here for explanation:

    https://akrabat.com/ignoring-revisions-with-git-blame/

    There may be a few commits in the history we would want to add to that file as well? Not sure, sometimes big moves end up squashed in along with real code changes.

  22. larsoner commented on Jun 15, 2022

    @larsoner
    MemberAuthor

    .git-blame-ignore-revs file

    For a template to follow we can look at numpy/numpydoc#394 which IIRC was done following what NumPy did

    Not sure, sometimes big moves end up squashed in along with real code changes.

    Yes this will be true for the last 2-3 years. But there are probably some before that that we could include, though

    I'm thinking this could be an early PR in the 1.2 cycle

  23. added this to the 1.4 milestone on Mar 8, 2023
  24. cbrnr commented on Mar 9, 2023

    @cbrnr
    Contributor

    Does anyone have time in the near future to tackle the switch to Black formatting? Once this is implemented, I can enable Black as a pre-commit hook in #11541.

  25. larsoner commented on Mar 9, 2023

    @larsoner
    MemberAuthor

    It would be nice to get #11152 in first if possible since it's big. Maybe #11064, too, but since I'm authoring that I'm not as worried about the rebase/merge. But I have this marked for 1.4 so it should hopefully get done within the next month.

  26. cbrnr commented on Mar 9, 2023

    @cbrnr
    Contributor

    Could/should we start using Black style in PRs even before the big one is merged? It wouldn't hurt, right?

  27. larsoner commented on Mar 9, 2023

    @larsoner
    MemberAuthor

    Yes people can start doing that if they want

  28. agramfort commented on Mar 12, 2023

    @agramfort
    SponsorMember
  29. cbrnr commented on Mar 12, 2023

    @cbrnr
    Contributor

    I only meant in new code. But it doesn't really work anyway because we're changing our line length from 79 to 88.

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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions