Skip to content

Use black and isort for automatic formatting #3058

@Eric-Arellano

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    project: policyOrganization of the typeshed project

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions