ruff: Support baselines, i.e., ignore existing errors for incremental adoption

Baselines would allow developers to introduce Ruff to their codebase incrementally by ignoring existing errors and address them as they reappear when making changes to the code.

Supported by some tools in the flake8 ecosystem:

A very basic proposal for this in ruff:

In the CLI

ruff . --exit-zero --format baseline > .ruff_baseline
ruff . --baseline .ruff_baseline  # doesn't report any errors

In pyproject.toml

[tool.ruff]
baseline = ".ruff_baseline"

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 44
  • Comments: 25 (4 by maintainers)

Most upvoted comments

I also have a baseline implementation for mypy as a separate tool: mypy-baseline. As people already said in the thread, baseline doesn’t store line numbers. In case of mypy-baseline, violations are matched to the baseline based on the file name and error message: https://github.com/orsinium-labs/mypy-baseline/blob/master/mypy_baseline/_error.py#L60-L68

In the case of flakehell (now flakeheaven) things were a bit easier because I could use error codes (since error messages may change): https://github.com/life4/flakehell/blob/master/flakehell/_logic/_baseline.py#L6-L11

In case of a new violation being similar to the old one (the same file and the same error code), simply the last one is reported based on assumption that the new code is more often added closer to the end of the file. It may produce false-positives in theory but my teams haven’t faced it yet.

I think it was a good decision to keep baselines in a separate file. I also considered automatically adding # noqa to the code but abandoned that idea:

  1. All # noqa (or typing: ignore in case of mypy) comments should be used in rare occasions of legit false-poisitives when a human looked at the code and said: “yep, that would be hard to work around”. The baseline, on the other hand, is for the tech debt that humans haven’t looked into yet but should gradually go through and get rid of. I don’t think these two should be mixed.
  2. Having the baseline in one place gives a better perspective on the work left to do. In mypy-baseline, I provide history, plot, and top-files commands that I often run and show the team to remind them that we should do progress resolving it.
  3. Introducing a tool that just needs a single baseline file and a single config is much easier than a tool that changes something in every file.

What I regret doing in flakehell is that the baseline is a collection of obscure md5 hashes. In mypy-baseline, the baseline is human-readable and we quite often use that fact to manually read and edit it.

I hope this perspective can be of any help.

As (temporary?) solution, I just released Riff PyPi / GitHub,

Riff wraps Ruff, runs git diff, checks for modified lines, only fails if errors were found in modified lines. Works as a CLI tool as well as a pre-commit hook, check it out 😃

I’m new to ruff (loving it so far!) but have been using flakeheaven and it’s predecessor flakehell for quite a while now. I really enjoy flakeheaven’s baseline feature - there’s some more information here. We’ve used it successfully on a larger code base to incrementally fix legacy issues by incorporating the diff command in that linked doc. We have a fairly large Python monolith at my day job, and when developers make changes they are prompted to fix issues in code they touched; an easy example of this is docstrings - in functions that do not have docstrings, if a developer modifies that function, flakeheaven will then identify it as needing a docstring and flag it in pre-commit or our CI.

I’m definitely not an expert in the mechanism used to generate the baseline file, but just wanted to toss in some lived experience with this functionality. I believe it is similar to what others have called out above, in that it’s tied to the filename and the line numbers, so we do occasionally see instances where very old legacy code is touched and multiple “unrelated” issues then pop up. However, in general, even on a code base with some fairly large legacy files (7k lines of Django serializers), it’s usually not so bad to get things fixed up. This incrementally modernizes and even fixes our existing code, which is great.

Hi,

I wholeheartedly support the implementation of a separate baseline file as it proves to be an immensely valuable feature. Given the context of my position as the head of the department in a company with a substantial monolithic codebase, I am actively striving to enforce robust code standards. To achieve this, I have introduced linters during the code push to the repository and have provided the team with suitable tools. However, due to the sheer size of the codebase, it is not feasible to lint the entirety of it. Therefore, my objective is to exclusively lint the new code changes—the actual differences in the codebase as it progresses forward.

Flakeheaven or flakehell do a great job doing this baseline, I wish ruff could bring something similar.

