How to tell flake8 to ignore comments

Question:

I’m using flake8 in emacs in order to clean up my python code. I find it annoying to have my comments flagged as errors (E501 line too long (x > 79 characters)). I’m wondering if anyone knows a way to kindly ask flake8 to ignore comments, both single and multi-line, but still let me know when my non-comment lines are too long?

Thanks in advance!

Asked By: sacuL

||

Answers:

I’ve figured out a possible solution to this, but there might be something better. If you write a comment that will raise an E501 error, i.e. it is too long, you can append that line with # noqa: E501, and flake8 will ignore it. For example:

# This is a really really long comment that would usually be flagged by flake8 because it is longer than 79 characters

would usually raise an E501, but

# This is a really really long comment that would usually be flagged by flake8 because it is longer than 79 characters # noqa: E501

will not.

documented here.

Answered By: sacuL

You can change the list of codes ignored by flake8 using a configuration file. For example, in your project directory create a file named .flake8 with the following content:

[flake8]
per-file-ignores =
    # line too long
    path/to/file.py: E501,

This may be easier than using # noqa comments.

Answered By: Eugene Yarmash

Using an inline comment # noqa: E501 should ignore this issue for you.

If you have a multi-line comment, you can ignore inline at the end of the multi-line string like so:

def do_something():
    """
    Long url as a reference.

    References:
        1. https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling
    """  # noqa: E501
    ...

If you have a long inline comment, you can ignore by marking the comment with # noqa: E501 the same way:

# Reference: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling  # noqa: E501

^ Weirdly enough, you need to add the 2nd # for it to work…

Answered By: Yaakov Bressler
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.