How to let pyflakes ignore some errors?
Asked Answered
H

2

9

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?

Hibernal answered 7/6, 2015 at 20:56 Comment(4)
@ZdaR I like long linesHibernal
Do not ignore E101, as you'll almost certainly get errors when trying to run your code. PEP-8 strongly recommends using 4 spaces.Alluring
Also, please read the rest of PEP-8 while you're there. It's the Python style guide, and many projects require conformance to it, aside from the fact that most of what's contained in it is just good practice in general. A couple characters over 80 is fine, but much more than that and you're doing something wrong.Alluring
@Alluring sometimes i just have long function names : flag, message = FacebookUserController.AddFBUserToDB(iOSUserId, fburl, fbsecret, code) 92 charactersHibernal
A
9

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.
Alluring answered 7/6, 2015 at 22:19 Comment(0)
S
1

As others suggest, possibly heed the warnings. But in those cases where you can't, you can add # NOQA to the end offending lines. Note the two spaces before the # as that too is a style thing that will be complained about.

And if pyflakes is wrapped in flake8 that allows ignoring by specific errors.

For example in a file in the project put or add to tox.ini:

[flake8]
exclude = .tox,./build
filename = *.py
ignore = E501,E101

This is possibly a duplicate with How do I get Pyflakes to ignore a statement?

Seaward answered 7/6, 2015 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.