This post mainly serves as a reminder to myself, since I have had to deal with this one-too-many times and have gotten frustrated with how poorly this is documented online currently.

The Problem: ruff format does not auto-fix E501 line too long violations

Ruff is a wonderful linter + formatter, with one major shortcoming that often irks me: it is overly cautious when auto-formatting Python to meet line-length limits, to the point where it too often leaves violations as “unfixable”.

Consider the following:

# lots of args
print("Lorem", "ipsum","Lorem", "ipsum","Lorem", "ipsum","Lorem", "ipsum","Lorem", "ipsum","Lorem", "ipsum","Lorem", "ipsum","Lorem", "ipsum")

# plain string
some_var = "Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum"

# nested, in dict
some_list = [
    {
        "description": "Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum"
    }
]

Playground

With the E501 line-too-long rule enabled (and the line-length setting configured), all three text strings in the above example will be flagged with line-too-long / E501 Line too long rule violations if we run ruff check against our code.

However, running ruff format against the same code will not fix all three - it will only fix the first violation. That is because while the first violation can be fixed without requiring splitting string literals, the other ones cannot - and this is not something that ruff currently handles.

Main issue I have found tracking this: Formatter: string_processing preview style

This can be extremely annoying because, from a layperson’s perspective that doesn’t necessarily care about the extreme edge-cases that come with string-literal splitting, these seem like they should be auto-fixable via a formatter and not require hand-editing.

And, indeed, they are!

The Fix

Other than hand-editing the instances that ruff refuses to auto-format, the current best way I have found to handle this is to combine black’s auto-formatter with ruff, having black make the first pass and then running ruff last.

Unfortunately, the incantation to get this right and get much more aggressively auto-fixed line-length rule violations is a bit much:

# Run black first
black \
	--enable-unstable-feature=string_processing \
	--preview \
	--line-length 120 \
	${FILES}

# Run ruff afterwards
ruff format ${FILES}

To get this to happen automatically as you save files in your IDE, it is a bit more complicated. In VSCode, this is possible to do by setting black as the defaultFormatter and the ruff fixer as a codeActionsOnSave. Example:

{
"[python]": {
	"editor.defaultFormatter": "ms-python.black-formatter",
	"editor.insertSpaces": true,
	"editor.formatOnSave": true,
	"editor.codeActionsOnSave": {
		"source.fixAll.ruff": "explicit"
	}
},
"black-formatter.args": [
	"--line-length",
	"120",
	"--skip-string-normalization",
	"--preview",
	"--enable-unstable-feature=string_processing",
]
}

Results may vary, VS Code settings often conflict

Of course, this is just one of many ways to automate this; you could also use pre-commit hooks, shell scripting aliases, etc.

Anyways, hope this helps someone - most likely myself when I forget this in a year and need it again 😂