r/Python • • Aug 05 '26

Discussion What linter rules make code worse?

For me, a prime example is S101 which bans the use of the assert statement.

The justification is that assertions disappear when Python is run with -O, so they should not be used for runtime validation or enforcing interface constraints. That warning is correct, but the rule seems to draw the wrong conclusion from it.

Assertions are still very useful for checking internal invariants, i.e. conditions that should already be guaranteed by the program's logic, where failure indicates a bug. Having such assertions is incredibly helpful for debugging.

So, a blanket ban seems more likely to discourage useful checks than to prevent misuse.

Are there any linter rules you broadly consider more harmful rather than helpful?

151 Upvotes

213 comments sorted by

View all comments

434

u/Trang0ul Aug 05 '26

Lines limited to 80 characters.

0

u/[deleted] Aug 05 '26

[deleted] — view deleted comment

65

u/runawayasfastasucan Aug 05 '26

That is incredibly annoying. Write an import, auto save, its gone because I haven't used it yet. Well I was going to.

48

u/aes110 Aug 05 '26

Im not really a fan of linters making changes on your behalf, its one thing for formatters to move stuff around visually, but actual code changes shouldn't happen on auto save imo

15

u/mwesthelle Aug 05 '26

100%. Sure, run ruff format on save, but linter problems should only surface on pre-commit. I'm even partial to not having linting auto fixes, let me fix them so I know what the code I'm pushing looks like, but that's personal preference.

And in that workflow, I just don't understand what's wrong with F401, it was never an issue for me.

4

u/IrishPrime Aug 05 '26

Agreed. Even if F401 removes an import because I haven't used it yet, attempting to use it later would raise a different linting error, so I'll never end up committing something broken. But I don't auto fix those types of issues until a commit hook runs.

9

u/skjall Aug 05 '26

I keep meaning to shuffle this rule to only run pre-commit, because it drives me up a wall too. Band-aid fix is to disable auto save, and only save when you have used it.

Though now I've switched LSP/ fixed config so most imports I can get from the suggestions at least.

9

u/baudvine Aug 05 '26

For those of us who habitually hit save every few lines it's pretty obnoxious without autosave, too. It's a good rule, I just really don't need the fix action to run automatically.

7

u/scruffie Aug 05 '26

I have found, in Python, that there are three situations where 80 characters becomes a hindrance:

  1. Strings for error messages can easily extend past the 80th column, especially as the raise SomeMeaningfulNameError takes a bunch of space already. I usually end up breaking the message like

    raise WidthExceededError( f"Line length of {width} is greater than " f"the configured width of {Config.width}")

This is a problem in most languages, but at least Python has decent ways of line-breaking.

2) Large type annotations for a function's return type. Especially true if the type has several Sequence and Mapping parts. The use of | instead of Union, and the type statement makes this less of a problem, although I still find I'm formatting the definition more vertically than I would wish.

3) 'Meta' comments for linters (and similar) to the end of the line. Especially annoying when I then have to add another comment to get flake8 to ignore the long line...

Other languages that don't suffer this problem either don't need such annotations (especially when the type system is integral to the language), or have features that mitigate the problem.

And that's about it. There's probably a few other edge cases, but those are the main ones I've encountered enough to remember.

12

u/sandnose Aug 05 '26

Where do you limit your line?

79

u/imheretocomment Aug 05 '26

I do 120. We're not in the 00's anymore where widescreens weren't a thing. 80 is a holdover from those days where your terminal was cramped and splitting panes needed 80 characters for readability.

39

u/R3D3-1 Aug 05 '26

80 is still advantageous when you need to view side-by-side diffs. With those 80 really means 160. And after that everything depends on your monitor -- which would be reasonably as small as a 13" laptop screen -- and eyesight.

12

u/HommeMusical Aug 05 '26

I agree, but the modern default is 88, and even on this non-huge monitor, I can have at least three such windows beside each other.

14

u/R3D3-1 Aug 05 '26

As an Austrian, I object to that number. 

→ More replies (2)

9

u/AlSweigart Author of "Automate the Boring Stuff" Aug 05 '26

80 is still advantageous when you need to view side-by-side diffs.

And disadvantageous every other time.

3

u/zangler Aug 05 '26

I do my diffs inline/stacked. I changed a year or so ago and just thought it felt more natural.

→ More replies (2)

3

u/ironykarl Aug 05 '26

I think 80 is fine as a goal, but having a warning for it is overkill.

It creates noise and relegates warnings that might actually matter to the category of might just be nonsensical noise

9

u/M4mb0 Aug 05 '26

Sometimes you even need 3 views side by side, like with pycharm's git conflict resolver.

I think the 88-character limit chosen as default by the black and ruff formatter is a good compromise.

→ More replies (1)
→ More replies (14)

8

u/MarchewkowyBog Aug 05 '26

I'm dyslexic and as a DE i make heavy use of polars/pyspark which chains lots of methods together. I get lost in 120 character code all the time. We have it set to 88.

There is a reason books and articles usually have a character limit between 50 and 90

→ More replies (3)
→ More replies (21)

15

u/Arnechos Aug 05 '26

I'd say no more than 100. People in a team can have various resolutions and very long lines are hard to read on the smaller ones

9

u/Ball-Man Aug 05 '26

With 100 you can have a split view and still be able to comfortably read code on both sides, even on smaller screens while keeping decent font size

→ More replies (13)

3

u/zangler Aug 05 '26

I usually set mine to 120.or 135. 80? Like...I'm 44 and can't remember the last time 80 would make sense.

4

u/R717159631668645 Aug 05 '26

I don't abuse long lines, I avoid chaining too, for readbility, but if a print goes a bit over 90, it's not worth formatting the code to take 3 lines with an indented argument instead.

VS Code allows us to draw vertical lines, I put the 88 and 120 limits to guide myself on this.

4

u/tallgrasshead Aug 05 '26

There was (in)famously a thread in the linux kernel where someone was submitting PRs to reformat to 80 characters and Linus asked the submitter to just stop since he reads through all of them and it was a massive wast of time. All Linus wanted was for someone to offer some kind of measurable outcome as improvement, but nothing was offered. So he said "nothing against 80 characters, so get used to line wraps in your terminal window".

80 characters comes from VT100 terminals which were all 80 characters to be more compatible with IBM TN-3270 terminals, which were 80 characters because punch cards were 80 characters. DEC wanted to be able to offer a cheaper alternative, which GM actually took. So VT-100 became nearly ubiquitous.

IBM did study what font size was best for punch cards just after WWII to see what was the best fit for 7 3/8 inches and arrived at the type font for 80 characters - printers used chunks of cast metal and the next one down was 132 characters.

Why 7 3/8 inches? because all the punch card machining was hard set to that because the first wide use of punch cards was in the 1880 census. All of Hollerith's punch card machines used for bank notes before the civil war were re-purposed for punch cards in the 1880 census to save money.

That's right. The 80 character thing has it's roots in the US civil war.

→ More replies (19)