55

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!

Jess
  • 58
  • 5
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • You can tell to ignore E501 error, but I don't think it will able to distinguish between code and comments. – sashk Dec 18 '17 at 20:41
  • 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`)? – Willem Van Onsem Dec 18 '17 at 20:43
  • 1
    In that case, I would want it to be flagged. I just want it to ignore pure comment lines if possible. – sacuL Dec 18 '17 at 20:44

3 Answers3

80

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.

Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • 2
    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 ? – baxx Jul 25 '22 at 10:15
  • 1
    Doesn’t work for Sphinx comments (e.g. `#: Sphinx comment`) because that `# noqa: E501` will be emitted as part of the documentation. – Jens Nov 10 '22 at 02:15
  • `# too long comment line # noqa: E501` approach works, thanks! – storenth Dec 27 '22 at 09:05
21

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.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 16
    This doesn't answer the OP because it also suppresses E501 on non-comment lines – leoll2 Dec 14 '20 at 19:51
  • @leoll2 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... – Jonathan Jul 20 '23 at 13:35
6

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

Yaakov Bressler
  • 9,056
  • 2
  • 45
  • 69