Skip to content
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

Break before slice colon #7332

Closed
wants to merge 1 commit into from
Closed

Break before slice colon #7332

wants to merge 1 commit into from

Conversation

konstin
Copy link
Member

@konstin konstin commented Sep 13, 2023

Summary Break slices at the colon first, since the colon is separator with the lowest precedence and we're in a parenthesized context.

Input

section_header_data = byte_array[byte_begin_index + byte_step_index * event_index : byte_begin_index + byte_step_index * (event_index + 1)]

Black

section_header_data = byte_array[
    byte_begin_index
    + byte_step_index * event_index : byte_begin_index
    + byte_step_index * (event_index + 1)
]

Current formatting

section_header_data = byte_array[
    byte_begin_index + byte_step_index * event_index : byte_begin_index
    + byte_step_index * (event_index + 1)
]

Proposed formatting

section_header_data = byte_array[
    byte_begin_index + byte_step_index * event_index 
    : byte_begin_index + byte_step_index * (event_index + 1)
]

This is another intentional black deviation, but i find it a clear style improvement.

This is consistent with adding a step:

section_header_data2 = byte_array[
    byte_begin_index + byte_step_index * event_index 
    : byte_begin_index + byte_step_index 
    : section_size
]

As-is, this regresses trailing colon comments:

in

c1 = "c"[
    1:  # e
    # f
    2
]

out

c1 = "c"[
    1
    :  # e
    # f
    2
]

main

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99981 2760 40
transformers 0.99944 2587 413
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99834 648 20
zulip 0.99956 1437 23

PR

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99981 2760 41
transformers 0.99942 2587 430
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99834 648 20
zulip 0.99956 1437 24

For the similarity index, this is a regression.

Fixes #7316

Test Plan Added the fixtures above.

@konstin konstin added the formatter Related to the formatter label Sep 13, 2023
@MichaReiser
Copy link
Member

Nice! Does this impact our similarity index? How would it look if we intended the right side if the line breaks?

@@ -91,7 +91,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
if !all_simple && lower.is_some() {
space().fmt(f)?;
}
token(":").fmt(f)?;
write!(f, [soft_line_break(), token(":")])?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this into the else branch or we may end up with an unintentional a \n<space>

@@ -155,7 +167,8 @@ d1 = "d"[ # comment
:
]
d2 = "d"[ # comment
1:
1
:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this isn't ideal. Do we need to group the line break with the left side so that it only renders if the left expands (or the : and the right side don't fit on the line)

**Summary** Break slices at the colon first, since the colon is separator with the lowest precedence and we're in a parenthesized context.

**Input**
```python
section_header_data = byte_array[byte_begin_index + byte_step_index * event_index : byte_begin_index + byte_step_index * (event_index + 1)]
```
**Black**
```python
section_header_data = byte_array[
    byte_begin_index
    + byte_step_index * event_index : byte_begin_index
    + byte_step_index * (event_index + 1)
]
```
**Current formatting**
```python
section_header_data = byte_array[
    byte_begin_index + byte_step_index * event_index : byte_begin_index
    + byte_step_index * (event_index + 1)
]
```
**Proposed formatting**
```python
section_header_data = byte_array[
    byte_begin_index + byte_step_index * event_index
    : byte_begin_index + byte_step_index * (event_index + 1)
]
```

This is another intentional black deviation, but i find it a clear style improvement.

This is consistent with adding a step:
```python
section_header_data2 = byte_array[
    byte_begin_index + byte_step_index * event_index
    : byte_begin_index + byte_step_index
    : section_size
]
```

As-is, this regresses trailing colon comments:

**in**
```python
c1 = "c"[
    1:  # e
    # f
    2
]
```
**out**
```python
c1 = "c"[
    1
    :  # e
    # f
    2
]
```

Fixes #7316

**Test Plan** Added the fixtures above.
@konstin konstin force-pushed the break-before-slice-colon branch from e5f4163 to d4cfe1f Compare September 13, 2023 10:15
@konstin
Copy link
Member Author

konstin commented Sep 19, 2023

We have decided to not merge this to instead focus on black compatibility instead of adding more deviations

@konstin konstin closed this Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Formatter undocumented deviation: slice formatting
2 participants