8000 MAINT Black formatting prework by thomasjpfan · Pull Request #20260 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

MAINT Black formatting prework #20260

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

Merged
merged 11 commits into from
Jun 17, 2021
Merged

Conversation

thomasjpfan
Copy link
Member

Reference Issues/PRs

Related to #18948

What does this implement/fix? Explain your changes.

This PR adds the documentation and prepares the CI to check black's formatting. This PR also fixes the black config to run correctly with black . I think we can do this all on the main branch:

  1. Merge this PR.
  2. Merge PR that runs black formatting. This PR also turns on CI to check black formatting.
  3. Merge PR to add a .git-blame-ignore-revs that ignores the black formatting commit.
  4. Cherrypick this PR onto 0.24.x to update docs.

Any other comments?

I experimented with resolving merge conflicts merging black formatted code into some PRs. I think they are not difficult to resolve and should not be a major blocker.

CC @rth @NicolasHug

Copy link
Member
@rth rth left a comment

Choose a reason for hiding this comment

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

Very minor comments, otherwise looks good!

Thanks Thomas!

Edit: CI needs investigating though.

rev: 21.6b0
hooks:
- id: black
language_version: python3
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure this is necessary?

Copy link
Member
@ogrisel ogrisel Jun 14, 2021

Choose a reason for hiding this comment

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

You mean the python3 config or the black pre-commit config as whole?

As far as I know the precommits will not be enabled as long as the user does install the optional pre-commit developer dependency.

Copy link
Member Author

Choose a reason for hiding this comment

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

pre-commit support is not strictly necessary, so we can remove.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think I have an opinion on this. Any option would do.

@@ -45,8 +45,12 @@ jobs:
versionSpec: '3.9'
- bash: |
# Include pytest compatibility with mypy
pip install pytest flake8 mypy==0.782
pip install pytest flake8 mypy==0.782 black
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
pip install pytest flake8 mypy==0.782 black
pip install pytest flake8 mypy==0.782 black==21.6b0

Copy link
Member

Choose a reason for hiding this comment

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

If we pin a specific version of black in the CI, we should also pin it in doc/developers/contributing.rst.

However I am not sure it's worth the added complexity. I am pretty sure that devs will often have the latest version of black in their dev env.

I would be in favor of trying without pinning. If it proves annoying we can always decide to pin a specific version later until the black format spec is declared final.

Copy link
Member
@rth rth Jun 14, 2021

Choose a reason for hiding this comment

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

After having used black on some other projects for a while, I find that that if one doesn't pin black version one will get in trouble eventually. For instance my current scikit-learn dev env is from 2020 and the version there produces a slightly different formatting to version 21.6b0 . Similarly there might a change in the next release that is not be fully compatible etc. So it's not like flake8 where the version doesn't matter.

Copy link
Member
@rth rth Jun 14, 2021

Choose a reason for hiding this comment

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

And for sure the pre-commit version of black and the CI version need to match exactly. Otherwise we will get CI failure for each new black release.

Copy link
Member
@ogrisel ogrisel Jun 14, 2021

Choose a reason for hiding this comment

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

Alright, I am fine with pinning, then (if others agree). The documentation needs to be updated accordingly.


.. prompt:: bash $

git config blame.ignoreRevsFile .git-blame-ignore-revs
Copy link
Member

Choose a reason for hiding this comment

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

Neat!

Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to make it enabled by default? In particular for the online git blame UI of github.com?

Copy link
Member

Choose a reason for hiding this comment

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

I read the link. Unfortunately it's not possible at this time:

The one caveat is that GitHub and GitLab do not yet support ignoring revisions using their native UI of blame. So blame information will be cluttered with a reformatting commit on those platforms. (If you’d like this feature, there’s an open issue for GitLab and please let GitHub know!)

Copy link
Member Author

Choose a reason for hiding this comment

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

The request for github to support blame ignore files is: https://github.community/t/support-ignore-revs-file-in-githubs-blame-view/3256 So using git blame with the github UI will require one more click to get pass the black commit. On the other hand, editors support blame.ignoreRevsFile pretty well.

Copy link
Member

Choose a reason for hiding this comment

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

I "hearted" the feature request on github.community.

@rth
Copy link
Member
rth commented Jun 14, 2021

It might be good to also have instructions of what to do if an existing PR has conflicts due to black. As in run black before merging master in (not after). And because it's temporary, it might be easier to have it as an issue or a discussion entry rather than documentation section. The main part is that we need to be able to link to it, which is currently not possible with documentation paragraphs.

Documentation CI has some issues apparently.

8000

@rth
Copy link
Member
rth commented Jun 14, 2021

Opened #20261

@NicolasHug
Copy link
Member

Maybe a sticky issue will be more visible? This is probably where contributors will post to ask for help rather than in dicussions

@rth
Copy link
Member
rth commented Jun 14, 2021

As you prefer. It's just looked like a perfect use case for a Discussion Q&A and I'm trying to try using it somewhere :) I'm fine with an issue as well, feel free to re-open one otherwise.

It's possible to pin a discussion entry as well.

@thomasjpfan
Copy link
Member Author

The merge conflicts you get from merging with the black commit has the same workflow as we have now. What kind of additional information would you like to see in this pinned issue?

@rth
Copy link
Member
rth commented Jun 14, 2021

I meant to indicate that users should definitely not try manually resolving merge conflicts they get before running black. And if they have run,

git merge upstream/main

already, they can cancel it with git merge --abort, run black then re-run the merge.

@NicolasHug
Copy link
Member
NicolasHug commented Jun 14, 2021

Also the "run black" part of the instruction is probably not just black . since users don't necessarily have https://github.com/scikit-learn/scikit-learn/pull/19031/files merged already, where all the black config is.

@thomasjpfan
Copy link
Member Author

