ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Feature request: Ignore specific rule on per file basis via in-file comment

Open mmarras opened this issue 2 years ago • 3 comments

I know we can ignore specific files per toml (per-file-ignores) and we can also ignore the whole file by putting # ruff: noqa at the top of the file. Also I can do --extend-ignore=<rule>.

But how about if I want to ignore only one specifc rule in this file?

The first solution per toml file is not possible for my workflow, the last one does not help with a --pre-commit huck. Would it be possible to allow for it via the in-line comment alla # ruff: noqa: N803.

Background, I am programming a physics based model and N803 is giving me hell, as we use capital variables because they do have commonly understood physical meaning (t for time and T for temperature).

mmarras avatar Feb 01 '23 17:02 mmarras

I suspect extends-per-file-ignore would solve many of the common use cases for this as well. That is biting me a little as well for our monorepo.

Alphasite avatar Feb 02 '23 01:02 Alphasite

You can always turn off the N803 violation entry (ignore = ["N803"] in your pyproject.toml), but I assume you want to keep it enabled, and just disable it in select files -- is that right?

charliermarsh avatar Feb 02 '23 01:02 charliermarsh

That is correct. I‘m working on a django app, which, as explained earlier, encapsulates a physics based model. So here N803 and friends should be off as upper-case conveys meaning, but in general we like the idea of keeping things lower-case in all other apps in general.

Also for this specific project, the .toml is managed by the repo owner, many people contributing apps. And we have pre-commit hooks in place which prevent you from pushing changes to config files. Of course I could ask for a change or force-commit, I was just thinking why not make #ruff: noqa more fine-grained, would be the perfect match for my use-case.

mmarras avatar Feb 02 '23 07:02 mmarras

Flake8 intentionally doesn't support this IIUC. Clippy (Rust's linter) does support something like it although their system is very powerful and effectively allows you to turn rules on and off in any scope.

charliermarsh avatar Feb 03 '23 19:02 charliermarsh

Awesome! Thanks for picking this one up in no time. On Feb 17, 2023, at 02:59, Charlie Marsh @.***> wrote: Closed #2446 as completed via e081455.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

mmarras avatar Feb 17 '23 07:02 mmarras

For those that come here but did not understand how to use that, see the docs on per-file-ignores here. An example is how FastAPI use that here.

yasirroni avatar Mar 07 '23 13:03 yasirroni

It seems that the # flake8: noqa syntax disables ruff for a singular file without having to edit the pyproject.toml!

egeres avatar Jul 09 '23 12:07 egeres

Yes, you can use # ruff: noqa or (for example) # ruff: noqa: F401 to turn off violations all violations or a specific rule, respectively, within a given file. (# flake8: noqa is also supported for compatibility.)

charliermarsh avatar Jul 09 '23 19:07 charliermarsh