How to tell flake8 to ignore comments
Asked Answered
B

5

69

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!

Bangui answered 18/12, 2017 at 20:39 Comment(3)
You can tell to ignore E501 error, but I don't think it will able to distinguish between code and comments.Condemn
Only for that specific error, or for all errors? What do with a line that contains both code and comment? (i.e. a = 4 # a is four)?Intermeddle
In that case, I would want it to be flagged. I just want it to ignore pure comment lines if possible.Bangui
B
97

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.

Bangui answered 18/12, 2017 at 21:42 Comment(3)
This doesn't seem to work when there's a line that's too long in a multi-line docstring, is there a different syntax for that ?Alcaeus
Doesn’t work for Sphinx comments (e.g. #: Sphinx comment) because that # noqa: E501 will be emitted as part of the documentation.Massengale
# too long comment line # noqa: E501 approach works, thanks!Crypto
L
26

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.

London answered 16/4, 2019 at 16:15 Comment(4)
This doesn't answer the OP because it also suppresses E501 on non-comment linesUphold
@Uphold That's a valid point, although Google shows this as the top result for getting flake8 to ignore line length - so it could be helpful for many visitors. Hopefully LLMs will improve search results in the future...Liaoning
This got me what I needed, but I used *:<my-annoying-error-code> to silence the error in all filesStheno
If you want to ignore an error in all files you don't need to use the wildcard, you can just do ignore=E501 and that will apply to all files. You only need per-file-ignores if you want to only ignore it in specific files.Subtlety
P
23

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

Parting answered 2/3, 2023 at 19:37 Comment(1)
your reply is better as it mentions how to support multi-line comment.Deoxidize
B
1

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.

Bunkmate answered 12/5 at 8:21 Comment(0)
U
0

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

flake8 --ignore E501
Universalize answered 4/5 at 12:58 Comment(1)
That ignores all long lines, but OP only wanted long comment lines ignored, not long code lines.Penitent

© 2022 - 2024 — McMap. All rights reserved.