0

I am trying to ignore flycheck warning messages, when lsp-mode for the python-mode is on. I have tried ["# noqa", "# NOQA", "# flake8: noqa", "# type: ignore"] but neither of them seems working.

enter image description here


I am totaly lost how I can fix this .

Installation from https://github.com/python-lsp/python-lsp-server

pip install 'python-lsp-server[all]'

my basic configuration is as follows:

(require 'package)
(setq user-init-file (or load-file-name (buffer-file-name)))
(setq user-emacs-directory (file-name-directory user-init-file))

(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(setq frame-background-mode 'dark)
(defun flycheck-python-setup ()
  (flycheck-mode))
(require 'flycheck)
(require 'flycheck-mypy)
(add-hook 'after-init-hook #'global-flycheck-mode)
(add-hook 'after-init-hook #'global-flycheck-mode)
(add-to-list 'flycheck-disabled-checkers 'python-flake8)
(add-hook 'python-mode-hook
          (lambda ()
            (setq flycheck-python-pylint-executable "~/venv/bin/pylint")
            (setq flycheck-pylintrc "~/.pylintrc")
            (setq indent-tabs-mode  nil
                  python-indent-offset  4
                  tab-width         4)
            (let ((inhibit-message  t))
              )))
(use-package python :ensure nil)
(add-hook 'python-mode-hook #'flycheck-python-setup)
(flycheck-add-next-checker 'python-flake8 'python-pylint 'python-mypy)

(require 'lsp-pylsp)
(setq lsp-pylsp-plugins-pylint-args ["--rcfile=~/.pylintrc"])
(setq lsp-pylsp-plugins-pylint-enabled t)
(add-hook 'python-mode-hook 'lsp)
(add-hook 'python-mode-hook #'lsp-deferred)
(setq lsp-headerline-breadcrumb-enable nil)
(setq lsp-enable-symbol-highlighting nil)
(setq lsp-modeline-diagnostics-enable 1)
(setq lsp-ui-doc-enable nil)
(setq lsp-lens-enable nil)
(setq lsp-pyls-plugins-pylint-enabled nil)
(setq lsp-ui-sideline-enable nil)
(setq lsp-ui-sideline-show-diagnostics nil)
(setq lsp-log-io nil) ; if set to true can cause a performance hit
(setq lsp-enable-snippet nil)  ;; solves to prevent () to be added during completions
(setq lsp-file-watch-threshold 2000)
(setq lsp-idle-delay 0.500)
(setq read-process-output-max (* 1024 1024)) ;; 1mb
(setq gc-cons-threshold 402653184
      gc-cons-percentage 0.6)

Please note that # noqa works if lsp is disabled.

11
  • Not all language-servers (or linters) respect #noqa. Make sure your language server is configured correctly and required plugins are loaded. Or use a language server that you know respects #noqa.
    – nega
    Nov 15, 2021 at 18:20
  • @neqa I am using # noqa only for Python, not any other language. How could I configure python-language server correctly? When I check cmd-c-!-v all plugins seems loaded
    – alper
    Nov 15, 2021 at 20:15
  • I don't know how you could configure your language server correctly since you haven't told us which python language server you're using. I can think of at least 5 python language servers off the top of my head, and I'd bet 30 seconds of googling will add 3 more. I'm going to guess that you're using Douglas Davis's lsp-pylsp which was integrated into lsp-mode earlier this year. Reading that .el file ...
    – nega
    Nov 16, 2021 at 4:07
  • ... you learn that it's designed to use the Spyder IDE python-language-server. I would suggest you start with that project's README.
    – nega
    Nov 16, 2021 at 4:09
  • Additionally, you add 'python-flake8 to flycheck-disabled-checkers, then later you use 'python-flake8 as an argument to flycheck-add-next-checker. Does that combination actually do what you think it does?
    – nega
    Nov 16, 2021 at 4:12

1 Answer 1

2

tl;dr

When trying to figure anything out, simplify!

$ pip3 install 'pyparsing<3'
$ pip3 install python-lsp-server
# use a minimal `~/.emacs.d/init.el`, such as the one listed below

Here's my recipe to get lsp-mode+python-lsp-server working with flycheck.

python-lsp-server

First we need an lsp server for Python. Following the instructions at https://github.com/python-lsp/python-lsp-server#installation I attempted to install python-lsp-server

$ pip3 install python-lsp-server

Installation failed, because pip3 wanted to install pyparsing v3.0.5 while python-lsp-server wanted 'pyparsing<3,>=2.0.2'. This was easily remedied.

$ pip3 install 'pyparsing<3'
$ pip3 install python-lsp-server

lsp-mode + flycheck

Next I created a fresh ~/.emacs.d/init.el, and following the instructions at https://emacs-lsp.github.io/lsp-mode/page/installation/#use-package I added the recommended use-package form. Also, by adding setup for flycheck this gives me a minimal init file of:

;; -*- mode: emacs-lisp; ; indent-tabs-mode: nil -*-

;; Bootstrap package.el
(require 'package)
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/")
             '("gnu" . "https://elpa.gnu.org/packages/"))
(package-initialize)

;; Bootstrap 'use-package'
(eval-after-load 'gnutls
  '(add-to-list 'gnutls-trustfiles "/etc/ssl/cert.pem"))
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(eval-when-compile
  (require 'use-package))
(require 'bind-key)
(setq use-package-always-ensure t)

;; setup flycheck
(use-package flycheck
  :config (global-flycheck-mode))

;; setup lsp-mode, https://emacs-lsp.github.io/lsp-mode/page/installation/#use-package
(use-package lsp-mode
  :init
  ;; set prefix for lsp-command-keymap (few alternatives - "C-l", "C-c l")
  (setq lsp-keymap-prefix "C-c l")
  :hook (;; replace XXX-mode with concrete major-mode(e. g. python-mode)
         (python-mode . lsp)
         ;; if you want which-key integration
         (lsp-mode . lsp-enable-which-key-integration))
  :commands lsp)

Sample python file

Now we need a sample python file with known style errors.

$ cat > ~/foo/foo.py
#!/usr/bin/env python3

import os,sys

example = lambda: 'example'
^D
$

Editing our python file

Now we get to work...

$ emacs ~/foo/foo.py

lsp-mode asks us what we want to do..

lsp-mode import dialog

Naturally, we want to import our new file, so we hit i, and lsp-mode lets us know it has successfully imported our file.

lsp-mode notification of successful import

Now, you'll notice that our file has flycheck's familiar arrows and squiggles.

emacs window with python code and linter error symbols

If we add # noqa to line #5, you'll see its squiggle, and error listing, are gone.

emacs window with python code and linter error symbols, showing the effect of # noqa to a line of code in order to skip linting

You may also have noticed that the "Checker" in *Flycheck errors* is "lsp". This is what we want. If you want flake8 to be the primary checker, you can select it for the current session via C-c!v, selecting "python-flake8", and hitting q to close the selection window. When back at your foo.py window, run M-x flycheck-error-list-reset-filter. You'll notice that line #5 continues to be "error free".

similar as the previous image, but using flycheck's python-flake8 checker

Now what?

Continue to build up your configuration until something strange or unexpected happens. Stop. Undo that last change. Read the documentation of what you added and figure out what you did wrong. Do not blindly add more and more to your configuration, and dig yourself a deeper hole.

4
  • I have tried your exact code with a python file: emacs -Q -nw -l ~/home/emacs_lsp.el ~/test.py but I am still seeing warning message for the lines that has # noqa. I am using python-lsp-server==1.3.3 and pyparsing==2.4.7
    – alper
    Feb 6, 2022 at 14:13
  • worksforme... make sure your subordinate tools (pycodestyle, flake8, etc.) are not ignoring # noqa. see those respective tools' documentation.
    – nega
    Feb 6, 2022 at 19:28
  • How could they ignore # noqa? I have pycodestyle==2.8.0 and flake8==4.0.1 // I have also ~/.pylintrc and ~/.config/flake8 file. Don't they automatically ignore # noqa?
    – alper
    Feb 7, 2022 at 9:25
  • How can I prevent pycodestyle, flake8 from ignoring the # noqa? I have applied your solution but it did not detect # noqa to ignore warning messages
    – alper
    Feb 22, 2022 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.