Pylint: Ignore line-too-long for long URLs

Created on 11 Jun 2018  路  5Comments  路  Source: PyCQA/pylint

This is a feature request.
I have doc comments with URLs in them. Some of these URLs are long (for example canonical Github URLs, with the full commit hash). I want to ignore the line-too-long warning for these lines.

Steps to reproduce

  1. Write some code with a doc comment containing a long URL

    def parse_proc_stat(data):
       """
       Parses the content of a `proc/[pid]/stat` file.
    
       :see: https://linux.die.net/man/5/proc
       :see: https://github.com/torvalds/linux/blob/68abbe729567cef128b2c2141f2ed2567f3b8372/fs/proc/array.c#L528
       """
       match = PROC_STAT_PATTERN.match(data)
       ...
       return result
    
  2. Run pylint on the script, with the default config

Current behavior

I get a line-too-long error.

Expected behavior

Have a good way to locally disable this error (see below).

pylint --version output

1.9.1


The line-too-long error is not helpful in this case. The current solutions are either noisy or too extreme.

Current solutions

  1. Disable the line-too-long error globally. Either with the CLI flag or with a # pylint: disable=line-too-long at the top of the file. This is too extreme: I still want to catch code lines that are too long.
  2. Disable the error with a trailing directive. This seems to work, even inside doc strings, but trips up other tools using the doc string and makes the line even longer:
    python def parse_proc_stat(data): """ :see: https://github.com/torvalds/linux/blob/68abbe729567cef128b2c2141f2ed2567f3b8372/fs/proc/array.c#L528 # pylint: disable=line-too-long """ return result
  3. Locally disable then re-enable the error by wrapping the doc-string with a pair of pylint: disable=... and pyling: enable=... directives. This solution does not modify the doc-string or over-extend the line with the URL, but it requires to add two lines of Pylint directives and is very error prone: if you forget/accidentally remove the enable directive, the content will not be checked.
    python def parse_proc_stat(data): # pylint: disable=line-too-long """ :see: https://github.com/torvalds/linux/blob/68abbe729567cef128b2c2141f2ed2567f3b8372/fs/proc/array.c#L528 """ # pylint: enable=line-too-long return result

Proposed solutions:

I used other linters in other languages (Javascript, Typescript, C++) and I saw two general ways to handle this kind of issues:

  • disable-next directive. This is the mirror image of disable: it ignores errors for the next element instead of the previous one. It allows you to write the directive comment above the failing element. This is also mentioned in this issue. This solution can be applied to other checks, it would also allow you to avoid lines mixing both code and comments (code style),
    python def parse_proc_stat(data): # pylint: disable-next=line-too-long """ :see: https://github.com/torvalds/linux/blob/68abbe729567cef128b2c2141f2ed2567f3b8372/fs/proc/array.c#L528 """ return result
    You still have to prefix the failing element, but it's less noisy or error prone.
  • line-too-long-whitelist-regex option (better names are welcome). Configure the line-too-long check with a regex that lets you whitelist some lines. If the regexp matches the line, do not emit the error. For example, I could use a regexp matching URLs but someone else could write its own regexps (I mainly found it used for URLs in other tools). The regexp may fail to distinguish some cases (URL in a doc/comment VS in a string literal) but it's usually good-enough.

Most helpful comment

I'm updating this issue because it's the first thing that shows up for the google search "pylint ignore line for long strings".

This regex will ignore

  • URLs
  • Pure string assignment lines (item = "long string")
  • Long strings in lists.
^\s*(# )?<?https?://\S+>?$|^\s*(\w*\s*=\s*)?(\"|\').*(\"|\'),?\s*$

All 5 comments

Have you tried --ignore-long-lines? It's an option that receives a regex that lets you whitelist particular long lines that match that regex.

disable-next sounds like a good suggestion, but I'm not sure we'll get to have it too soon.

Thank you for your help: I haven't found --ignore-long-lines while searching for a solution.

For references, here is the documentation link: http://pylint.pycqa.org/en/latest/technical_reference/features.html#format-checker-options

I'm updating this issue because it's the first thing that shows up for the google search "pylint ignore line for long strings".

This regex will ignore

  • URLs
  • Pure string assignment lines (item = "long string")
  • Long strings in lists.
^\s*(# )?<?https?://\S+>?$|^\s*(\w*\s*=\s*)?(\"|\').*(\"|\'),?\s*$

If you start with the boiler plate via pylint --generate-rcfile a single url on a line is currently default: http://pylint.pycqa.org/en/latest/technical_reference/features.html#format-checker-options

I'm updating this issue because it's the first thing that shows up for the google search "pylint ignore line for long strings".

This regex will ignore

* URLs

* Pure string assignment lines (`item = "long string"`)

* Long strings in lists.
^\s*(# )?<?https?://\S+>?$|^\s*(\w*\s*=\s*)?(\"|\').*(\"|\'),?\s*$

How can it be added to pylint? I added it to the .pylintrc like

[FORMAT]

# Regexp for a line that is allowed to be longer than the limit.
ignore-long-lines=^\s*(# )?<?https?://\S+>?$|^\s*(\w*\s*=\s*)?(\"|\').*(\"|\'),?\s*$

but it didn't help. Btw, the default setting ignore-long-lines=^\s*(# )?<!--?https?://\S+-->?$ should do the job as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TBoshoven picture TBoshoven  路  3Comments

PCManticore picture PCManticore  路  3Comments

glmdgrielson picture glmdgrielson  路  3Comments

Hubro picture Hubro  路  3Comments

mrginglymus picture mrginglymus  路  3Comments