avatarJohannes Schmidt

Summary

The webpage provides a comprehensive guide on automating code formatting and linting in PyCharm using tools like black, isort, and flake8 to ensure consistent coding styles and adherence to PEP8 standards.

Abstract

The article discusses the importance of code consistency in team environments through the use of formatters and linters. It introduces popular formatting tools such as black and yapf, which help maintain a uniform code style, and linters like flake8 that check for semantic discrepancies and style guide violations. The author emphasizes the integration of these tools into PyCharm using the File Watchers plugin to automatically run formatters and linters upon file changes or saves. This integration helps in onboarding new team members, reducing merge conflicts, and saving time by automating the enforcement of coding standards. The article also provides examples and configurations for setting up black for formatting, isort for import sorting, and flake8 for code linting within the PyCharm environment, ensuring that developers adhere to PEP8 without manual intervention.

Opinions

  • The author believes that using an uncompromising formatter like black is beneficial because it eliminates the need for discussions about formatting options and aligns the codebase across various projects.
  • The use of isort in conjunction with black is recommended to ensure that imports are not only formatted but also sorted correctly according to PEP8.
  • The author suggests that flake8 is a valuable tool for enforcing style consistency and that it should be configured to avoid incompatibilities with black.
  • Automating the linting and formatting process with PyCharm's File Watchers plugin is seen as a significant timesaver and a way to streamline code reviews.
  • The author encourages the use of specific configurations for black and flake8 to maintain consistency and avoid common pitfalls.
  • The author advocates for the integration of these tools as a means to improve the overall code quality and developer experience within a team.
  • The author promotes the use of an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), offering similar performance and functionality at a lower price point.

Automate linting & formatting in PyCharm with your favourite tools

Run black, flake8 & other tools automatically with every file change

Photo by Brett Jordan on Unsplash

If you’ve ever worked in a team, you know that to achieve code and style consistency, you need to agree on a formatter and linter. It will help you with onboarding new members to the codebase, create fewer merge conflicts and generally save time because developers don’t have to care about formatting and style while coding.

Before we dive into how to make your PyCharm run your favourite tools with every file change automatically, let’s cover a few basics.

Formatters

Formatters are tools that reformat some basic things in your code like quotes, commas, and line length. They make sure your codebase stays in a consistent style without any manual work on your end.

Popular Formatters: black, autopep8, yapf

Linters

These tools perform static analysis of source code, checking for semantic discrepancies. So “Linting” means running a tool against your code that identifies violations of style guides. The tool will check your code syntax and provide message that tell you what’s wrong with it.

Popular linters: flake8, pylint

In this blog article I will focus on black, isort and flake8. However, other tools such as mypy (static type checker) can be used in the exact same way. So here’s a short introduction into these tools:

Black

A popular auto formatter is black, “the uncompromising code formatter”. The great part about this tool is:

Blackened code looks the same regardless of the project you’re reading.

It is very opinionated but that’s actually good thing. Because not having many options makes you not discuss the options at all. It’s the basis of many projects (pytest, django etc.) and formats the code to look nicely without being too strict. You can compare its mindset to the formatter prettier for JavaScript.

Here’s an example:

Running black — gif by author

Black is a great formatter but it does not everything what you’d expect. While black also formats your imports, it does not automate sorting them for you. Here is an example of imports that violates the style guide for Python code PEP8:

Black does not sort imports — gif by author

isort

Here comes isort to the rescue. It “helps to sort and format imports in your Python code.” While Black also formats imports, isort takes care of sorting them correctly, pep8-compliant:

Running isort — gif by author

Because there could be conflicts between these two tools, it is advised to use the black profile in isort. Just add .isort.cfg with this content in your projects root:

Ok, so code formatting is covered with e.g. black and imports can be sorted with isort. What about stylistic issues? PyCharm does already a good job finding these through its built in plugins but to be able to enforce specific style-guides for CI, we need a tool that finds stylistic errors for us. Here comes flake8, a popular code linter:

Running flake8 — gif by author

It can detect violations of style rules in source code. It is easy to configure and extensible though plugins. There is a great curated list for flake8 plugins; include flake8-markdown and flake8-pytest.

flake8 itself is a wrapper which verifies pep8, pyflakes and circular complexity. It’s also a command line utility for enforcing style consistency across Python projects.

To avoid incompatibilities with black, here are the recommended settings in a .flake8 config file:

PyCharm integration

You might be thinking: Ok that’s all cool but how do I integrate these tools in PyCharm to run automatically?

I assume that you have formatted your code with PyCharm’s Reformat Code command and imports have been optimised with Optimize Imports so far, either through a shortcut or with a suggested action:

Automatic file formatting with file watchers plugin — gif by author

If a project uses an auto-formatter, e.g. black, and you reformat code in this way (default PyCharm project settings), chances are high that the code review will be unnecessarily long and harder to read for your colleagues. Even though PyCharm adheres to PEP8 rules and requirements for arranging and formatting Python code, you often get a slightly different result depending on the formatter used. To mitigate this issue, we can change the code style scheme or the change the .editorconfig file in our project. If anything is not defined in .editorconfig, it's taken from the project settings. And we can even have automatic reformatting on file save.

But there is another option that allows us to run formatters on file change. Introducing the file watchers plugin.

File Watcher is a PyCharm tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

So first, we need to download and enable this plugin:

File Watchers plugin — image by author

We can then find the plugin under the Tools section in Preferences . Here we can create a file watcher for each of our two formatters black and isort:

File Watchers custom watcher — gif by author

Black settings:

Settings for the black watcher — image by author

isort settings:

Settings for the isort watcher — image by author

Please note: For isort I specify the .isort.cfg that can be found in the project’s root directory. But as pointed out in the comments, we don’t necessarily require this config file that just tells us to use the black profile. Instead we can have it like this: $FilePath$ — profile black

Here is the result for our formatters (black & isort):

black and isort watcher in action — gif by author

Note: In this example, formatting is invoked with every file save. If you want instant changes while coding, consider checking the Auto-save edited files to trigger the watcher checkbox for your formatters.

Now let’s add flake8 to our automatic stack.

flake8 settings:

Settings for the flake8 watcher — image by author

Which we also only want to run when the file is saved. Adding to that, we want inspection hints that tell us what’s wrong with the code we are writing. For this, we need to make the modifications in the inspections section under the Editor settings:

Here I want pink borders around code that violates the flake8 style guides. The result looks like this:

Voilà! We integrated our tools into the PyCharm IDE. Feel free to try it out!

If you enjoyed this article, please leave a clap. If you disagree with any of the content, leave a comment.

Python
Formatting
Linting
Code
Programming
Recommended from ReadMedium