Skip to main content

Fixed: "E501 Line too long (107 > 100)" in Ruff After ruff format

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

ruff check stopped on one line of a small script: E501 Line too long (123 > 100). Running ruff format changed the file but not the verdict: E501 Line too long (107 > 100). The line was a long SQL string, and the formatter doesn't split strings. Here's how I worked that out, and the fix, with real output from Ruff 0.17.6.

What I Was Doing​

I was writing an example script that creates a SQLite table, in a uv project that checks its code with Ruff. The project's pyproject.toml allows lines of up to 100 characters and turns on the E and F rules:

pyproject.toml
[tool.ruff]
line-length = 100

[tool.ruff.lint]
select = ["E", "F"]

The script puts the whole CREATE TABLE statement in one string:

items_table.py
import sqlite3

connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE items (quantity INTEGER, price REAL, name TEXT, note TEXT, photo BLOB, in_stock INTEGER)")
print("Table created.")

What I Expected​

Expected output
All checks passed!

What I Got​

Terminal
uv run ruff check
Output
E501 Line too long (123 > 100)
--> items_table.py:4:101
|
3 | connection = sqlite3.connect(":memory:")
4 | connection.execute("CREATE TABLE items (quantity INTEGER, price REAL, name TEXT, note TEXT, photo BLOB, in_stock INTEGER)")
| ^^^^^^^^^^^^^^^^^^^^^^^
5 | print("Table created.")
|

Found 1 error.

A terminal where uv run ruff check reports E501 Line too long (123 > 100) at items_table.py line 4, column 101, under the connection.execute line with the long CREATE TABLE string, and Found 1 error.

Line 4 is 123 characters long, and the carets mark everything past column 100.

What I Checked​

1. What E501 checks. Ruff's E501 rule page: it "Checks for lines that exceed the specified maximum character length." Its default is 88 characters, "though that limit is configurable via the line-length setting." This project sets 100, so 123 is over.

2. Whether ruff's default rules include it. They don't. uv run ruff check --isolated --line-length 100 items_table.py, which ignores the project's settings, printed All checks passed! for the same file. E501 runs here because the project selects the E rules.

3. Whether ruff format fixes it. The formatter wraps long lines, so I ran it, then checked again:

Terminal
uv run ruff format
uv run ruff check
Output
1 file reformatted
E501 Line too long (107 > 100)
--> items_table.py:5:101
|
3 | connection = sqlite3.connect(":memory:")
4 | connection.execute(
5 | "CREATE TABLE items (quantity INTEGER, price REAL, name TEXT, note TEXT, photo BLOB, in_stock INTEGER)"
| ^^^^^^^
6 | )
7 | print("Table created.")
|

Found 1 error.

A terminal where uv run ruff format reports 1 file reformatted, then uv run ruff check still reports E501 Line too long (107 > 100) at line 5, where the long string now sits on its own line inside connection.execute.

The formatter moved the string onto its own line, but left the string whole, and that line alone is 107 characters. Ruff's line-length setting says as much: "While the formatter will attempt to format lines such that they remain within the line-length, it isn't a hard upper bound, and formatted lines may exceed the line-length."

Why It Happens​

The SQL string is 103 characters with its quotes. Indented by 4 spaces it's 107, whatever the formatter does around it. Ruff's formatter changes the layout of code, but it doesn't cut a string in two, so the line can't get under 100 without a change to the string itself.

The Fix​

Split the string into two strings, side by side inside the parentheses. Python joins adjacent string literals into one. Its language reference: "Multiple adjacent string or bytes literals, possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation".

items_table.py
import sqlite3

connection = sqlite3.connect(":memory:")
connection.execute(
"CREATE TABLE items (quantity INTEGER, price REAL, name TEXT,"
" note TEXT, photo BLOB, in_stock INTEGER)"
)
print("Table created.")

The fixed items_table.py in a macOS-style code window: connection.execute receives the CREATE TABLE statement as two adjacent strings on two lines, the second starting with a space before note TEXT.

  • The first part ends after name TEXT,, the second starts with a space, so the joined SQL is exactly the original statement.
  • No + is needed: the two strings are joined when Python reads the file.

After the Fix​

Terminal
uv run ruff check
uv run python items_table.py
Output
All checks passed!
Table created.

A terminal where uv run ruff check prints All checks passed!, and uv run python items_table.py prints Table created.

Ruff passed, and the script still created the table. uv run ruff format --check printed 1 file already formatted, so the formatter keeps the split as it is.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting ruff format to fix every E501It doesn't split strings: the error stayed, at (107 > 100)Split the long string yourself
Splitting with no space at the join, "... name" "TEXT, ..."No error at all: SQLite created a column called nameTEXT with no typeKeep the space, at the end of one part or the start of the next

Troubleshooting​

SymptomLikely causeFix
E501 is still reported after ruff formatThe line holds a string longer than the limitSplit the string into adjacent literals
E501 appears in one project but not anotherOnly projects that select the E rules run itCheck [tool.ruff.lint] in pyproject.toml

FAQ​

Can I allow one long line instead?​

Yes. A # noqa: E501 comment at the end of the line tells Ruff to skip it: with it, ruff check printed All checks passed! for the original line. Use it for lines that really can't be split.

Why does the message show two numbers?​

The first is the line's length, the second the limit: (123 > 100) is a 123-character line in a project that allows 100.

Conclusion​

The long SQL string kept its line at 107 characters even after ruff format, because the formatter doesn't split strings. Splitting it into two adjacent literals brought every line under 100, and ruff check passed.

Additional Resources​