Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Descriptive flake8 errors in PyCharm

Tags:

PyCharm does not have a built-in support for flake8 at the moment. But, flake8 can be configured to run as an external tool.

Sometimes, especially for Python newcomers, not every flake8 warning is understandable and additional clarification is required.

We've recently stumbled upon the Flake8Rules project which attempts to describe every single warning in a detailed way with supportive examples.

Is there a way to combine PyCharm, flake8 and Flake8Rules altogether to have static code analysis warnings displayed with additional descriptions or links to the Flake8Rules catalog?

like image 473
alecxe Avatar asked Jan 02 '18 18:01

alecxe


People also ask

What is flake8?

Flake8 is a Python library that wraps PyFlakes, pycodestyle and Ned Batchelder's McCabe script. It is a great toolkit for checking your code base against coding style (PEP8), programming errors (like “library imported but unused” and “Undefined name”) and to check cyclomatic complexity.


2 Answers

It is definitely possible.

One approach would be to adjust the flake8 output using the --format command-line option to specify http(s) links to the Flake8Rules catalog:

--format='%(path)s:%(row)d,%(col)d:%(code)s:%(text)s:https://lintlyci.github.io/Flake8Rules/rules/%(code)s.html' 

The problem then is for the console or PyCharm output window to render the links properly.

Fortunately, we can do that using the plugins - "Awesome Console" for the terminal and "Console Link" for the output window.


##Step-by-step Instructions

  1. make sure to have flake8 installed in the current Python environment
  2. install "Awesome Console" plugin:
  • go to PyCharm Preferences -> Plugins -> Browser Repositories...
  • find "Awesome Console" and install (PyCharm restart required): enter image description here
  1. configure "flake8" as an External Tool:
  • go to PyCharm Preferences -> Tools -> External Tools -> "+"
  • configure the path to flake8 as well as $FilePath$ placeholder for the desired directory/path to be processed: enter image description here

##Demo

Now, let's say we have created this test.py file with a few violations:

def f(a = 10):   return a*10 

If we right-click on a test.py file, select External Tools -> flake8, this is the output we are going to get (note the clickable links for every warning):

enter image description here

Now, whenever in doubt, we can follow the link for additional details about a warning.

This is just one way to do it, would be happy to hear if there is an easier or better way to combine these tools and projects.

like image 160
alecxe Avatar answered Sep 30 '22 17:09

alecxe


Today i Also face this problem although @alecxe answer is good for one project settings

If you want to set flake8 globally , you can follow below process

  1. make sure flake8 installed in your project
  2. make sure virtualenv path set in pycharm
  3. configure flake8 as external tool goto file> settings> (Tools) > External Tools > '+'

configure the path enter image description here

Program - The path to the flake8 executable $PyInterpreterDirectory$ is a directory where the Python interpreter of the current project is placed

Argument- Specifies what files and folders should be checked $FilePath$

Working directory - Project root directory $ContentRoot$

like image 43
Somil Avatar answered Sep 30 '22 17:09

Somil