ruff
04a3ec36 - Adjust own-line comment placement between branches (#21185)

Commit
22 days ago
Adjust own-line comment placement between branches (#21185) This PR attempts to improve the placement of own-line comments between branches in the setting where the comment is more indented than the preceding node. There are two main changes. ### First change: Preceding node has leading content If the preceding node has leading content, we now regard the comment as automatically _less_ indented than the preceding node, and format accordingly. For example, ```python if True: preceding_node # leading on `else`, not trailing on `preceding_node` else: ... ``` This is more compatible with `black`, although there is a (presumably very uncommon) edge case: ```python if True: this;that # leading on `else`, but trailing in `black` else: ... ``` I'm sort of okay with this - presumably if one wanted a comment for those semi-colon separated statements, one should have put it _above_ them, and one wanted a comment only for `that` then it ought to have been on the same line? ### Second change: searching for last child in body While searching for the (recursively) last child in the body of the preceding _branch_, we implicitly assumed that the preceding node had to have a body to begin the recursion. But actually, in the base case, the preceding node _is_ the last child in the body of the preceding branch. So, for example: ```python if True: something last_child_but_no_body # leading on else for `main` but trailing in this PR else: ... ``` ### More examples The table below is an attempt to summarize the changes in behavior. The rows alternate between an example snippet with `while` and the same example with `if` - in the former case we do _not_ have an `else` node and in the latter we do. Notice that: 1. On `main` our handling of `if` vs. `while` is not consistent, whereas it is consistent in the present PR 2. We disagree with `black` in all cases except that last example on `main`, but agree in all cases for the present PR (though see above for a wonky edge case where we disagree). <table> <tr> <th>Original &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th> <th><code>main</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th> <th>This PR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th> <th><code>black</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th> </tr> <tr> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass else: # comment pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> </tr> <tr> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> </tr> <tr> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> </tr> <tr> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> </tr> <tr> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass else: # comment pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> while True: pass # comment else: pass </pre> </td> </tr> <tr> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> <td> <pre lang="python"> if True: pass # comment else: pass </pre> </td> </tr> </table>
Author
Parents
Loading