-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[3608] Fix using multiple fmt:skip
pragmas in a single block
#3978
base: main
Are you sure you want to change the base?
Conversation
In cases where 'fmt:skip' belonged to a closing bracket, prev_sibling would give us the opening bracket instead of the previous element inside the brackets. This is desirable if the whole thing is on the same line (as that is what fmt:skip targets), but in other cases we actually want to start processing the preceding leaf instead.
diff-shades results comparing this PR (58e011b) to main (46be1f8). The full diff is available in the logs under the "Generate HTML diff report" step.
|
Apparently one line in Hypothesis would be changed due to this. Are bug fixes also supposed to go behind |
Here we enable the backtracking to reach preceding leafs even outside the same parent. As long as they are on the same line, it should be fine, as is supposed to target the whole line.
Part of the point of the stable style is so we don't have to decide whether something is a bug or a feature. When you upgrade from one version of Black 23.* to another, you shouldn't see any formatting changes on already-formatted code. However, in this case it looks like the change is made only in preview style, so you're fine! I'll look at the rest of the diff over the next few days. |
After looking at the format skipping code for a while now, it is starting to feel like modifying the AST by rewriting nodes to standalone comments might have not been the optimal approach and PR #3610 might have been a step into problems in the future. Format skipping could instead be a property of the node and leave the tree intact. Then it would be the responsibility of There might be something I still don't understand about the code, but I think giving this change a shot could tell if it's feasible quite fast. Or if you @JelleZijlstra have some insight why the current approach should not be changed, please let me know! 🙏 |
I'm open to alternative approaches for implementing Is there a concrete problem that the current approach can't solve? |
I guess the current approach can solve cases, but it leads to the transformation code being contaminated with checks for standalone comment created by
This would be relevant to
If the nodes are not transformed to standalone comments, we wouldn't have to check for it everywhere we might be interested in the fact. Currently there probably are edge cases where the code doesn't check both for node type and standalone comment containing that node type, but those are not tested against and nobody has reported them. And if they surface, I don't think fixing them with the current approach will be a battle that can be won. |
@JelleZijlstra should this PR be split into smaller ones to make reviewing more inviting? |
It's probably fine as is, I'll need a bit of time to review it though. (And I'd welcome reviews from anyone else following along.) |
Description
This PR contains some refactoring to
comments.py
, as the code was not the cleanest and comments/naming were not up to date. This also fixes bugs with using multiplefmt: skip
pragmas inside a single block as originally reported in 3608, and another problem with skipping statements on the same line as block open, as reported in 3682.Due to the refactoring, this PR is in small commits, so it's easier to review the changes piece by piece.
Fixes #3608
Fixes #3682
Checklist - did you ...
CHANGES.md
if necessary?