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

Improve comprehension line break beheavior #5680

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

MichaReiser
Copy link
Member

Summary

This PR improves the Black compatibility when it comes to breaking comprehensions.

We want to avoid line breaks before the target and in whenever possible. Furthermore, if X is not None should be grouped together, similar to other binary like expressions

Test Plan

cargo test

@MichaReiser
Copy link
Member Author

MichaReiser commented Jul 11, 2023

@@ -33,36 +33,40 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {

let comments = f.context().comments().clone();

write!(f, [in_parentheses_only_group(&left.format())])?;
let inner = format_with(|f| {
Copy link
Member Author

Choose a reason for hiding this comment

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

No change other than that this gets all wrapped in an in_parentheses_only_group

Comment on lines 95 to 99
for (
ccccccccccccccccccccccccccccccccccccccc,
ddddddddddddddddddd,
[eeeeeeeeeeeeeeeeeeeeee, fffffffffffffffffffffffff],
) in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd
Copy link
Member Author

Choose a reason for hiding this comment

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

Black doesn't insert parentheses here. I'll look into this next

Copy link
Member

Choose a reason for hiding this comment

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

yep i think that black remove tuple parentheses here same as for statements. i'd expect this is the same case for if-else expressions being analogous to if statements

@MichaReiser MichaReiser added the formatter Related to the formatter label Jul 11, 2023
@MichaReiser MichaReiser requested a review from konstin July 11, 2023 10:01
@MichaReiser MichaReiser force-pushed the comprehension-line-break branch from 71a4aa2 to 7357591 Compare July 11, 2023 10:02
@github-actions
Copy link
Contributor

github-actions bot commented Jul 11, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00     10.4±0.33ms     3.9 MB/sec    1.15     11.9±0.96ms     3.4 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.4±0.05ms     7.0 MB/sec    1.05      2.5±0.13ms     6.6 MB/sec
formatter/numpy/globals.py                 1.02   288.4±19.15µs    10.2 MB/sec    1.00   282.7±18.32µs    10.4 MB/sec
formatter/pydantic/types.py                1.07      5.7±0.50ms     4.4 MB/sec    1.00      5.4±0.31ms     4.7 MB/sec
linter/all-rules/large/dataset.py          1.03     19.4±1.23ms     2.1 MB/sec    1.00     18.7±0.71ms     2.2 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      4.5±0.15ms     3.7 MB/sec    1.01      4.5±0.17ms     3.7 MB/sec
linter/all-rules/numpy/globals.py          1.00   585.5±27.18µs     5.0 MB/sec    1.01   592.4±30.12µs     5.0 MB/sec
linter/all-rules/pydantic/types.py         1.01      8.2±0.28ms     3.1 MB/sec    1.00      8.1±0.35ms     3.2 MB/sec
linter/default-rules/large/dataset.py      1.00      9.0±0.20ms     4.5 MB/sec    1.06      9.5±0.47ms     4.3 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1942.6±49.28µs     8.6 MB/sec    1.17      2.3±0.34ms     7.3 MB/sec
linter/default-rules/numpy/globals.py      1.00   231.0±10.75µs    12.8 MB/sec    1.02   235.5±19.77µs    12.5 MB/sec
linter/default-rules/pydantic/types.py     1.00      4.2±0.17ms     6.0 MB/sec    1.00      4.2±0.16ms     6.0 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.02      9.6±0.12ms     4.3 MB/sec    1.00      9.4±0.08ms     4.3 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.2±0.04ms     7.7 MB/sec    1.01      2.2±0.14ms     7.7 MB/sec
formatter/numpy/globals.py                 1.00    238.1±4.50µs    12.4 MB/sec    1.02    243.7±7.53µs    12.1 MB/sec
formatter/pydantic/types.py                1.01      4.7±0.07ms     5.4 MB/sec    1.00      4.7±0.08ms     5.4 MB/sec
linter/all-rules/large/dataset.py          1.10     18.2±0.23ms     2.2 MB/sec    1.00     16.5±0.24ms     2.5 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.10      4.6±0.11ms     3.6 MB/sec    1.00      4.2±0.07ms     3.9 MB/sec
linter/all-rules/numpy/globals.py          1.05   536.5±11.07µs     5.5 MB/sec    1.00    509.4±7.81µs     5.8 MB/sec
linter/all-rules/pydantic/types.py         1.12      8.1±0.18ms     3.2 MB/sec    1.00      7.2±0.12ms     3.5 MB/sec
linter/default-rules/large/dataset.py      1.24     10.2±0.15ms     4.0 MB/sec    1.00      8.2±0.08ms     4.9 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.18      2.1±0.03ms     8.1 MB/sec    1.00  1746.3±25.40µs     9.5 MB/sec
linter/default-rules/numpy/globals.py      1.12    227.1±4.37µs    13.0 MB/sec    1.00    202.4±5.45µs    14.6 MB/sec
linter/default-rules/pydantic/types.py     1.20      4.4±0.12ms     5.8 MB/sec    1.00      3.7±0.04ms     7.0 MB/sec

target.format(),
soft_line_break_or_space(),
in_spacer,
Copy link
Member

Choose a reason for hiding this comment

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

would the macros rules allow inlining the in_space as if expression?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so because space and soft_line_break_or_space return different structs.

i
in
aitertools._async_map(self.async_inc, arange(8), batch_size=3)
async for i in aitertools._async_map(
Copy link
Member

Choose a reason for hiding this comment

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

that looks much nicer

a, b, c = 3, 4, 5
if (
a == 3
- and b != 9 # fmt: skip
+ and b
+ != 9 # fmt: skip
+ and b != 9 # fmt: skip
Copy link
Member

Choose a reason for hiding this comment

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

this, too

Comment on lines 95 to 99
for (
ccccccccccccccccccccccccccccccccccccccc,
ddddddddddddddddddd,
[eeeeeeeeeeeeeeeeeeeeee, fffffffffffffffffffffffff],
) in eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffffffggggggggggggggggggggghhhhhhhhhhhhhhothermoreeand_even_moreddddddddddddddddddddd
Copy link
Member

Choose a reason for hiding this comment

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

yep i think that black remove tuple parentheses here same as for statements. i'd expect this is the same case for if-else expressions being analogous to if statements

@MichaReiser MichaReiser force-pushed the format-annotated-assignment branch from a9c9dd5 to 3fbd025 Compare July 11, 2023 11:30
@MichaReiser MichaReiser force-pushed the comprehension-line-break branch from 7357591 to 3bce23a Compare July 11, 2023 11:30
@MichaReiser MichaReiser force-pushed the format-annotated-assignment branch from 3fbd025 to 30e7756 Compare July 11, 2023 12:31
@MichaReiser MichaReiser force-pushed the comprehension-line-break branch from 3bce23a to e83b302 Compare July 11, 2023 12:32
Base automatically changed from format-annotated-assignment to main July 11, 2023 14:40
@MichaReiser MichaReiser force-pushed the comprehension-line-break branch from e83b302 to 6e5dc7b Compare July 11, 2023 14:41
@MichaReiser MichaReiser merged commit 8b9193a into main Jul 11, 2023
@MichaReiser MichaReiser deleted the comprehension-line-break branch July 11, 2023 14:51
@MichaReiser
Copy link
Member Author

@MichaReiser merged this pull request with Graphite.

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: Don't insert a newline between if and parentheses in list comprehensions
2 participants