ruff/crates/ruff_python_formatter/tests/snapshots
Charlie Marsh 1a4f2a9baf
Avoid reordering mixed-indent-level comments after branches (#7609)
## 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 https://github.com/astral-sh/ruff/issues/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 |
2023-09-22 18:12:31 -04:00
..
black_compatibility@conditional_expression.py.snap Don't "flatten" nested if expressions when formatting (#6996) 2023-08-30 04:11:58 +00:00
black_compatibility@miscellaneous__blackd_diff.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@miscellaneous__debug_visitor.py.snap format ExprJoinedStr (#5932) 2023-08-01 08:26:30 +02:00
black_compatibility@miscellaneous__decorators.py.snap Treat empty-line separated comments as trailing statement comments (#6999) 2023-08-31 20:55:05 +00:00
black_compatibility@miscellaneous__docstring_no_string_normalization.py.snap Format docstrings (#6452) 2023-08-14 12:28:58 +00:00
black_compatibility@miscellaneous__docstring_preview_no_string_normalization.py.snap format ExprJoinedStr (#5932) 2023-08-01 08:26:30 +02:00
black_compatibility@miscellaneous__force_pyi.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
black_compatibility@miscellaneous__long_strings_flag_disabled.py.snap Maybe parenthesize long constants and names (#6816) 2023-08-24 09:47:57 +00:00
black_compatibility@miscellaneous__power_op_newline.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@miscellaneous__string_quotes.py.snap format ExprJoinedStr (#5932) 2023-08-01 08:26:30 +02:00
black_compatibility@py_38__pep_572_remove_parens.py.snap Update Black tests (#6618) 2023-08-16 15:05:51 +00:00
black_compatibility@py_39__python39.py.snap Remove parentheses around some walrus operators (#6173) 2023-07-29 10:06:26 -04:00
black_compatibility@py_310__pattern_matching_extras.py.snap Format PatternMatchOr (#6905) 2023-08-28 08:09:17 +00:00
black_compatibility@py_310__pattern_matching_style.py.snap Introduce AST nodes for PatternMatchClass arguments (#6881) 2023-08-26 14:45:44 +00:00
black_compatibility@py_310__pep_572_py310.py.snap Format expr generator exp (#5804) 2023-07-19 13:01:58 +02:00
black_compatibility@simple_cases__attribute_access_on_number_literals.py.snap Format numeric constants (#5972) 2023-07-24 07:04:40 +00:00
black_compatibility@simple_cases__comment_after_escaped_newline.py.snap Update Black tests (#5438) 2023-06-30 06:32:50 +00:00
black_compatibility@simple_cases__comments2.py.snap Add trailing comma for single-element import-from groups (#6583) 2023-08-15 07:15:33 -04:00
black_compatibility@simple_cases__comments6.py.snap Exclude pragma comments from measured line width (#7008) 2023-09-01 06:34:51 +00:00
black_compatibility@simple_cases__comments9.py.snap Remove newline-insertion logic from JoinNodesBuilder (#6205) 2023-07-31 16:58:15 -04:00
black_compatibility@simple_cases__composition.py.snap Avoid parenthesizing multiline strings in binary expressions (#6973) 2023-08-30 16:03:17 +02:00
black_compatibility@simple_cases__composition_no_trailing_comma.py.snap Avoid parenthesizing multiline strings in binary expressions (#6973) 2023-08-30 16:03:17 +02:00
black_compatibility@simple_cases__docstring_preview.py.snap Format docstrings (#6452) 2023-08-14 12:28:58 +00:00
black_compatibility@simple_cases__expression.py.snap fix: Don't omit optional parentheses for subscripts (#7380) 2023-09-14 08:43:53 +00:00
black_compatibility@simple_cases__fmtonoff.py.snap Treat empty-line separated comments as trailing statement comments (#6999) 2023-08-31 20:55:05 +00:00
black_compatibility@simple_cases__fmtonoff4.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
black_compatibility@simple_cases__fmtonoff5.py.snap Treat empty-line separated comments as trailing statement comments (#6999) 2023-08-31 20:55:05 +00:00
black_compatibility@simple_cases__fmtpass_imports.py.snap Treat empty-line separated comments as trailing statement comments (#6999) 2023-08-31 20:55:05 +00:00
black_compatibility@simple_cases__fmtskip5.py.snap Improve comprehension line break beheavior 2023-07-11 16:51:24 +02:00
black_compatibility@simple_cases__function.py.snap Call chain formatting in fluent style (#6151) 2023-08-04 13:58:01 +00:00
black_compatibility@simple_cases__function2.py.snap Add empty lines before nested functions and classes (#6206) 2023-08-01 15:30:59 +00:00
black_compatibility@simple_cases__ignore_pyi.py.snap Rewrite placement logic (#6040) 2023-07-26 16:21:23 +00:00
black_compatibility@simple_cases__multiline_consecutive_open_parentheses_ignore.py.snap Add general support for parenthesized comments on expressions (#6485) 2023-08-15 18:59:18 +00:00
black_compatibility@simple_cases__remove_await_parens.py.snap Use reserved width to include line suffix measurement (#6901) 2023-08-30 08:07:11 +00:00
black_compatibility@simple_cases__remove_except_parens.py.snap Format raise statement (#5595) 2023-07-10 21:23:49 +02:00
black_compatibility@simple_cases__remove_for_brackets.py.snap Format lambda expression (#5806) 2023-07-19 11:47:56 +00:00
black_compatibility@simple_cases__return_annotation_brackets.py.snap Add general support for parenthesized comments on expressions (#6485) 2023-08-15 18:59:18 +00:00
black_compatibility@simple_cases__torture.py.snap Exclude pragma comments from measured line width (#7008) 2023-09-01 06:34:51 +00:00
black_compatibility@simple_cases__trailing_commas_in_leading_parts.py.snap Use BestFits for non-fluent attribute chains (#6817) 2023-08-24 14:09:25 +02:00
black_compatibility@simple_cases__tupleassign.py.snap Format bytes string (#6166) 2023-07-31 10:46:40 +02:00
format@carriage_return__string.py.snap Normalize '\r' in string literals to '\n' 2023-06-30 10:13:23 +02:00
format@docstring.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@empty_multiple_trailing_newlines.py.snap Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
format@empty_now_newline.py.snap Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
format@empty_trailing_newline.py.snap Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
format@empty_whitespace.py.snap Fix handling of newlines in empty files (#7473) 2023-09-18 06:08:10 +00:00
format@expression__annotated_assign.py.snap Format target: annotation = value? expressions (#5661) 2023-07-11 16:40:28 +02:00
format@expression__attribute.py.snap Avoid re-parenthesizing call chains whose inner values are parenthesized (#7373) 2023-09-14 05:05:37 -04:00
format@expression__await.py.snap Respect parentheses for precedence in await (#7468) 2023-09-18 09:56:41 -04:00
format@expression__binary.py.snap Allow parenthesized content exceed configured line width (#7490) 2023-09-20 08:39:25 +02:00
format@expression__binary_implicit_string.py.snap Fix panic when formatting binary expression with two implicit concatenated string operands (#7287) 2023-09-12 09:49:51 +02:00
format@expression__boolean_operation.py.snap Bool expression comment placement (#7269) 2023-09-12 06:39:57 +00:00
format@expression__bytes.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@expression__call.py.snap fix: Use BestFit layout for subscript (#7409) 2023-09-16 16:21:45 +02:00
format@expression__compare.py.snap fix: Don't omit optional parentheses for subscripts (#7380) 2023-09-14 08:43:53 +00:00
format@expression__dict.py.snap Add dangling comment handling to dictionary key-value pairs (#7495) 2023-09-19 19:17:21 +00:00
format@expression__dict_comp.py.snap Add dangling comment handling to dictionary key-value pairs (#7495) 2023-09-19 19:17:21 +00:00
format@expression__fstring.py.snap Fix placement for comments within f-strings concatenations (#7047) 2023-09-01 16:27:32 +00:00
format@expression__generator_exp.py.snap Preserve generator parentheses in single argument call expressions (#7226) 2023-09-08 10:53:34 +02:00
format@expression__if.py.snap Don't "flatten" nested if expressions when formatting (#6996) 2023-08-30 04:11:58 +00:00
format@expression__lambda.py.snap Add dangling comment handling for lambda expressions (#7493) 2023-09-19 15:23:51 -04:00
format@expression__list.py.snap Respect own-line leading comments before parenthesized nodes (#6820) 2023-08-25 00:18:05 -04:00
format@expression__list_comp.py.snap Avoiding grouping comprehension targets separately from conditions (#7429) 2023-09-16 17:19:34 +00:00
format@expression__named_expr.py.snap Parenthesize NamedExpr if target breaks (#6714) 2023-08-21 16:29:26 +02:00
format@expression__set_comp.py.snap Avoid line break before for in comprehension if outer expression expands (#5912) 2023-07-20 10:07:22 +00:00
format@expression__slice.py.snap fix: Group fluent subscript (#7386) 2023-09-14 13:04:14 +02:00
format@expression__split_empty_brackets.py.snap Avoid omitting optional parentheses for argument-less parentheses (#6484) 2023-08-11 17:58:42 +00:00
format@expression__starred.py.snap Fix comment formatting for yielded tuples (#6603) 2023-08-16 13:41:07 +00:00
format@expression__string.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@expression__subscript.py.snap Avoid re-parenthesizing call chains whose inner values are parenthesized (#7373) 2023-09-14 05:05:37 -04:00
format@expression__tuple.py.snap Use reserved width to include line suffix measurement (#6901) 2023-08-30 08:07:11 +00:00
format@expression__unary.py.snap Avoid extra parentheses in unary expressions (#7428) 2023-09-16 13:07:38 -04:00
format@expression__unsplittable.py.snap Allow parenthesized content exceed configured line width (#7490) 2023-09-20 08:39:25 +02:00
format@expression__yield.py.snap Avoid extra parentheses in yield expressions (#7444) 2023-09-16 14:46:56 -04:00
format@expression__yield_from.py.snap Format ExprYield/ExprYieldFrom (#5921) 2023-07-21 12:07:51 +00:00
format@fmt_on_off__comments.py.snap Indent statements in suppressed ranges (#6507) 2023-08-15 08:00:35 +02:00
format@fmt_on_off__empty_file.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__fmt_off_docstring.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@fmt_on_off__form_feed.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__indent.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@fmt_on_off__last_statement.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__mixed_space_and_tab.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@fmt_on_off__newlines.py.snap Treat empty-line separated comments as trailing statement comments (#6999) 2023-08-31 20:55:05 +00:00
format@fmt_on_off__no_fmt_on.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__off_on_off_on.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__simple.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__trailing_comments.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_on_off__yapf.py.snap fmt: off..on suppression comments (#6477) 2023-08-14 15:57:36 +00:00
format@fmt_skip__decorators.py.snap Support fmt: skip for simple-statements and decorators (#6561) 2023-08-17 05:58:19 +00:00
format@fmt_skip__docstrings.py.snap Support fmt: skip for simple-statements and decorators (#6561) 2023-08-17 05:58:19 +00:00
format@fmt_skip__match.py.snap Format PatternMatchClass (#6860) 2023-08-25 19:03:37 +00:00
format@fmt_skip__or_else.py.snap Support fmt: skip on compound statements (#6593) 2023-08-17 06:05:41 +00:00
format@fmt_skip__parentheses.py.snap Support fmt: skip on compound statements (#6593) 2023-08-17 06:05:41 +00:00
format@fmt_skip__type_params.py.snap Fix fmt:skip for function with return type (#6733) 2023-08-21 17:45:23 +02:00
format@module_dangling_comment1.py.snap Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
format@module_dangling_comment2.py.snap Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
format@newlines.py.snap Treat empty-line separated comments as trailing statement comments (#6999) 2023-08-31 20:55:05 +00:00
format@newlines.pyi.snap Truncate to one empty line in stub files (#7558) 2023-09-21 16:24:42 -04:00
format@parentheses__call_chains.py.snap Preserve parentheses around partial call chains (#7109) 2023-09-04 10:57:04 +01:00
format@parentheses__nested.py.snap Bool expression comment placement (#7269) 2023-09-12 06:39:57 +00:00
format@parentheses__opening_parentheses_comment_empty.py.snap Format PatternMatchSequence (#6676) 2023-08-23 00:44:33 +00:00
format@parentheses__opening_parentheses_comment_value.py.snap Handle open-parenthesis comments on match case (#6798) 2023-08-23 00:40:18 -04:00
format@skip_magic_trailing_comma.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@statement__ann_assign.py.snap Fix StmtAnnAssign formatting by mirroring StmtAssign (#5732) 2023-07-13 10:51:25 +00:00
format@statement__assert.py.snap Allow parenthesized content exceed configured line width (#7490) 2023-09-20 08:39:25 +02:00
format@statement__assign.py.snap fix: Use BestFit layout for subscript (#7409) 2023-09-16 16:21:45 +02:00
format@statement__assign_breaking.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@statement__aug_assign.py.snap Fix StmtAnnAssign formatting by mirroring StmtAssign (#5732) 2023-07-13 10:51:25 +00:00
format@statement__break.py.snap Add tests for skip magic trailing comma 2023-06-26 14:15:55 +02:00
format@statement__class_definition.py.snap Unset after_class_docstring state on every iteration (#7001) 2023-08-30 08:20:28 +02:00
format@statement__delete.py.snap Use reserved width to include line suffix measurement (#6901) 2023-08-30 08:07:11 +00:00
format@statement__ellipsis.pyi.snap Implement FormatPatternMatchValue (#6799) 2023-08-23 14:01:14 +00:00
format@statement__for.py.snap Omit tuple parentheses in for statements except when absolutely necessary (#6765) 2023-08-22 12:18:59 +02:00
format@statement__function.py.snap Fix instability in trailing clause body comments (#7556) 2023-09-21 13:32:16 +00:00
format@statement__global.py.snap Break global and nonlocal statements over continuation lines (#6172) 2023-08-02 19:55:00 +00:00
format@statement__if.py.snap Avoid reordering mixed-indent-level comments after branches (#7609) 2023-09-22 18:12:31 -04:00
format@statement__import.py.snap Ignore blank lines between comments when counting newlines-after-imports (#7607) 2023-09-22 17:49:39 +00:00
format@statement__import_from.py.snap Add trailing comma for single-element import-from groups (#6583) 2023-08-15 07:15:33 -04:00
format@statement__match.py.snap Handle keyword comments between = and value (#6883) 2023-08-30 09:52:51 -04:00
format@statement__module_comment.py.snap Fix dangling module comments (#7456) 2023-09-17 14:56:41 +00:00
format@statement__nonlocal.py.snap Break global and nonlocal statements over continuation lines (#6172) 2023-08-02 19:55:00 +00:00
format@statement__raise.py.snap Add general support for parenthesized comments on expressions (#6485) 2023-08-15 18:59:18 +00:00
format@statement__return.py.snap Use optional parentheses for tuples in return statements (#6875) 2023-08-29 08:30:05 +02:00
format@statement__return_annotation.py.snap Treat parameters-with-newline as empty in function formatting (#7550) 2023-09-20 16:20:22 -04:00
format@statement__top_level.py.snap Use one line between top-level items if formatting a stub file (#6501) 2023-08-15 09:33:57 +02:00
format@statement__top_level.pyi.snap Format function and class definitions into a single line if its body is an ellipsis (#6592) 2023-08-21 09:02:23 +02:00
format@statement__try.py.snap Use reserved width to include line suffix measurement (#6901) 2023-08-30 08:07:11 +00:00
format@statement__type_alias.py.snap Add formatting of type alias statements (#6162) 2023-08-02 20:40:32 +00:00
format@statement__while.py.snap Use reserved width to include line suffix measurement (#6901) 2023-08-30 08:07:11 +00:00
format@statement__with.py.snap Improve with statement comment handling and expression breaking (#6621) 2023-08-18 03:30:38 +00:00
format@stub_files__comments.pyi.snap Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
format@stub_files__nesting.pyi.snap Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
format@stub_files__suite.pyi.snap Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
format@stub_files__top_level.pyi.snap Format empty lines in stub files like black's preview style (#7206) 2023-09-11 08:03:59 +00:00
format@tab_width.py.snap Add a test for stmt assign breaking in preview mode (#7516) 2023-09-19 16:16:40 +02:00
format@trailing_comments.py.snap Match Black's formatting of trailing comments containing NBSP (#7030) 2023-09-01 14:52:59 +02:00
format@trivia.py.snap Add tests for skip magic trailing comma 2023-06-26 14:15:55 +02:00