scikit-learn: How to fix merge conflicts related to the black code style reformatting of the scikit-learn code base

In #18948 we recently started to use black to format almost all of scikit-learn’s code, so it’s possible that your PR is conflicting with the main branch due to the new code formatting style. Here are some instructions to resolve conflict that you may encounter when merging your PR’s branch with the main branch.

Here we assume that upstream is an alias for the https://github.com/scikit-learn/scikit-learn git repo or its ssh variant. You can confirm that on your end by checking the output of git remote -v.

You could try to directly merge with main and then apply black locally, but this is likely to introduce a lot of conflicts due to the formatting style change. Instead, we recommend the following workflow:

  • Abort any previous merge attempts in your branch: git merge --abort. If you weren’t in the middle of a merge before this will fail with “fatal: There is no merge to abort”. It’s normal, you can safely ignore this message.

  • Fetch all the commits from the main repo git fetch upstream

  • Merge your PR branch with the commit right before the black formatting commit:

    git merge 0e7761cdc4f244adb4803f1a97f0a9fe4b365a99
    

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

  • Cherry-pick the commit the updates the black config with target_version:

    git cherry-pick e8f58cddb55777e0e40195aaf64d5a890b9fdaac
    

Then, locally apply black to all the code:

  • pip install black==21.6b0

  • To format the code changes in your PR branch from the top level scikit-learn source folder:

    git diff $(git merge-base upstream/main HEAD) --name-only -- '*.py' | xargs black
    
  • Commit black formatted changes.

  git commit -am "apply black"
  • Merge your branch with main:
    git fetch upstream main
    git merge upstream/main
    

This should work without code-style related conflicts.

If git blame is important to you

For git blame to work locally run:

git config blame.ignoreRevsFile .git-blame-ignore-revs

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 16 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I think we can close this PR now that two years have elapsed. What do you think?

Thanks @thomasjpfan , I updated its title, otherwise 👍

Maybe it’s easier to reformat only the files changed in the PR with black by passing all the necessary config and filenames as command line argument, commit the result and then merge the latest main?

That would work. Is there a nice way to get all the files that the PR changed in windows? Maybe just copying and pasting is good enough?

  1. git merge-base upstream/main HEAD
  2. git diff COMMIT_FROM_STEP_1 --name-only -- "*.py"
  3. black -t py39 --exclude ... FILES_FROM_STEP_2

I just tried this approach with one of my older PRs. I think straight merging is easier. Running black yourself with the commit right before black introduces its own merge conflicts.

I updated commit to cherry-pick https://github.com/scikit-learn/scikit-learn/commit/e8f58cddb55777e0e40195aaf64d5a890b9fdaac which adds the target_version into the black config.