2

Flake8 Config in Pyls for Code Linting.

 3 years ago
source link: https://jdhao.github.io/2020/11/05/pyls_flake8_setup/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Flake8 Config in Pyls for Code Linting.

2020-11-05350 words 2 mins read 124 times read

In my previous post, I have go over the basic setup to make vim-lsp work. However, I haven’t touch on an important part of writing code: linting. It turns out that configure it correctly is harder than I thought.

I found the documentation of pyls to be vague and incomplete. It took me quite a few hours in order to understand how to configure flake8 correctly for pyls.

Pyls is a combination of different packages to form a unified workable language server. Flake8 is like a driver package for pycodestyle (former known as pep8 and got renamed) and pyflakes. In simple words, it uses pyflakes and pycodestyle and a few other packages to check your code for possible style issues and syntax errors.

By default, pyls disables flake8, and use pycodestyle and pyflakes for code checking directly. I do not want to configure two packages, I just want to configure flake8 for simplicity’s sake.

From here and the actual code, we can see that flake8 is indeed disabled. Also, if we use flake8, it makes sense to disable pycodestyle and pyflakes, otherwise we will get duplicated diagnostic messages.

if executable('pyls')
    " pip install python-language-server
    au User lsp_setup call lsp#register_server({
          \ 'name': 'pyls',
          \ 'cmd': {server_info->['pyls']},
          \ 'allowlist': ['python'],
          \ 'workspace_config': {
          \    'pyls':
          \        {'configurationSources': ['flake8'],
          \         'plugins': {'flake8': {'enabled': v:true},
          \                     'pyflakes': {'enabled': v:false},
          \                     'pycodestyle': {'enabled': v:false},
          \                    }
          \         }
          \ }})
endif

This is the correct config for pyls to use flake8 for linting, period.

config location

According to flake8 documentation, the location of flake8 config varies based on systems, on Linux and Mac, it is ~/.config/flake8 , and for Windows, it is $HOME\.flake8 ($HOME is like C:\\Users\sigmavirus24). The content should be in INI format:

[flake8]
max-line-length = 100
max-complexity = 30
ignore =
    # missing whitespace around arithmetic operator
    E226,
    # line break before/after binary operator
    W503,
    W504,
    # expected 1 blank line, found 0
    E301,E302,

To suppress a single warning, it is also handy to add # noqa: F841-like (change the code to the actual code you want to use) comment string to suppress it.

References

Author jdhao

LastMod 2020-11-16

License CC BY-NC-ND 4.0

Reward
Prev Next

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK