The Problem:
Given a Python project, the latest version of ruff is configured for linting and formatting with specific settings in pyproject.toml
. When using the ruff check
command with the autofix feature (--fix
), ruff identifies lines that exceed the specified line length (E501
errors) but does not automatically format the code to wrap to the next line. The goal is to enable ruff to fix this issue or determine if it is not currently possible.
The Solutions:
Solution 1: Ruff doesn’t autofix line-too-long violation
Ruff’s current version, v0.0.280, does not have the ability to autofix line-length violations (E501
). However, Ruff has released a Python formatter in alpha state as part of v0.0.289. This new feature allows for autofixing line-length violations.
As a workaround, you can use a combination of Black and Ruff, tailoring them to meet your specific project requirements.
Solution 2: Autofix Disabled For `E501` Lint Rule
As of today’s Ruff autoformatter, not every lint rule can be autofixed, and `E501` is one of them. Ruff does not enforce autofixing rules that overlap with the use of a formatter, as `E501` inherently does. Here’s what you can do:
- Use Ruff’s formatter (or Black) to autoformat
E501
issues according to theline-length
setting, where possible. - Ignore the
E501
lint rule in your configuration to allow the formatter to do the work. For example:
[tool.ruff]
line-length = 99
[tool.ruff.lint]
select = ["E", "F", "W", "Q", "I"]
ignore = ["E203", "E501"]
Note that fixable = ["ALL"]
is the default in Ruff, and should be used to specify a subset of selected rules that you want to autofix, if available and not unsafe.
Q&A
Why is ruff not autofixing line-too-long violation?
Ruff doesn’t autofix all lint rules, especially those overlapping with formatters, like line-too-long (E501).
Is there any workaround to auto-format line-too-long violation with ruff?
You can use ruff’s formatter (or Black) to autoformat code and ignore the E501 lint rule.