Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let pyflakes ignore some errors?

I am using SublimePythonIDE which is using pyflakes. There are some errors that I would like it to ignore like:

(E501) line too long
(E101) indentation contains mixed spaces and tabs

What is the easiest way to do that?

like image 566
user3571278 Avatar asked Jun 07 '15 20:06

user3571278


People also ask

How do I ignore warnings in flake8?

There are two ways to ignore the file: By explicitly adding it to our list of excluded paths (see: flake8 --exclude ) By adding # flake8: noqa to the file.


1 Answers

Configuring a plugin in Sublime almost always uses the same procedure: Click on Preferences -> Package Settings -> Plugin Name -> Settings-Default to open the (surprise surprise) default settings. This file generally contains all the possible settings for the plugin, usually along with comments explaining what each one does. This file cannot be modified, so to customize any settings you open Preferences -> Package Settings -> Plugin Name -> Settings-User. I usually copy the entire contents of the default settings into the user file, then customize as desired, then save and close.

In the case of this particular plugin, while it does use pyflakes (as advertised), it also makes use of pep8, a style checker that makes use of the very same PEP-8 official Python style guide I mentioned in the comments. This knowledge is useful because pyflakes does not make use of specific error codes, while pep8 does.

So, upon examination of the plugin's settings file, we find a "pep8_ignore" option as well as a "pyflakes_ignore" one. Since the error codes are coming from pep8, we'll use that setting:

"pep8_ignore": [ "E501", // line too long
                 "E303", // too many blank lines (3)
                 "E402" // module level import not at top of file
               ]

Please note that codes E121, E123, E126, E133, E226, E241, E242, and E704 are ignored by default because they are not rules unanimously accepted, and PEP 8 does not enforce them.


Regarding long lines:

Sometimes, long lines are unavoidable. PEP-8's recommendation of 79-character lines is based in ancient history when terminal monitors only had 80 character-wide screens, but it continues to this day for several reasons: it's backwards-compatible with old code, some equipment is still being used with those limitations, it looks good, it makes it easier on wider displays to have multiple files open side-by-side, and it is readable (something that you should always be keeping in mind when coding). If you prefer to have a 90- or 100-character limit, that's fine (if your team/project agrees with it), but use it consistently, and be aware that others may use different values. If you'd like to set pep8 to a larger value than its default of 80, just modify the "pep8_max_line_length" setting.

There are many ways to either decrease the character count of lines to stay within the limit, or split long lines into multiple shorter ones. In the case of your example in the comments:

flag, message = FacebookUserController.AddFBUserToDB(iOSUserId, fburl, fbsecret, code)

you can do a couple of things:

# shorten the module/class name
fbuc = FacebookUserController 
# or
import FacebookUserController as fbuc
flag, message = fbuc.AddFBUserToDB(iOSUserId, fburl, fbsecret, code)
# or eliminate it all together
from FacebookUserController import AddFBUserToDB
flag, message = AddFBUserToDB(iOSUserId, fburl, fbsecret, code)
# split the function's arguments onto separate lines
flag, message = FacebookUserController.AddFBUserToDB(iOSUserId,
                                                     fburl, 
                                                     fbsecret, 
                                                     code)
# There are multiple ways of doing this, just make sure the subsequent
# line(s) are indented. You don't need to escape newlines inside of 
# braces, brackets, and parentheses, but you do need to outside of them.
like image 191
MattDMo Avatar answered Sep 28 '22 07:09

MattDMo