Skip to main content

flake8-length

Flake8 plugin for a smart line length validation.

pycodestyle linter (used in Flake8 under the hood by default) already has E501 and W505 rules to validate the line length. flake8-length provides an alternative check that is smarter and more forgiving.

What is allowed:

  • Long string literals.
  • Long URLs in strings and comments.
  • When the last word in a text doesn't fit a bit.

Motivation

From linux code style:

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.

Some modern languages even don't have this limitation:

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

However, it makes sense to keep some limit to guide developers and keep the alignment reasonable.

Uncle Bob analyzed line length in some popular Java project. The conclusion is it is usually about 45 on average, more than 97 is too much and exceptional.

Raymond Hettinger advises to keep it 90ish. The limit should be about 90 but with reasonable exceptions for when breaking the line would negatively affect the readability.

Kevlin Henney says even 80 is too generous. People read the code following one up-down flow, and breaking the flow with long lines makes the code harder to read.

If you ever had to break a text message to fit in the limit, you know why the plugin exists.

If you're about having as strict limits as possible, flake8-length is on your side. It's better to set 90 chars limit with a few reasonable exceptions rather than have 120 or more chars limit for everything.

Installation

Install:

python3 -m pip install --user flake8-length

And check if the plugin is detected by flake8:

flake8 --version

If it doesn't, flake8-length was installed in another python interpreter rather than flake8. You can find the right one:

head -1 $(which flake8)

Usage

  • If you're installed flake8-length and flake8 in the same environment, when you run flake8 it will run the plugin. Just give it a try.
  • pycodestyle has a few hard limits on lime length (E501 and W505), so these checks should be disabled to avoid conflicts with flake8-length.
  • The default soft limit is set using max-line-length option. It is 79 by default.

Configuration example (setup.cfg):

[flake8]
extend-ignore =
    E501,
    W505
max-line-length = 90

What the limit you should use? I'd say, as small as possible. Try to start with the default one (79) and if you feel it's not enough, extend it to 90. More is too generous.

Metadata

Release files for flake8-length 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-length 0.3.1
File Size Uploaded
flake8-length-0.3.1.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-length 0.3.1
File Interpreter ABI Platform
flake8_length-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / flake8-length-0.3.1.tar.gz

Download URL flake8-length-0.3.1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0ebd614c25361b54937335c9b143cc18562cd4da48024d48f79bf15ec453b510
BLAKE2b-256 checksum
How to use checksums
65f98a80b7caf5637ff74ae9b0666e2f130a2493b6a325ec6b41dd2159b65d60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.25.1

Release files / flake8_length-0.3.1-py3-none-any.whl

Download URL flake8_length-0.3.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73b5e974957575b87b5df55ddfe71e52f3806fb9d6a6b6b5cddceeff77800e4c
BLAKE2b-256 checksum
How to use checksums
dabb11d8a6f5e6f2c9a760a75a3faef9ff7c88c6527e9927cd97c26bdce5ce2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.25.1

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page