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

Smart code line length validation #1182

Closed
orsinium opened this issue Feb 24, 2020 · 9 comments
Closed

Smart code line length validation #1182

orsinium opened this issue Feb 24, 2020 · 9 comments
Labels
rule request Adding a new rule

Comments

@orsinium
Copy link
Collaborator

orsinium commented Feb 24, 2020

Rule request

Thesis

  1. Validate that code line not longer than N chars. Looks like 89.5 chars is a convention for now (one, two).
  2. Allow lines longer than N chars if the part after N chars contains only a string and optional comma.
  3. Maybe, even such lines should be limited. For instance, not to get longer than 120 chars. However, this part can be validated by flake8 max-line-length without any effort from us.

Example:

d = {
  # it is ok
  'one': 'some really long message that goes beyond 90 chars',
}

Reasoning

Statements longer than 80 columns will be broken into sensible chunks, unless exceeding 80 columns significantly increases readability and does not hide information. <...> However, never break user-visible strings such as printk messages, because that breaks the ability to grep for them.

I see a lot of Python code that does some awful breaks to fit long text messages into the project's line limit just because. However, it creates a lot of difficulties:

  1. Difficult to grep.
  2. Easy to miss a space on the string breaks.
  3. It doesn't make code more readable at all, even decreases readability. In most cases, I don't care if the ending of an error message goes outside of my screen.

After all, it's good to relax a rule that looks not so reasonable in the modern world:

Go has no line length limit. Don't worry about overflowing a punched card.

@orsinium orsinium added the rule request Adding a new rule label Feb 24, 2020
@sobolevn
Copy link
Member

Yes, there are issues with some long lines:

  • URL that are long
  • Text messages

@orsinium
Copy link
Collaborator Author

Yes, exactly. Sometimes it helps to move it into a separate var:

msg = "something went really-really wrong"
raise SomeReallyEnterpriseClassicJavaNameError(msg)

But sometimes the message itself is too long.

@ruwaid4
Copy link
Contributor

ruwaid4 commented Apr 2, 2020

Hi again, me and my friend would like to try and take on this issue. What's the believed difficulty level for this one?

@sobolevn
Copy link
Member

sobolevn commented Apr 2, 2020

This one is not clear to me yet. You can select any issue from: https://github.com/wemake-services/wemake-python-styleguide/milestone/16

It would be easier 🙂

@orsinium
Copy link
Collaborator Author

flake8-bugbear has B950: line too long rule.

https://github.com/PyCQA/flake8-bugbear/blob/5e541a8e7036cb43c97c86295eeb2c6498980f01/bugbear.py#L44-L52

The idea is a bit similar: to relax a bit line length limit.

@orsinium
Copy link
Collaborator Author

orsinium commented Jun 1, 2020

Also, if the line-length limit is violated by # noqa... or # pragma... comment. You can't just move it on a separate line, the linter says where the comment must be put.

UPD: also, # {N,W,E,R}: comments from pytest-mypy-testing and pytest-mypy-plugins

@orsinium
Copy link
Collaborator Author

orsinium commented Nov 6, 2020

The related task for a go linter: walle/lll#11

@orsinium
Copy link
Collaborator Author

orsinium commented Feb 5, 2021

I decided to experiment with it a bit and made a separate small plugin:
https://github.com/orsinium-labs/flake8-length

@sobolevn
Copy link
Member

sobolevn commented Feb 5, 2021

Awesome! 👍

@sobolevn sobolevn closed this as completed Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

3 participants