You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use black and isort for automatic formatting #3058
Hello, I would like to add isort for automatic import sorting and black for general auto-formatting. I'm opening this ticket to first discuss and get the go-ahead.
Motivation
For imports, there is no uniform practice for how to order them, beyond compliance with PEP 8's separation of stdlib vs 3rd. party vs 1st. party import.
For general code formatting, there is a very clear code style but it requires developers to spend time enforcing it. There are also many files that violate the style guide.
This introduces a minor burden to readers who do not have consistent formatting and import orders and a burden to contributors to deal with formatting and import ordering.
Both isort and black offer automated, stable, and accessible mechanisms to fix this for very little effort.
Implementation plan
Add isort==4.3.20 and black==19.3b0 to requirements-tests-py3.txt.
Setup pyproject.toml to configure both isort and black with a line length of 130 and so that isort works with black.
Update the README to explain how to run both tools.
Update .travis.yml with a new entry to run isort and black, both in check-only mode.
Run isort -rc . to fix all current code.
Run black over all code.
Possible concern - git blame
The implementing PR will pollute the git history, but is mitigated by these factors:
Once the tool is setup, there should never be future PRs dealing with import order because the tool will automatically enforce a good order.
GitHub has the option "View prior commit" and most IDE tools like PyCharm have similar features.
There is precedent for PRs that solely impact style, including an endorsement of this type of change in the contributing guide.
I'm OK with this, but if we do we should also start using Black for general formatting, so we only have to pay the one-time cost of reformatting everything once.
we should also start using Black for general formatting
That was going to be my next proposal. Sounds good to combine black into the same PR. Thanks!
One decision that needs to be made: what to do about line length? Currently, the style guide says 130. Black defaults to 88. It encourages not going greater than 100 because it makes it harder for people with sight disabilities + less effective side-by-side diffs. Should I stick with 130 or go with something like 100?
130 is the consensus that was reached in the last discussion. Stubs are a bit different from implementation in that makes sense to have them feel more like an index.
Overall I am in favor of reformatting. The only concern I have is that it puts more of a burden on external contributors, especially since we have an uncharacteristically high amount of them. Ideally we would reformat stubs automatically when merging. Is there a way to do this?
130 is the consensus that was reached in the last discussion.
Sounds good, I will keep this.
Ideally we would reformat stubs automatically when merging. Is there a way to do this?
I don't think so, because Travis cannot propagate changes back up to GitHub. I couldn't find any way to do this via GitHub either.
The only concern I have is that it puts more of a burden on external contributors
I think the best way to address this is to create a simple Python script validate_changes.py to automate all of these steps:
Run:
$ python3.6 -m venv .venv3
$ source .venv3/bin/activate
(.venv3)$ pip3 install -r requirements-tests-py3.txt
This will install mypy (you need the latest master branch from GitHub), typed-ast, flake8, and pytype. You can then run mypy, flake8, and pytype tests by invoking:
(.venv3)$ python3 tests/mypy_test.py
...
(.venv3)$ python3 tests/mypy_selftest.py
...
(.venv3)$ flake8
...
(.venv3)$ python3 tests/pytype_test.py
...
We would replace this with Run ./validate_changes.py to autoformat the code and to run all the unit tests. The script would look like this:
My bot black-out is in experimental mode. It can apply black formatting if the PR is made in the upstream repo, not forked repo.
Example:
Would that be something you're interested in? I can privately tell you how to install it.
I also have plan to make it runs black after every merged PR so it will create another PR that is just black formatting. This is still WIP.
Have a bot either listen to PRs with a ready-to-merge label or at least one successful maintainer review and a successful travis build.
Squash and merge the branch into master.
can apply black formatting if the PR is made in the upstream repo, not forked repo.
Thought I'll add why: This limitation is because of limitation of GitHub Apps API. I've raised the problem with GitHub, they're aware of the issue, but it is in low priority for them to change it.
I'm against using bots for that (if anyone cares about my opinion 😀) and let me explain why. The main reason for the black tool is for code review simplification for a reviewer. A contributor can create MR without code formatting at all, then it will be hard to review. Either it should be the contributor responsibility, (using mentioned tox above) or bot must be run before code review.
Maybe we should start simple. Setup CI to check whether black and isort return errors. At least black can be set up to emit a diff. If we see many CI failures due to this, set up a bot.
Activity
JelleZijlstra commentedon Jun 15, 2019
I'm OK with this, but if we do we should also start using Black for general formatting, so we only have to pay the one-time cost of reformatting everything once.
[-]Use isort for consistent import order[/-][+]Use black and isort for automatic formatting[/+]Eric-Arellano commentedon Jun 16, 2019
That was going to be my next proposal. Sounds good to combine black into the same PR. Thanks!
One decision that needs to be made: what to do about line length? Currently, the style guide says 130. Black defaults to 88. It encourages not going greater than 100 because it makes it harder for people with sight disabilities + less effective side-by-side diffs. Should I stick with 130 or go with something like 100?
cc @ambv
srittau commentedon Jun 16, 2019
130 is the consensus that was reached in the last discussion. Stubs are a bit different from implementation in that makes sense to have them feel more like an index.
Overall I am in favor of reformatting. The only concern I have is that it puts more of a burden on external contributors, especially since we have an uncharacteristically high amount of them. Ideally we would reformat stubs automatically when merging. Is there a way to do this?
Eric-Arellano commentedon Jun 16, 2019
Sounds good, I will keep this.
I don't think so, because Travis cannot propagate changes back up to GitHub. I couldn't find any way to do this via GitHub either.
I think the best way to address this is to create a simple Python script
validate_changes.pyto automate all of these steps:We would replace this with
Run ./validate_changes.py to autoformat the code and to run all the unit tests.The script would look like this:If we go this route, I recommend that we leave the script for a followup PR for a smaller diff that is easier to review.
srittau commentedon Jun 17, 2019
Could we maybe automate this with bots:
ready-to-mergelabel or at least one successful maintainer review and a successful travis build.@Mariatta
Mariatta commentedon Jun 17, 2019
My bot black-out is in experimental mode. It can apply black formatting if the PR is made in the upstream repo, not forked repo.
Example:
Would that be something you're interested in? I can privately tell you how to install it.
I also have plan to make it runs black after every merged PR so it will create another PR that is just black formatting. This is still WIP.
You can see how miss-islington does it: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py
srittau commentedon Jun 17, 2019
@Mariatta Hah, the idea behind black-out seems to be very close to what I had in mind. I will definitely have a look.
Mariatta commentedon Jun 17, 2019
Thought I'll add why: This limitation is because of limitation of GitHub Apps API. I've raised the problem with GitHub, they're aware of the issue, but it is in low priority for them to change it.
hh-h commentedon Sep 28, 2019
@Eric-Arellano your solution with the custom script looks bulky :) There's a tool for that https://tox.readthedocs.io/en/latest/
hh-h commentedon Sep 28, 2019
I'm against using bots for that (if anyone cares about my opinion 😀) and let me explain why. The main reason for the
blacktool is for code review simplification for a reviewer. A contributor can create MR without code formatting at all, then it will be hard to review. Either it should be the contributor responsibility, (using mentionedtoxabove) or bot must be run before code review.srittau commentedon Oct 8, 2019
Maybe we should start simple. Setup CI to check whether black and isort return errors. At least black can be set up to emit a diff. If we see many CI failures due to this, set up a bot.
@JelleZijlstra What do you think?
JelleZijlstra commentedon Oct 8, 2019
Sure, that's worth trying.
srittau commentedon Oct 9, 2019
Changes to CONTRIBUTING.md, .travis.yml and a sample pre-commit file in #3329. We need to decide how to best do the "big reformat".
Eric-Arellano commentedon Jun 28, 2020
Superseded by #3329 🚀