How to make pylint, flake8 or pycodestyle to automatically correct errors?
Asked Answered
C

3

18

I have properly installed all of the aforementioned modules on a VM I use on Ubuntu 18.04. When running either of them on a specific script or folder, they do correctly identify style errors and output them in the console. E.g.:

(venv) .../src$ python3.6 -m flake8
./free_prediction.py:8:1: E303 too many blank lines (5)
./free_prediction.py:8:28: E231 missing whitespace after ','
./free_prediction.py:10:5: E225 missing whitespace around operator
./free_prediction.py:12:3: E225 missing whitespace around operator
./free_prediction.py:15:13: E225 missing whitespace around operator

However, the same style errors persist in the code. How can I make them to be automatically corrected?

Claycomb answered 22/2, 2019 at 23:38 Comment(0)
D
24

AFAIK, none of those linting tools will fix the style issues they identify. However, there are several code formatting tools that will automatically fix many of the style errors that were flagged.

Some of the more popular Python code formatting tools worth checking out are: black, autopep8, and yapf. (all of them are on PyPI and installable via pip)

More info:

Dilapidation answered 23/2, 2019 at 1:28 Comment(0)
I
8

The solution you are looking for is called an "auto-formatter."

The tools you mentioned are just for checking for the style of the code.


See this blog post for a thorough comparison of black, autopep8, and yapf against actual Python snippets.

https://medium.com/3yourmind/auto-formatters-for-python-8925065f9505

Iphigenia answered 20/6, 2019 at 18:31 Comment(0)
S
4

Flake8 is growing in attraction.

Hence, the situation has changed a little bit since 2019. I consider it worth noting that there is now also a project for fixing flake8 errors (e.g., McCane complexity) additionally to pycodestyle errors (PEP8):

Selfsupporting answered 11/4, 2022 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.