Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore line-too-long for long URLs #2178

Closed
demurgos opened this issue Jun 11, 2018 · 8 comments
Closed

Ignore line-too-long for long URLs #2178

demurgos opened this issue Jun 11, 2018 · 8 comments

Comments

@demurgos
Copy link

demurgos commented Jun 11, 2018

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:
    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.
    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),

    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.

@PCManticore
Copy link
Contributor

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.

@demurgos
Copy link
Author

demurgos commented Jun 11, 2018

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

@tedivm
Copy link

tedivm commented Sep 20, 2019

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*$

@DaveParr
Copy link

DaveParr commented Jan 7, 2020

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

jgstew added a commit to jgstew/bigfix_prefetch that referenced this issue Apr 7, 2020
@danieltuzes
Copy link

danieltuzes commented Dec 3, 2020

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.

@swirle13
Copy link

swirle13 commented Oct 7, 2021

Turned out my issue was the default of ^\s*(# )?<?https?://\S+>?$ expected a max of one space after the octothorpe (# )? and not any amount of spaces after it as I had a TODO block with each line commented and the items were spaced, i.e.

# TODO:
#   do X
#   https://github.com/aws/chalice/issues/927#issuecomment-411080600

the last line being 82 chars long, exceeding the 80 character limit. This was fixed by putting the TODO block in triple double quotes or updating the regex to ^\s*(#\s*)?<?https?://\S+>?$

@Pierre-Sassoulas
Copy link
Member

disable-next was also introduced in the latest pylint

@adam-grant-hendry
Copy link
Contributor

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

Things may have changed since you originally posted, but this is now in the standard checkers here:

https://pylint.pycqa.org/en/latest/user_guide/configuration/all-options.html?highlight=--ignore-long-lines#ignore-long-lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants