-
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
Formatter instability: trailing clause comments in if
statement
#7602
Milestone
Comments
charliermarsh
added
bug
Something isn't working
formatter
Related to the formatter
labels
Sep 22, 2023
This is fixed on main |
More minimal repro. It's related with |
This was referenced Sep 22, 2023
charliermarsh
added a commit
that referenced
this issue
Sep 22, 2023
## Summary Given: ```python if True: if True: if True: pass #a #b #c else: pass ``` When determining the placement of the various comments, we compute the indentation depth of each comment, and then compare it to the depth of the previous statement. It turns out this can lead to reordering comments, e.g., above, `#b` is assigned as a trailing comment of `pass`, and so gets reordered above `#a`. This PR modifies the logic such that when we compute the indentation depth of `#b`, we limit it to at most the indentation depth of `#a`. In other words, when analyzing comments at the end of branches, we don't let successive comments go any _deeper_ than their preceding comments. Closes #7602. ## Test Plan `cargo test` No change in similarity. Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 319 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 319 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 |
charliermarsh
added a commit
that referenced
this issue
Sep 25, 2023
## Summary Given: ```python if True: if True: pass else: pass # a # b # c else: pass ``` We want to preserve the newline after the `# c` (before the `else`). However, the `last_node` ends at the `pass`, and the comments are trailing comments on the `pass`, not trailing comments on the `last_node` (the `if`). As such, when counting the trailing newlines on the outer `if`, we abort as soon as we see the comment (`# a`). This PR changes the logic to skip _all_ comments (even those with newlines between them). This is safe as we know that there are no "leading" comments on the `else`, so there's no risk of skipping those accidentally. Closes #7602. ## Test Plan No change in compatibility. Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 319 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99979 | 3496 | 22 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99963 | 2587 | 319 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given:
We're removing the newline after
# print(xsrfRequest.headers["x-csrf-token"],xsrfRequest.headers)
, which leads to an instability.The above code is the once-formatted version of:
Sourced from #7590 (
A_14230110707883433080.py
).The text was updated successfully, but these errors were encountered: