Skip to content

Cover Black's is_aritmetic_like formatting#5738

Merged
MichaReiser merged 1 commit intomainfrom
handle-arith-like-expr
Jul 14, 2023
Merged

Cover Black's is_aritmetic_like formatting#5738
MichaReiser merged 1 commit intomainfrom
handle-arith-like-expr

Conversation

@MichaReiser
Copy link
Member

Summary

Black only wrapps some arithmetic like expressions in parentheses on the expression but keeps all other statements single lines.

This PR implements this behavior by introducing new in_parentheses_only_* helpers for line breaks. We don't really need the
in_parentheses_only_group anymore but it is a nice optimisation that prevents that the Printer must measure if a group fits if it doesn't contain a single soft line break.

Test Plan

You can see how + expressions continue to expand, while comparision expressions and strings remain flat.

The similarity index for the django project remains unchanged.

@MichaReiser
Copy link
Member Author

MichaReiser commented Jul 13, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@MichaReiser MichaReiser force-pushed the handle-arith-like-expr branch from edf6bbf to 283068d Compare July 13, 2023 13:44
@github-actions
Copy link
Contributor

github-actions bot commented Jul 13, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      8.4±0.10ms     4.9 MB/sec    1.00      8.4±0.02ms     4.9 MB/sec
formatter/numpy/ctypeslib.py               1.00   1869.7±3.13µs     8.9 MB/sec    1.01  1884.9±59.04µs     8.8 MB/sec
formatter/numpy/globals.py                 1.00    203.5±0.90µs    14.5 MB/sec    1.01    204.9±0.52µs    14.4 MB/sec
formatter/pydantic/types.py                1.02      4.2±0.01ms     6.1 MB/sec    1.00      4.1±0.00ms     6.2 MB/sec
linter/all-rules/large/dataset.py          1.00     14.1±0.06ms     2.9 MB/sec    1.00     14.2±0.07ms     2.9 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.5±0.02ms     4.7 MB/sec    1.00      3.6±0.01ms     4.7 MB/sec
linter/all-rules/numpy/globals.py          1.00    380.5±1.10µs     7.8 MB/sec    1.00    380.0±4.36µs     7.8 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.3±0.01ms     4.0 MB/sec    1.01      6.3±0.03ms     4.0 MB/sec
linter/default-rules/large/dataset.py      1.00      7.1±0.03ms     5.7 MB/sec    1.02      7.3±0.02ms     5.6 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1459.0±3.02µs    11.4 MB/sec    1.02  1482.4±50.94µs    11.2 MB/sec
linter/default-rules/numpy/globals.py      1.00    157.4±1.72µs    18.7 MB/sec    1.00    157.5±0.23µs    18.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.2±0.02ms     8.0 MB/sec    1.00      3.2±0.04ms     8.0 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.03     11.7±0.30ms     3.5 MB/sec    1.00     11.4±0.29ms     3.6 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.6±0.08ms     6.4 MB/sec    1.01      2.6±0.18ms     6.3 MB/sec
formatter/numpy/globals.py                 1.01   304.2±25.25µs     9.7 MB/sec    1.00   300.2±16.81µs     9.8 MB/sec
formatter/pydantic/types.py                1.00      5.7±0.16ms     4.5 MB/sec    1.00      5.7±0.18ms     4.5 MB/sec
linter/all-rules/large/dataset.py          1.05     20.0±0.40ms     2.0 MB/sec    1.00     19.1±0.42ms     2.1 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.03      5.1±0.15ms     3.2 MB/sec    1.00      5.0±0.13ms     3.3 MB/sec
linter/all-rules/numpy/globals.py          1.03   617.2±25.63µs     4.8 MB/sec    1.00   601.6±15.96µs     4.9 MB/sec
linter/all-rules/pydantic/types.py         1.05      9.0±0.25ms     2.8 MB/sec    1.00      8.6±0.20ms     3.0 MB/sec
linter/default-rules/large/dataset.py      1.10     10.6±0.26ms     3.8 MB/sec    1.00      9.6±0.19ms     4.2 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.04      2.2±0.05ms     7.7 MB/sec    1.00      2.1±0.07ms     8.0 MB/sec
linter/default-rules/numpy/globals.py      1.04   255.6±10.60µs    11.5 MB/sec    1.00   246.2±10.11µs    12.0 MB/sec
linter/default-rules/pydantic/types.py     1.08      4.7±0.10ms     5.5 MB/sec    1.00      4.3±0.13ms     5.9 MB/sec

Base automatically changed from assignment-left-to-right to main July 13, 2023 14:00
@MichaReiser
Copy link
Member Author

MichaReiser commented Jul 13, 2023

Oh wow... I need to look into performance. Something's messed up 😮 I would have expected this change to improve performance and not regress it by 20%

@MichaReiser MichaReiser force-pushed the handle-arith-like-expr branch from 283068d to 58a79c1 Compare July 13, 2023 14:23
@MichaReiser MichaReiser added the formatter Related to the formatter label Jul 13, 2023
@MichaReiser MichaReiser merged commit 8187bf9 into main Jul 14, 2023
@MichaReiser MichaReiser deleted the handle-arith-like-expr branch July 14, 2023 15:55
@MichaReiser
Copy link
Member Author

These changes were also necessary to get the with item parentheses formatting closer to Black

evanrittenhouse pushed a commit to evanrittenhouse/ruff that referenced this pull request Jul 19, 2023
konstin pushed a commit that referenced this pull request Jul 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.

2 participants