4

I would like to use a specific linter for python files in spacemacs. A coworker sent me an emacs code-snippet that should solve my problem, but doesn't seem to be loaded.

(add-hook 'python-mode '(lambda ()
                           (semantic-mode 1)
                           (setq flycheck-checker 'python-pylint
                                 flycheck-pylintrc "~/.pylintrc")))

I know it isn't loaded because when I load a (very poorly written file) it has the following error:

Warning (flycheck): Syntax checker python-flake8 reported too many errors (826) and is disabled.

I am trying to use pylint with our specific lintrc file to get around this problem.

Edit: C-h v flycheck-checker says the value is nil. Also, it doesn't seem like I can set that variable at all interactively (with M-x set-variable) Edit2: I can't seem to even disable flycheck, so if anyone knows how to do that, it would be great.

  • Which variable are you trying to set? – elethan Jan 4 '16 at 19:07
  • 1
    It sounds like you need to inscrease flycheck-checker-error-threshold. You can check its value with C-h v flycheck-checker-error-threshold RET. Mine is set to 400 by default, and it looks like the file that you are trying to access has more errors than that. Try adding flycheck-checker-error-threshold 900 under flycheck-pylintrc "~/.pylintrc" in your configuration snippet (although this will most likely make the buffer for that file a bit slow...) – elethan Jan 4 '16 at 19:12
  • 1
    also, these need to be added to python-mode-hook not python-mode. I will combine this advice in an answer. – elethan Jan 4 '16 at 19:33
  • 1
    Ok, I did those things and also had to move them to user-init (user-config didn't work). It now seems to be working -- thanks! – vitiral Jan 4 '16 at 19:38
6

Replace your relevant configuration with this:

(add-hook 'python-mode-hook (lambda ()
                               (flycheck-mode 1)
                               (semantic-mode 1)
                               (setq flycheck-checker 'python-pylint
                                     flycheck-checker-error-threshold 900
                                     flycheck-pylintrc "~/.pylintrc")))

The specific error you mention was being caused because your file has a lot of errors, and by default the variable flycheck-checker-error-threshold is set to 400 (while your file seems to contain 826).

By setting this really high, you will avoid this error, but I assume you will get a very slow buffer. With files like this it might be better to not use flycheck and instead use something that doesn't do its checking in real time (I use the line (setq python-check-command "flake8") in my configuration and syntax is checked each time the file is saved. I don't use pylint but (setq python-check-command "pylint") should work too, as long as you have pylint installed, and should use the same .pylintrc (in fact, you can try the example above without setting flycheck-pylintrc, because I think emacs will look for this file in your home folder by default).

This configuration snippet will also enable flycheck-mode by default so remove the line (flycheck-mode 1) if you don't want this. At any rate, you can run M-x flycheck-mode to toggle this mode on and off.

Finally, when you are using add-hook make sure the next argument is a mode hook, not the mode itself (e.g., 'python-mode-hook not 'python-mode) Happy configuring!

Edit:

Also, note that I have removed the ' before the lambda from the OP's original configuration snippet. As pointed out in the comments by @lunaryorn, lambda's are "self-quoting" and so the quote is at best redundant, and at worst...well, I will direct you to these posts that seem useful on the subject (I will be reading up on this topic myself):

https://stackoverflow.com/a/20948567/3642398

http://www.emacswiki.org/emacs/QuotedLambda

Also, if you were using the snippet pre-edit (i.e., with the quote), have no fear: it will still work as expected without it.

  • 2
    Please do not quote lambdas. Although it makes no difference here, but can cause serious harm with lexical scoping and is considered bad style. – lunaryorn Jan 4 '16 at 21:58
  • 1
    @lunaryorn Thank you for the tip! I was trying to modify the OP's original configuration code as little as possible, but I will admit that I did not know that about quoting lambdas. In this case, removing the quote will give the same result, right? At least in my tests it seemed to have no effect whether I have the quote or not, but I want to make sure if I remove it in my answer (with a note of explanation of course), that it will not have any unforeseen consequences. – elethan Jan 4 '16 at 22:04
  • 1
    @elethan No, it'll not have any adverse consequences; on the contrary, the quote could cause those (albeit it does not in this case). ☺️ Lambas are "self-quoting", and never need to be quoted explicitly, nor should they. – lunaryorn Jan 4 '16 at 22:07
0

You can simply disable the python-flake8 checker. This will make the next checker which is python-pylint to be used. You can do this by using the command flycheck-disable-checker.

Before this verify if the pylint is configured by using command flycheck-verify-setup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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