-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the --unsafe
option for --fix
, --fix-only
, and --diff
#5119
Add the --unsafe
option for --fix
, --fix-only
, and --diff
#5119
Conversation
PR Check ResultsBenchmarkLinux
Windows
|
@evanrittenhouse I suspect there are probably too many outstanding I think we may want |
Yeah, so by default we'll apply |
What's the consensus on opting into suggested fixes? I was thinking we could either:
Or both! |
I don't think we have a consensus yet and is something we should discuss: Adding a new CLI option seems reasonable to me. I would suggest to prefix the option with I'm unsure how to support this in the configuration best. I would expect that adding a rule to fixable promotes it to automatic (only when applying, not for rendering diagnostics). Adding a rule to unfixable demotes it to manual. The challenge with this approach is that promoting a single suggested fix requires adding all automatic fixes to fixable, which is rather annoying. We could deprecate the existing fixable and unfixable in favor of adding new options We should also think about what changes are necessary in the VS code extension and I'm probably missing a few details here. |
Why did we move away from
I like this! We could default to
I like this approach, though I think we could smooth the implementation by providing an |
cc @MichaReiser wondering if the above design makes sense - I plan on wrapping up the remaining linters tonight, then I'll be able to come back to this and implement the rest of the selection logic. |
@evanrittenhouse - Micha's gonna be out tomorrow so feel free to ping again on Wednesday if he doesn't get back to you. |
@evanrittenhouse you can see the discussion about the names in #4183 I think since the display is "Fix" and "Suggested fix" (as implemented in #4303 but we could change this) users would understand |
@zanieb Interesting. So if we were to go with Also, I'm curious how we want to handle the configuration file for this. In my opinion, adding a rule (regardless of applicability level) to
What do you think? |
I'm a bit hesitant from adding new fields to
I'm unsure if I understand your motivation for adding the override field. Is it to avoid generating unnecessary fixes?
That's correct. Manual fixes should not be applied automatically. I wonder if it even makes sense allowing users to promote manual fixes because it is intentional that manual fixes can be incomplete, always or sometimes resulting in invalid syntax.
Would this be initialized depending on whether the rule is in the
What do you mean by appropriate toml file. Reading it from the closest pyproject toml or is this another configuration file? |
Yes, an attempt to keep the current configuration option setup with I believe above you also mentioned:
Which would also definitely work (and I think is better), but I'm not sure how we'd handle the above case.
That's fine with me. I think the important thing is that we make it clear that
Yeah, closest @MichaReiser let me know what you think! |
1f707ff
to
9ce39ae
Compare
Which code would be responsible for changing/setting the
Can you elaborate how this would work with the What I had in mind is that ruff generates all fixes and then filter out the fixes where the Applicability doesn't match. This has the downside that we'll generate more fixes than what's strictly necessary but I don't think I'm too concerned about that. An alternative would be to change |
@MichaReiser This is basically how I'm imagining the entire feature. CLI
Rules with a Configuration File
ImplementationConsider the example where we're enforcing default rules (e.g., The Now assume that the user wants to fix all Automatic fixes, with F841 added. They F841 into the We need to somehow tell F841 that its
If we go with the second option, What do you think? |
Thanks for the in-depth elaboration. This helped. Inputs/Questions from my side:
|
@MichaReiser Done! I'll be out most of today but will be able to get to your feedback by tonight or tomorrow morning probably. |
29d243e
to
5f532dd
Compare
f180504
to
b73420b
Compare
Hey @evanrittenhouse I just want to let you know that I'm working on our CLI design right now and I expect it to overlap a bit with our plans for this feature. For that reason, I expect it to be a bit before we're ready to merge this breaking change. I'll be back with some guidance soon hopefully. Let me know if you have any questions or concerns! |
@zanieb no worries, thanks for the heads up! I'll just work on implementing the changes mentioned and leave the CLI option stuff for later. Should be able to get the underlying implementation down at least. I might go back and see what I can do about the Markdown parser... |
b73420b
to
6500c36
Compare
02a84a6
to
5d8864d
Compare
aaf677b
to
ec2edd0
Compare
CodSpeed Performance ReportMerging #5119 will not alter performanceComparing Summary
|
ec2edd0
to
93cc501
Compare
Finally getting around to rebasing this, hoping to have it done either early this week. |
93cc501
to
05462ce
Compare
Following much discussion for #4181 at #5119, #5476, #7769, #7819, and in [Discord](https://discord.com/channels/1039017663004942429/1082324250112823306/1159144114231709746), this pull request changes `Applicability` from using `Automatic`, `Suggested`, and `Manual` to `Always`, `Sometimes`, and `Never`. Also removes `Applicability::Unspecified` (replacing #7792).
Rebase of #5119 authored by @evanrittenhouse with additional refinements. ## Changes - Adds `--unsafe-fixes` / `--no-unsafe-fixes` flags to `ruff check` - Violations with unsafe fixes are not shown as fixable unless opted-in - Fix applicability is respected now - `Applicability::Never` fixes are no longer applied - `Applicability::Sometimes` fixes require opt-in - `Applicability::Always` fixes are unchanged - Hints for availability of `--unsafe-fixes` added to `ruff check` output ## Examples Check hints at hidden unsafe fixes ``` ❯ ruff check example.py --no-cache --select F601,W292 example.py:1:14: F601 Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option). ``` We could add an indicator for which violations have hidden fixes in the future. Check treats unsafe fixes as applicable with opt-in ``` ❯ ruff check example.py --no-cache --select F601,W292 --unsafe-fixes example.py:1:14: F601 [*] Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 2 fixable with the --fix option. ``` Also can be enabled in the config file ``` ❯ cat ruff.toml unsafe-fixes = true ``` And opted-out per invocation ``` ❯ ruff check example.py --no-cache --select F601,W292 --no-unsafe-fixes example.py:1:14: F601 Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option). ``` Diff does not include unsafe fixes ``` ❯ ruff check example.py --no-cache --select F601,W292 --diff --- example.py +++ example.py @@ -1,2 +1,2 @@ x = {'a': 1, 'a': 1} -print(('foo')) +print(('foo')) \ No newline at end of file Would fix 1 error. ``` Unless there is opt-in ``` ❯ ruff check example.py --no-cache --select F601,W292 --diff --unsafe-fixes --- example.py +++ example.py @@ -1,2 +1,2 @@ -x = {'a': 1} -print(('foo')) +x = {'a': 1, 'a': 1} +print(('foo')) \ No newline at end of file Would fix 2 errors. ``` #7790 will improve the diff messages following this pull request Similarly, `--fix` and `--fix-only` require the `--unsafe-fixes` flag to apply unsafe fixes. ## Related Replaces #5119 Closes #4185 Closes #7214 Closes #4845 Closes #3863 Addresses #6835 Addresses #7019 Needs follow-up #6962 Needs follow-up #4845 Needs follow-up #7436 Needs follow-up #7025 Needs follow-up #6434 Follow-up #7790 Follow-up #7792 --------- Co-authored-by: Evan Rittenhouse <evanrittenhouse@gmail.com>
Following much discussion for #4181 at #5119, #5476, #7769, #7819, and in [Discord](https://discord.com/channels/1039017663004942429/1082324250112823306/1159144114231709746), this pull request changes `Applicability` from using `Automatic`, `Suggested`, and `Manual` to `Always`, `Sometimes`, and `Never`. Also removes `Applicability::Unspecified` (replacing #7792).
Rebase of #5119 authored by @evanrittenhouse with additional refinements. ## Changes - Adds `--unsafe-fixes` / `--no-unsafe-fixes` flags to `ruff check` - Violations with unsafe fixes are not shown as fixable unless opted-in - Fix applicability is respected now - `Applicability::Never` fixes are no longer applied - `Applicability::Sometimes` fixes require opt-in - `Applicability::Always` fixes are unchanged - Hints for availability of `--unsafe-fixes` added to `ruff check` output ## Examples Check hints at hidden unsafe fixes ``` ❯ ruff check example.py --no-cache --select F601,W292 example.py:1:14: F601 Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option). ``` We could add an indicator for which violations have hidden fixes in the future. Check treats unsafe fixes as applicable with opt-in ``` ❯ ruff check example.py --no-cache --select F601,W292 --unsafe-fixes example.py:1:14: F601 [*] Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 2 fixable with the --fix option. ``` Also can be enabled in the config file ``` ❯ cat ruff.toml unsafe-fixes = true ``` And opted-out per invocation ``` ❯ ruff check example.py --no-cache --select F601,W292 --no-unsafe-fixes example.py:1:14: F601 Dictionary key literal `'a'` repeated example.py:2:15: W292 [*] No newline at end of file Found 2 errors. [*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option). ``` Diff does not include unsafe fixes ``` ❯ ruff check example.py --no-cache --select F601,W292 --diff --- example.py +++ example.py @@ -1,2 +1,2 @@ x = {'a': 1, 'a': 1} -print(('foo')) +print(('foo')) \ No newline at end of file Would fix 1 error. ``` Unless there is opt-in ``` ❯ ruff check example.py --no-cache --select F601,W292 --diff --unsafe-fixes --- example.py +++ example.py @@ -1,2 +1,2 @@ -x = {'a': 1} -print(('foo')) +x = {'a': 1, 'a': 1} +print(('foo')) \ No newline at end of file Would fix 2 errors. ``` #7790 will improve the diff messages following this pull request Similarly, `--fix` and `--fix-only` require the `--unsafe-fixes` flag to apply unsafe fixes. ## Related Replaces #5119 Closes #4185 Closes #7214 Closes #4845 Closes #3863 Addresses #6835 Addresses #7019 Needs follow-up #6962 Needs follow-up #4845 Needs follow-up #7436 Needs follow-up #7025 Needs follow-up #6434 Follow-up #7790 Follow-up #7792 --------- Co-authored-by: Evan Rittenhouse <evanrittenhouse@gmail.com>
Replaced by #7769 which used the commits from here. Thanks so much Evan for driving this forward! |
Summary
Fixes #4185.
This is a breaking change which will alter the rulesets fixed by
--fix
, among others. I'll update the PR description with new changes as I make them - see the issue link for the entire list.This PR depends on all
Fix::unspecified
instances being refactored to an applicable (pun intended :P )Applicability
. Currently,ripgrep
says there are occurrences in:flake8_commas
: fixed in Add Applicability to flake8_comma fixes #5127flake8_logging_format
: fixed in Add Applicability to flake8_logging_format fixes #5129flake8_pytest_style
: fixed in Add applicability to flake8_pytest_style #5389flake8_quotes
: fixed in Add Applicability to flake8_quotes fixes #5130flake8_simpilfy
: fixed in Add Applicability to flake8_simplify #5348flake8_tidy_imports
: fixed in Add Applicability to flake8_tidy_imports #5131flynt
: fixed in Add Applicability to flynt #5160isort
: fixed in Add Applicability to isort #5161pandas_vet
: fixed in Add Applicability to pandas_vet #5252pycodestyle
: fixed in Add Applicability to pycodestyle #5282pydocstyle
: fixed in Add applicability to pydocstyle #5390pyflakes
: fixed in Add Applicability to pyflakes #5253pylint
: fixed in Add Applicability to pylint #5251pyupgrade
: fixed in Add Applicability to pyupgrade #5162Test Plan
C410
isApplicability::unspecified
. I made a new file:and ran Ruff against it. The file was not fixed. I then changed the rule to have
Applicability::automatic
and ran it again - this time, it was fixed.