You can use ondivi Pypi Github. Is a Python script filtering coding violations, identified by static analysis, only for changed lines in a Git repo. You can use it with ruff, flake8, pylint, mypy and others.

flakeheaven and flakehell are not supported because they rely on internal flake8 API, which can lead to compatibility issues as flake8 evolves. In contrast, ondivi uses only the text output of violations and the state of Git repository, making it more robust and easier to maintain.

Flakeheaven: https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/integrations/flakeheaven.html#legacy-first-1

WPS remove this page from the docs and replace it with https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/integrations/ondivi.html

That’s interesting! I’ve never seen this before. I think we could support it? I’d been hoping that --add-noqa would serve this purpose. What do you see as the strengths of a baseline file vis-a-vis that workflow?

One point of confusion for me w/r/t the baseline file is that if it’s just storing errors + line numbers, then if you edit a file (e.g., add a function to the top of a file that has some ignored errors), doesn’t the baseline get invalidated? Since all the errors are pushed to new lines?

flakeheaven stores md5 hash of the violation and it’s context. If performance is a concern a cheaper hash should be fine too.

The --add-noqa wouldn’t leave a way to find out if the noqa were never meant to be fixed, or were just added for initial adoption.

I would recommend looking into what elm-review did, with its error suppression system. I described the system in this blog post. The baseline doesn’t include the line numbers — but instead counts the number of problems per file and per rule — meaning it doesn’t suffer from the problem @charliermarsh was rightfully wary of.

My experience with this system is very positive. I would definitely recommend it over adding a bunch of noqa comments everywhere, because now it becomes unclear what purposefully got a noqa and what got one because someone used --add-noqa because there were too many errors. It’s also a better system than turning the rules on as “warnings” (which ruff doesn’t support but IMO shouldn’t) which some people tend to do when there are too many existing errors.

FYI, elm-review is a very beloved linter for the Elm language, and it supports neither noqa comments nor severity levels (warning/error), only ignored files through the configuration and this suppression/baseline system. And it’s working really well. Some more information around this in its documentation

Happy to answer any questions on the topic (and linters in general 😁)

Very excited for this. Is currently needed on my team.

One possible implementation of baselines that would handle many of the issues herein (though it would introduce at least one of its own) would be to incorporate the baseline into the file itself with the noqa directive. If --add-noqa could include extra information in the comment containing the directive, it could be used to set a baseline. Combined with rule RUF100 and ruff’s auto-fix feature, this could provide a very user-visible version of baselines directly in the code.

For example, a file that contains an existing line of:

from math import *

could get a directive such as:

from math import *  # noqa: F403 - Existing errors as of 2023-01-29 ignored by ruff.

In my view it has the following advantages:

  1. No messing with line numbers, counting violations, etc.
  2. The directives are very visible to the user, encouraging their fixing when touching nearby code
  3. A baseline can follow a particular code snippet even if files are moved, etc. Splitting a thousand-line legacy file in two doesn’t have to come with the work of fixing all the linting errors in half that file. (After all, a linting tool is supposed to help us, not burden us.)
  4. With --fix and rule RUF100, directives get automatically removed when appropriate.

Of course, it also has one distinct disadvantage:

  1. Every rule-violating line of code now has an extra node in the blame layer just for adding noqa directives.

This could be mitigated by providing an obvious separate author credit on this (commit the output of --add-noqa “as ruff”, if you will). Developers would immediately recognise this and look at previous commits, and if a standard is generated tools might begin to provide similar options.

This is received positive feedback in terms of functionality. Is the only open question the format of the baseline data?

I was imagining this could be stored by hashing the filename with line of code and possibly error code, e.g. MD5(CONCAT(<filename>, <line of code>, <ruff error code>)).

Example:

Finding: test_foobar.py:

55 | def test_do_FooBar(api_client, authenticated_user, mock_pynamodb, mock_eventbridge_client):
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ N802
ruff . --exit-zero --format baseline > .ruff_baseline
ruff . --baseline .ruff_baseline  # doesn't report any errors

.ruff_baseline:

<filename>|<md5 of line 55>|<error code>
2af918dbc4589ec4a153761e4c9af86f

Darker is a very relevant tool here (which isn’t just for black).

And for prior art, basedmypy and Qodana both support baseline functionality.