Skip to content

Add empty lines before nested functions and classes#6206

Merged
charliermarsh merged 1 commit intomainfrom
charlie/nested-function
Aug 1, 2023
Merged

Add empty lines before nested functions and classes#6206
charliermarsh merged 1 commit intomainfrom
charlie/nested-function

Conversation

@charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Jul 31, 2023

Summary

This PR ensures that if a function or class is the first statement in a nested suite that isn't a function or class body, we insert a leading newline.

For example, given:

def f():
    if True:

        def register_type():
            pass

We want to preserve the newline, whereas today, we remove it.

Note that this only applies when the function or class doesn't have any leading comments.

Closes #6066.

@charliermarsh charliermarsh requested a review from konstin July 31, 2023 19:11
@charliermarsh charliermarsh force-pushed the charlie/nested-function branch 2 times, most recently from eff492c to 4b4f3fd Compare July 31, 2023 19:13
@github-actions
Copy link
Contributor

github-actions bot commented Jul 31, 2023

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      8.2±0.13ms     5.0 MB/sec    1.05      8.6±0.11ms     4.7 MB/sec
formatter/numpy/ctypeslib.py               1.00  1625.3±25.34µs    10.2 MB/sec    1.06  1722.8±37.61µs     9.7 MB/sec
formatter/numpy/globals.py                 1.00    182.0±3.79µs    16.2 MB/sec    1.07    194.3±7.01µs    15.2 MB/sec
formatter/pydantic/types.py                1.00      3.6±0.12ms     7.2 MB/sec    1.04      3.7±0.09ms     6.9 MB/sec
linter/all-rules/large/dataset.py          1.00     11.0±0.08ms     3.7 MB/sec    1.01     11.1±0.10ms     3.7 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      2.8±0.01ms     6.0 MB/sec    1.01      2.8±0.03ms     5.9 MB/sec
linter/all-rules/numpy/globals.py          1.00    380.8±0.36µs     7.7 MB/sec    1.00    381.8±0.66µs     7.7 MB/sec
linter/all-rules/pydantic/types.py         1.00      4.9±0.08ms     5.2 MB/sec    1.03      5.1±0.13ms     5.0 MB/sec
linter/default-rules/large/dataset.py      1.00      5.8±0.05ms     7.0 MB/sec    1.01      5.8±0.06ms     7.0 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1204.9±21.81µs    13.8 MB/sec    1.00  1210.6±12.42µs    13.8 MB/sec
linter/default-rules/numpy/globals.py      1.00    134.0±3.45µs    22.0 MB/sec    1.00    134.2±0.22µs    22.0 MB/sec
linter/default-rules/pydantic/types.py     1.00      2.6±0.06ms     9.9 MB/sec    1.00      2.6±0.04ms     9.9 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00     10.1±0.15ms     4.0 MB/sec    1.02     10.2±0.15ms     4.0 MB/sec
formatter/numpy/ctypeslib.py               1.00  1943.7±32.39µs     8.6 MB/sec    1.00  1939.2±26.89µs     8.6 MB/sec
formatter/numpy/globals.py                 1.00    211.1±4.61µs    14.0 MB/sec    1.01    212.9±6.92µs    13.9 MB/sec
formatter/pydantic/types.py                1.00      4.3±0.07ms     5.9 MB/sec    1.00      4.3±0.06ms     5.9 MB/sec
linter/all-rules/large/dataset.py          1.02     14.0±0.17ms     2.9 MB/sec    1.00     13.7±0.15ms     3.0 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.02      3.7±0.06ms     4.6 MB/sec    1.00      3.6±0.04ms     4.7 MB/sec
linter/all-rules/numpy/globals.py          1.02    446.9±7.70µs     6.6 MB/sec    1.00    440.1±8.07µs     6.7 MB/sec
linter/all-rules/pydantic/types.py         1.02      6.4±0.19ms     4.0 MB/sec    1.00      6.2±0.13ms     4.1 MB/sec
linter/default-rules/large/dataset.py      1.01      7.6±0.12ms     5.3 MB/sec    1.00      7.5±0.11ms     5.4 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1520.4±35.64µs    11.0 MB/sec    1.01  1536.2±29.39µs    10.8 MB/sec
linter/default-rules/numpy/globals.py      1.00    169.4±3.36µs    17.4 MB/sec    1.01    171.1±3.55µs    17.2 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.3±0.04ms     7.8 MB/sec    1.01      3.3±0.04ms     7.7 MB/sec

@charliermarsh charliermarsh force-pushed the charlie/move-joiner branch 2 times, most recently from c7c4e82 to 5391d50 Compare July 31, 2023 19:46
@charliermarsh charliermarsh force-pushed the charlie/nested-function branch 2 times, most recently from 1e4b76d to c046c4a Compare July 31, 2023 19:54
@charliermarsh charliermarsh force-pushed the charlie/move-joiner branch 2 times, most recently from 8a36ffa to 6882acf Compare July 31, 2023 20:33
Base automatically changed from charlie/move-joiner to main July 31, 2023 20:58
@charliermarsh charliermarsh force-pushed the charlie/nested-function branch from c046c4a to 17f5446 Compare July 31, 2023 21:02
@charliermarsh charliermarsh added the formatter Related to the formatter label Aug 1, 2023
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Note that this only applies when the function or class doesn't have any leading comments.

Because Black only inserts the new line when there's no leading comment? This feels a bit strange to me.

@charliermarsh charliermarsh force-pushed the charlie/nested-function branch from 17f5446 to 5cef5a1 Compare August 1, 2023 15:20
@charliermarsh charliermarsh enabled auto-merge (squash) August 1, 2023 15:21
@charliermarsh charliermarsh merged commit 928ab63 into main Aug 1, 2023
@charliermarsh charliermarsh deleted the charlie/nested-function branch August 1, 2023 15:31
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: Preserve leading empty lines of indented classes and functions

3 participants