Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruff does not autofix line-too-long violation

Tags:

python

ruff

I have a python project and I am configuring latest version of ruff for that project for linting and formating purpose. I have the below settings in my pyproject.toml file:

[tool.ruff]
select = ["E", "F", "W", "Q", "I"]

ignore = ["E203"]

# Allow autofix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# restrict Line length to 99
line-length = 99

The ruff check command with autofix feature (--fix) of ruff identifies that the lines are long with E501 errors, but it does not format that code to wrap to next line to maintain the line-length restriction. Is there something I need to enable or do to ensure that ruff fixes this? Or is this not possible in ruff currently? Please help.

I tried going through the documentation to find anything, but I am clueless what to do here.

like image 654
Sukanya Pai Avatar asked Nov 22 '25 22:11

Sukanya Pai


2 Answers

As of today (Ruff's autoformatter available), the following should hold:

  • not every lint rule can be autofixed, E501 can't. You can find the list of rules that can be autofixed at https://docs.astral.sh/ruff/rules/#rules (they are marked with 🛠️). Moreover, in general, Ruff does not enforce autofixing rules that do overlap with the use of a formatter (as E501 inherently does).

    From the docs (https://docs.astral.sh/ruff/faq/#is-the-ruff-linter-compatible-with-black)

Ruff is designed to be used alongside a formatter (like Ruff's own formatter, or Black) and, as such, will defer implementing stylistic rules that are obviated by automated formatting.

  • Ruff's formatter (or Black's, with little difference) - ruff format - would instead try to autoformat E501 issues according to the line-length setting, where* possible. *Namely, there are cases where autoformatting is not enabled as it would lead to ambiguities (eg single string longer than line-length and "not wrappable" without incurring again in line-too-long); more examples available at https://docs.astral.sh/ruff/rules/line-too-long/#why-is-this-bad. Btw, observe that there are plans to support some of such cases automatically as well (see https://github.com/astral-sh/ruff/issues/1904#issuecomment-1385409282, https://github.com/astral-sh/ruff/issues/1904#issuecomment-1387089577 and https://github.com/astral-sh/ruff/issues/1904#issuecomment-1806943382 eg).

All this said, IMO, aiming at autoformatting E501 issues (with no manual intervention), in your case the following configuration might help:

[tool.ruff]
line-length = 99

[tool.ruff.lint]
select = ["E", "F", "W", "Q", "I"]
ignore = ["E203", "E501"]

i.e. simply specify the line-length setting letting the formatter (Ruff or Black) do the work and ignore E501 lint rule (if all the E-like rules are selected). Observe that's kind of what Ruff does by default (i.e. in its default configuration). It enables F rules and a subset of E rules (among which you won't find E501); it leaves aside all those stylistic rules overlapping with the use of a formatter (see https://docs.astral.sh/ruff/tutorial/#configuration for reference).

Final side note, as per my understanding fixable = ["ALL"] is the default already (see https://docs.astral.sh/ruff/settings/#fixable); thus, fixable should be rather used to specify those rules - among the ones to be checked - that you want to autofix as well (provided that an autofix is available and not unsafe); IOW, it should specify a subset of the selected rules not to be redundant.

like image 159
amiola Avatar answered Nov 25 '25 12:11

amiola


It seems like Ruff has released Ruff Python Formatter as part of v0.0.289 and is currently in alpha state - https://github.com/astral-sh/ruff/blob/main/crates/ruff_python_formatter/README.md

We are currently using v0.0.280 which does not have this feature so we used a combination of Black and Ruff as per our project requirements.

like image 31
Sukanya Pai Avatar answered Nov 25 '25 11:11

Sukanya Pai