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

[INFRA] Initial formatting with black and precommit #3484

Merged
merged 30 commits into from
Feb 7, 2023

Conversation

Remi-Gau
Copy link
Collaborator

@Remi-Gau Remi-Gau commented Feb 3, 2023

Relates to #2528

Changes proposed in this pull request:

  • Set up pre-commit hooks to work only on the nilearn/decomposition module
    • Black
    • Flake8
    • Codespell
    • Several "classical" hooks from pre-commit (json, yml parsing, merge conflicts...)
    • pyupgrade to update python syntax to use versions of the language >= 3.6
  • Apply the hooks on nilearn/decomposition using pre-commit run -a
  • Adapt Flake8 config in setup.cfg to play nice with black
  • Add a pyproject.toml to host the config for black.
  • Add a flake8 CI workflow to run on entire python files, only for this module
  • Remove flake8 workflow that worked just on file diff
  • update contributing to mention the install and usage of black and pre-commit

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2023

👋 @Remi-Gau Thanks for creating a PR!

Until this PR is ready for review, you can include the [WIP] tag in its title, or leave it as a github draft.

Please make sure it is compliant with our contributing guidelines. In particular, be sure it checks the boxes listed below.

  • PR has an interpretable title.
  • PR links to Github issue with mention "Closes #XXXX"
  • Code is PEP8-compliant.
  • Changelog or what's new entry in "doc/changes/latest.rst"
  • (Bug fixes) There is at least one test that would fail under the original bug conditions.
  • (New features) There is at least one unit test per new function / class.
  • (New features) The new feature is demoed in at least one relevant example.

We will review it as quick as possible, feel free to ping us with questions if needed.

@Remi-Gau Remi-Gau changed the title [INFRA] Formatting with black and precommit [INFRA] Initial formatting with black and precommit Feb 3, 2023
setup.cfg Outdated
@@ -72,6 +72,7 @@ dev =
# W503: line break before binary operator
# W504: line break after binary operator
ignore=E402, W503, W504, W605
max-line-length = 91
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I could bring this number down but if I do Flake8 would complain for some files in this PR.

Other possibility I could try is setting the config for black for shorter lines.

Copy link
Member

Choose a reason for hiding this comment

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

Well, I'm just curious about where these numbers come from... Ten we simply need to decide collectively what we think is best for readability.
Actually, I'm a bit surprised to see how these things fluctuate across years ;-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For the record there is a quite bit of variability indeed.

Did set things at 90 for now (because 91 is just a weird number).

flake8_line_length.txt

Copy link
Member

Choose a reason for hiding this comment

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

I think the flake8 default is 79 and that's what we've been using before our PEP8 CI broke

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Don't feel strongly about any specific value: will change the value and adapt.

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks

files: "nilearn/decomposition"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding new modules and other files will have to be done by modifying this line.
It should be a valid python regex.

.github/workflows/pep8.yml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Feb 3, 2023

While the code formatting is transitioning it may be good to:

  • mention it in the contributing.md
  • also add a mention in there about pre-commit

@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Feb 3, 2023

Regarding mentions in the changelog, it may make sense to have a single entry once most of the code has been blackified (blacked?) rather than one per PR, no?

@Remi-Gau Remi-Gau marked this pull request as ready for review February 3, 2023 16:31
@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Feb 3, 2023

I could not see any code in the reformated files that contained code that had a "table like" layout that would have been mangled by black. Let me know, if you think I overlooked something.

@codecov
Copy link

codecov bot commented Feb 3, 2023

Codecov Report

Merging #3484 (0cb1d65) into main (64d51af) will increase coverage by 0.04%.
The diff coverage is 86.88%.

@@            Coverage Diff             @@
##             main    #3484      +/-   ##
==========================================
+ Coverage   90.94%   90.99%   +0.04%     
==========================================
  Files         133      133              
  Lines       15335    15343       +8     
  Branches     3024     3028       +4     
==========================================
+ Hits        13947    13961      +14     
+ Misses        820      817       -3     
+ Partials      568      565       -3     
Impacted Files Coverage Δ
nilearn/decomposition/dict_learning.py 89.06% <72.72%> (ø)
nilearn/decomposition/canica.py 91.66% <80.00%> (ø)
nilearn/decomposition/_base.py 94.87% <91.89%> (+1.79%) ⬆️
nilearn/decomposition/__init__.py 100.00% <100.00%> (ø)
nilearn/decomposition/_multi_pca.py 100.00% <100.00%> (ø)
nilearn/image/image.py 95.73% <0.00%> (ø)
nilearn/maskers/base_masker.py 83.95% <0.00%> (ø)
nilearn/maskers/multi_nifti_maps_masker.py 95.65% <0.00%> (ø)
nilearn/maskers/multi_nifti_labels_masker.py 95.65% <0.00%> (ø)
nilearn/_utils/docs.py 92.12% <0.00%> (+0.06%) ⬆️
... and 6 more

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

Copy link
Member

@NicolasGensollen NicolasGensollen left a comment

Choose a reason for hiding this comment

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

Great ! Thanks for taking care of that @Remi-Gau ! 👍
I went through the changes and they all LGTM.
Concerning the documentation, I also think we should add a few things related to setting up the pre-commit hook, probably here: https://nilearn.github.io/stable/development.html#setting-up-your-environment
Also, we should make sure our coding style guidelines (https://nilearn.github.io/stable/development.html#coding-style) are consistent with the linting we're enforcing.
Concerning the changelog, I also agree it might be cleaner to have only a single entry for the formatting of the whole code base rather than one entry per PR.
That being said, if we keep the one PR per module style, and different contributors end up doing them, I don't think that it would be a huge deal either...

.pre-commit-config.yaml Outdated Show resolved Hide resolved
.pre-commit-config.yaml Outdated Show resolved Hide resolved
@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Feb 3, 2023

Ok I will update the contributing (but not today).

Also I am realizing that unless we use something like pre-commit ci, it may be good to have something in CI that runs black to at least say which files in the PR should be formatted with black.
Otherwise someone who touches those files later and use pre-commit or black will see some line changes that he or she has not worked on.

Copy link
Member

@bthirion bthirion left a comment

Choose a reason for hiding this comment

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

The changes so far LGTM.

Copy link
Member

@bthirion bthirion left a comment

Choose a reason for hiding this comment

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

LGTM otherwise.

@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Feb 6, 2023

OK I think I am done with this PR.

Let me know if I am missing anything or if I should change anything.

Copy link
Member

@ymzayek ymzayek left a comment

Choose a reason for hiding this comment

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

Thanks, this set up looks great!

.github/workflows/pep8.yml Outdated Show resolved Hide resolved
CONTRIBUTING.rst Outdated Show resolved Hide resolved
@ymzayek
Copy link
Member

ymzayek commented Feb 6, 2023

Can you also remove the recommendation in step 3 under here: https://nilearn.github.io/stable/development.html#id3 and replace with a code block of syntax to flake8 a file?

@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Feb 6, 2023

Can you also remove the recommendation in step 3 under here: https://nilearn.github.io/stable/development.html#id3 and replace with a code block of syntax to flake8 a file?

done

also added mention of black and pre-commit

@ymzayek
Copy link
Member

ymzayek commented Feb 6, 2023

The documentation builder failure is unrelated, see actions/runner-images#7048

For the codecov patch failure, I'm guessing we're gonna see this when reformatting the code base as it will pick up on untested lines that are changed by the reformatting since it runs on the diff. I think we can ignore for these PRs but it could be an idea to start increasing the testing coverage in general for nilearn (in separate PRs based on these reports).

Copy link
Member

@ymzayek ymzayek left a comment

Choose a reason for hiding this comment

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

Thanks @Remi-Gau!

Copy link
Member

@NicolasGensollen NicolasGensollen left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @Remi-Gau !

Copy link
Member

@bthirion bthirion left a comment

Choose a reason for hiding this comment

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

LGTM, thx.

- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.10.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Black had just released a new version, so I should probably bump this before merging.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, you can go ahead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Once this is merged I will open a separate PR with a workflow to check and update the hooks version weekly so we don't have to remember to regularly update this.

Copy link
Member

Choose a reason for hiding this comment

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

Great thanks!

@ymzayek
Copy link
Member

ymzayek commented Feb 7, 2023

Ok merging

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

4 participants