Skip to content

Use black and isort for automatic formatting #3058

Closed

Description

@Eric-Arellano

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

  1. Add isort==4.3.20 and black==19.3b0 to requirements-tests-py3.txt.
  2. Setup pyproject.toml to configure both isort and black with a line length of 130 and so that isort works with black.
  3. Update the README to explain how to run both tools.
  4. Update .travis.yml with a new entry to run isort and black, both in check-only mode.
  5. Run isort -rc . to fix all current code.
  6. 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.

Activity

  1. JelleZijlstra commented on Jun 15, 2019

    @JelleZijlstra
    Member

    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.

  2. changed the title [-]Use isort for consistent import order[/-] [+]Use black and isort for automatic formatting[/+] on Jun 16, 2019
  3. Eric-Arellano commented on Jun 16, 2019

    @Eric-Arellano
    ContributorAuthor

    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?

    cc @ambv

  4. added
    project: policy
    on Jun 16, 2019
  5. srittau commented on Jun 16, 2019

    @srittau
    Collaborator

    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?

  6. Eric-Arellano commented on Jun 16, 2019

    @Eric-Arellano
    ContributorAuthor

    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:

    def main() -> None:
        args = create_parser().parse_args()
        check_valid_interpreters()
        if not Path(".venv3").is_dir():
            setup_venv()
        activate_venv()
        if len(sys.argv) == 1:
            autoformat()
            lint()
            check_consistent()
            mypy_test()
            mypy_self_test()
            pytype_test()
            return
        if args.autoformat:
            autoformat()
        if args.lint:
            lint()
        if args.check_consistent:
            check_consistent()
        if args.mypy_test:
            mypy_test()
        if args.mypy_self_test:
            mypy_self_test()
        if args.pytype_test:
            pytype_test()

    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.

  7. srittau commented on Jun 17, 2019

    @srittau
    Collaborator

    Could we maybe automate this with bots:

    • 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.
    • Checkout the PR into a separate branch.
    • Run black and isort against that branch and commit.
    • Squash and merge the branch into master.

    @Mariatta

  8. Mariatta commented on Jun 17, 2019

    @Mariatta
    Member

    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:

    D8332b1U0AAX7pk

    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.

    You can see how miss-islington does it: https://github.com/python/miss-islington/blob/master/miss_islington/status_change.py

  9. srittau commented on Jun 17, 2019

    @srittau
    Collaborator

    @Mariatta Hah, the idea behind black-out seems to be very close to what I had in mind. I will definitely have a look.

  10. Mariatta commented on Jun 17, 2019

    @Mariatta
    Member

    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.

  11. hh-h commented on Sep 28, 2019

    @hh-h
    Contributor
  12. hh-h commented on Sep 28, 2019

    @hh-h
    Contributor

    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.

  13. srittau commented on Oct 8, 2019

    @srittau
    Collaborator

    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?

  14. JelleZijlstra commented on Oct 8, 2019

    @JelleZijlstra
    Member

    Sure, that's worth trying.

  15. srittau commented on Oct 9, 2019

    @srittau
    Collaborator

    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".

  16. Eric-Arellano commented on Jun 28, 2020

    @Eric-Arellano
    ContributorAuthor

    Superseded by #3329 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    project: policy

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions