sacuL
sacuL

Reputation: 51335

How to tell flake8 to ignore comments

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!

Upvotes: 72

Views: 96545

Answers (5)

vignesh garrapally
vignesh garrapally

Reputation: 29

Other answers do work but here's a alternate solution if you are using flake8 extension in VS Code

  1. Open the Command Palette (Cmd+Shift+P on macOS, Ctrl+Shift+P on Windows/Linux).

  2. Type "Preferences: Open Settings (JSON)" and select it.

  3. In the settings.json file that opens, add the following configuration:

    "flake8.args": [ "--ignore=E501" ]

This way, you don't have to neither create .flake8 secret file nor add # noqa: E501 at the end of each comment.

Upvotes: 2

bardia zamanian
bardia zamanian

Reputation: 47

You can ignore E501 when running flake8 by adding --ignore flag

flake8 --ignore E501

Upvotes: -1

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

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...

Upvotes: 33

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

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.

Upvotes: 27

sacuL
sacuL

Reputation: 51335

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.

Upvotes: 101

Related Questions