-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
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
Conversation
There was a problem hiding this 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.
.pre-commit-config.yaml
Outdated
rev: 21.6b0 | ||
hooks: | ||
- id: black | ||
language_version: python3 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
azure-pipelines.yml
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pip install pytest flake8 mypy==0.782 black | |
pip install pytest flake8 mypy==0.782 black==21.6b0 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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. |
Opened #20261 |
Maybe a sticky issue will be more visible? This is probably where contributors will post to ask for help rather than in dicussions |
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. |
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? |
I meant to indicate that users should definitely not try manually resolving merge conflicts they get before running black. And if they have run,
already, they can cancel it with |
Also the "run black" part of the instruction is probably not just |
For current PRs, I think the simplest workflow is: Fetch and merge with upstream/main then run |
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. |
yeah:
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? |
More like on how to avoid them, but yes. It will probably look like
|
There was a problem hiding this 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.
I went through a few PRs and I think the best workflow for resolving black related merge conflicts is:
This will import the black config and deal with existing merge conflicts before black formatting.
|
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. |
Should we merge? I'll add the above answer once we have the hash of the last commit before black was applied. |
We still need to create a pinned issue with the instructions of #20260 (comment) right? |
Yes, I think. |
I opened: #20301 and pinned it. Feel free to improve it directly @thomasjpfan @rth @NicolasHug or others. |
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:.git-blame-ignore-revs
that ignores the black formatting commit.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