Search⌘ K

Tooling for Automatic Formatting

Explore how automatic formatting tools help enforce clean code practices in Python by standardizing style and reducing code review friction. Understand the features of tools like Black and Yapf, their advantages, limitations, and how they fit into development workflows to improve code readability and maintainability.

We'll cover the following...

Automatic formatting

As mentioned earlier in the course, it would be wise for the team to agree on a writing convention for the code, to avoid discussing personal preferences on pull requests, and focus on the essence of the code. But agreements only get us so far, and if these rules aren't enforced, they'll get lost over time.

Besides checking for adherence to standards by means of tooling, it is useful to automatically format the code directly.

There are multiple toolsFor example, most of the tools that validate PEP-8, like flake8, also have a mode to rewrite the code and make it PEP-8 compliant. that automatically format Python code, and they're also configurable and adaptable to each specific project. Among those is one that we'd like to highlight, in part because it does not offer full flexibility and configuration: black.

The black tool has a peculiarity that formats code in a unique and deterministic way, without allowing any parameters (except perhaps the length of the lines). ...