Also the "run black" part of the instruction is probably not just black . since users don't necessarily have https://github.com/scikit-learn/scikit-learn/pull/19031/files merged already, where all the black config is.

For current PRs, I think the simplest workflow is: Fetch and merge with upstream/main then run black .

@rth
Copy link
Member
rth commented Jun 14, 2021

Aww, so that works? I though it would create lots of merge conflicts. But maybe I'm mistaken. Anyway I guess we will find out after we apply black on main )

@NicolasHug
Copy link
Member

For current PRs, I think the simplest workflow is: Fetch and merge with upstream/main then run black .

upstream/main is black-formatted but the PR isn't. Wouldn't git pull upstream main create tons of conflicts?

@thomasjpfan
Copy link
Member Author

Aww, so that works? I though it would create lots of merge conflicts. But maybe I'm mistaken. Anyway I guess we will find out after we apply black on main )

I'll experiment a little more with some more PRs. I compared the conflicts I get merging pre-black and post-black and the complexity didn't change.

@NicolasHug
Copy link
Member
NicolasHug commented Jun 14, 2021

yeah:

(sklearn-dev) ➜  scikit-learn git:(main) ✗ git co -b black
Switched to a new branch 'black'
(sklearn-dev) ➜  scikit-learn git:(black) ✗ black sklearn/pipeline.py
reformatted sklearn/pipeline.py
All done! ✨ 🍰 ✨
1 file reformatted.
(sklearn-dev) ➜  scikit-learn git:(black) ✗ git ci -am "formatted pipeline"
[black de95fdf1d] formatted pipeline
 1 file changed, 129 insertions(+), 122 deletions(-)
(sklearn-dev) ➜  scikit-learn git:(black) ✗ code .
(sklearn-dev) ➜  scikit-learn git:(black) ✗ git co main
Switched to branch 'main'
Your branch is ahead of 'nicolashug/main' by 162 commits.
  (use "git push" to publish your local commits)
(sklearn-dev) ➜  scikit-learn git:(main) ✗ git co -b my_pr
Switched to a new branch 'my_pr'
(sklearn-dev) ➜  scikit-learn git:(my_pr) ✗ git diff
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
modified: sklearn/pipeline.py
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
@ sklearn/pipeline.py:463 @ class Pipeline(_BaseComposition):
        fit_params_last_step = fit_params_steps[self.steps[-1][0]]
        with _print_elapsed_time('Pipeline',
                                 self._log_message(len(self.steps) - 1)):
-            y_pred = self.steps[-1][1].fit_predict(Xt, y,
+            y_pred = self.steps[-1][1].fit_predict(Xt, y,  # some comment
                                                    **fit_params_last_step)
        return y_pred

(sklearn-dev) ➜  scikit-learn git:(my_pr) ✗ git ci -am "minor comment"
[my_pr 5b8564afc] minor comment
 1 file changed, 1 insertion(+), 1 deletion(-)
(sklearn-dev) ➜  scikit-learn git:(my_pr) ✗ git merge black
Auto-merging sklearn/pipeline.py
CONFLICT (content): Merge conflict in sklearn/pipeline.py
Automatic merge failed; fix conflicts and then commit the result.

TLDR: even a one-line change will create conflicts and we can expect those absolutely everywhere

@thomasjpfan
Copy link
Member Author

TLDR: even a one-line change will create conflicts and we can expect those absolutely everywhere

I see what you mean. Would the pinned issue be advice on how to resolve merge conflicts?

@NicolasHug
Copy link
Member
NicolasHug commented Jun 14, 2021

Would the pinned issue be advice on how to resolve merge conflicts?

More like on how to avoid them, but yes. It will probably look like

  • git merge --abort (depending on whether they're in the middle of conflicts)
  • install black
  • run black . --include all the stuff to inlcude -- exclude all the stuff to exclude aka all the stuff that's in https://github.com/scikit-learn/scikit-learn/pull/19031/files and that we can't assume to be in the PR author's history
  • git pull upstream main

Copy link
Member
@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM assuming we can come with a pinned issue to help contributors resolve the conflicts in their PR with a few lines to copy/paste as @NicolasHug suggested.

@thomasjpfan
Copy link
Member Author
thomasjpfan commented Jun 14, 2021

I went through a few PRs and I think the best workflow for resolving black related merge conflicts is:

  1. Abort your merge: git merge --abort
  2. git fetch upstream
  3. Merge your PR with the commit right before the black formatting commit.
git merge COMMIT_BEFORE_BLACK

This will import the black config and deal with existing merge conflicts before black formatting.

  1. Install and run black .
  2. Commit black formatted changes.
  3. Merge with main.

@ogrisel
Copy link
Member
ogrisel commented Jun 15, 2021

What is nice with this approach described in #20260 (comment) is that the commit right before black will have the necessary black configuration, so no need to pass complex command line options.

@rth
Copy link
Member
rth commented Jun 17, 2021

Should we merge? I'll add the above answer once we have the hash of the last commit before black was applied.

@ogrisel
Copy link
Member
ogrisel commented Jun 17, 2021

Should we merge? I'll add the above answer once we have the hash of the last commit before black was applied.

We should probably not merge #20260 if #18948 is not ready to merge just after it (which is not the case at the moment).

But +1 for merging #20260 and #18948 once the latter has green CI.

@ogrisel
Copy link
Member
ogrisel commented Jun 18, 2021

We still need to create a pinned issue with the instructions of #20260 (comment) right?

@rth
Copy link
Member
rth commented Jun 18, 2021

Yes, I think.

@ogrisel
Copy link
Member
ogrisel commented Jun 18, 2021

I opened: #20301 and pinned it. Feel free to improve it directly @thomasjpfan @rth @NicolasHug or others.

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.

4 participants
0