-
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 undocumented deviation: slice formatting #7316
Comments
Do you have an ideal formatting for this case? I find both of these hard to reason about. |
Maybe this def f():
for event_index in range(header.number_non_empty_events):
section_header_data = struct.unpack(
JSON_BINARY_PARAMETERS.fmt_section_header,
byte_array[
byte_begin_index + byte_step_index * event_index
: byte_begin_index + byte_step_index * (event_index + 1)
],
) |
Thanks for reporting this. The deviation here is rooted in the fact that:
Here's an example where Black splits before all def f():
for event_index in range(header.number_non_empty_events):
section_header_data = struct.unpack(
JSON_BINARY_PARAMETERS.fmt_section_header,
byte_array[
byte_begin_index
+ byte_step_index : byte_begin_index
+ byte_step_index
+ (event_index + 1)
],
)
# Ruff
def f():
for event_index in range(header.number_non_empty_events):
section_header_data = struct.unpack(
JSON_BINARY_PARAMETERS.fmt_section_header,
byte_array[
byte_begin_index + byte_step_index : byte_begin_index
+ byte_step_index
+ (event_index + 1)
],
) I generally prefer Ruff's approach of treating them as separate expressions but we should probably add a softline break before the Prettier's formatting looks similar to what @JonathanPlasse proposes (I had to adjust the syntax slightly to get the JS equivalent) for (event_index in range(header.number_non_empty_events)) {
section_header_data = struct.unpack(
JSON_BINARY_PARAMETERS.fmt_section_header,
{
[byte_begin_index + byte_step_index * event_index]:
byte_begin_index + byte_step_index * (event_index + 1),
},
);
} |
**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.
**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.
We decided not to implement the deviation for now and instead focus on Black compatibility because:
We plan to re-visit the formatting eventually |
We're not planning to change this for the Beta. We want to re-open the possibility of changing to match @JonathanPlasse's suggested style once we have better tooling to evaluate the impact (e.g., ruff-shades). |
We may want to consider #8897 when making a style decision to make sure our handling is consistent. |
Black formatting
Ruff formatting
Use Ruff 0.0.289 with line length to 100
The text was updated successfully, but these errors were encountered: