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:
- Bandit: https://bandit.readthedocs.io/en/latest/start.html#baseline
- Flakeheaven: https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/integrations/flakeheaven.html#legacy-first-1
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)
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
# noqato the code but abandoned that idea:# noqa(ortyping: ignorein 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.history,plot, andtop-filescommands that I often run and show the team to remind them that we should do progress resolving it.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 usingflakeheavenand it’s predecessorflakehellfor quite a while now. I really enjoyflakeheaven’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 thediffcommand 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,flakeheavenwill then identify it as needing a docstring and flag it inpre-commitor 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
ondiviPypi 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.
WPS remove this page from the docs and replace it with https://wemake-python-styleguide.readthedocs.io/en/latest/pages/usage/integrations/ondivi.html
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-noqawouldn’t leave a way to find out if thenoqawere never meant to be fixed, or were just added for initial adoption.I would recommend looking into what
elm-reviewdid, 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
noqaand what got one because someone used--add-noqabecause 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-reviewis a very beloved linter for the Elm language, and it supports neithernoqacomments 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 documentationHappy 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
noqadirective. If--add-noqacould include extra information in the comment containing the directive, it could be used to set a baseline. Combined with ruleRUF100and 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:
could get a directive such as:
In my view it has the following advantages:
--fixand ruleRUF100, directives get automatically removed when appropriate.Of course, it also has one distinct disadvantage:
noqadirectives.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:.ruff_baseline:Darker is a very relevant tool here (which isn’t just for black).
And for prior art, basedmypy and Qodana both support baseline functionality.