-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Policy for linter fixes that can drop comments #9790
Comments
Another thing we do in some places (which IMO is incorrect): if the range contains a comment, we don't offer the fix at all. For example, this is true of |
There also used to be some cases in which we wouldn't flag the violation at all if the range contained comments, but I think we removed these. (I'm not certain.) |
Just to put a decision out there for discussion, I'd propose...
|
Since I was asked elsewhere to clarify, the above suggestion is meant to say: We should not disable a rule due to the presence of comments. We should not disable a fix due to the presence of comments. We should mark a fix as unsafe if there's a comment in the range. We should only prioritize proactively changing the behavior of rules that rewrite large blocks of code (modify entire statements, or convert statements to expressions or vice versa). |
The ideal situation would be that all rules handle comments correctly and only remove comments if a fix removes the entire code block containing the comment. Now, what the short-term ideal should be is something I find much harder to implement, and I don't feel I have the understanding to decide on that. What's important to me is that we can come up with a proposal that avoids making the That's why I need a better understanding of how many rules would be affected by any of the above proposals and the ideal solution to me is the solution where only enabling safe fixes remains the best default for users while being as explicit (and defensive) in which situation ruff removes comments. |
Chiming in because I recently encountered a somehow related issue. Basically something like that: -from . import (
- foo,
- bar,
- MyVeryImportantClass, # noqa: F401
-)
+ from yay import foo, bar I think what you proposed above about demoting the fix safety to unsafe in case there are comments would have solve my issue here. |
My 2 cents: If possible it would be great if ruff could detect whether it has lost comments, and surface that to the user. I don't have as strong opinions between
I do agree that the current status quo is kinda iffy, but I've also spent an unreasonable amount of time trying to preserve comments in AST & libcst parsers that I can empathize <3 |
In some cases, a lint fix can drop comments. Retaining all comments was a major goal in the Ruff formatter and we've learned that it is very hard to accomplish. There is significant foundational work that would need to be invested in the linter to prevent it from ever dropping comments. While we would like to accomplish this eventually, we're a small team with a lot of priorities to balance. In the meantime, we want to formalize a policy for fixes that may drop comments.
A few options:
We currently apply a mix of these options, but we should be consistent. More options and discussion welcome.
Related issues and reports:
flake8-comprehension
fixes #4296The text was updated successfully, but these errors were encountered: