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

Add global and nonlocal formatting #6170

Merged
merged 1 commit into from
Jul 29, 2023
Merged

Conversation

charliermarsh
Copy link
Member

Summary

Adds global and nonlocal formatting, without the "deviation from black" outlined in the linked issue, which I'll do separately.

See: #4798.

Test Plan

Added a fixture in the Ruff-specific directory since the Black fixtures don't seem to cover this.

@charliermarsh charliermarsh added the formatter Related to the formatter label Jul 29, 2023

f.join_with(format_args![text(","), space()])
.entries(item.names.iter().formatted())
.finish()
Copy link
Member Author

Choose a reason for hiding this comment

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

Straightforward I think because the grammar is so limited here? These have to be identifiers, and they can't contain any content in-between.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 29, 2023

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.02      8.6±0.17ms     4.7 MB/sec    1.00      8.5±0.09ms     4.8 MB/sec
formatter/numpy/ctypeslib.py               1.00  1669.9±29.06µs    10.0 MB/sec    1.00  1665.1±31.76µs    10.0 MB/sec
formatter/numpy/globals.py                 1.00    180.6±2.94µs    16.3 MB/sec    1.01    182.7±5.28µs    16.1 MB/sec
formatter/pydantic/types.py                1.01      3.6±0.11ms     7.0 MB/sec    1.00      3.6±0.07ms     7.1 MB/sec
linter/all-rules/large/dataset.py          1.00     11.3±0.14ms     3.6 MB/sec    1.00     11.3±0.11ms     3.6 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      2.8±0.02ms     5.9 MB/sec    1.00      2.8±0.01ms     5.9 MB/sec
linter/all-rules/numpy/globals.py          1.00    381.6±1.34µs     7.7 MB/sec    1.00    382.6±0.94µs     7.7 MB/sec
linter/all-rules/pydantic/types.py         1.01      5.1±0.09ms     5.0 MB/sec    1.00      5.0±0.05ms     5.1 MB/sec
linter/default-rules/large/dataset.py      1.00      6.0±0.08ms     6.8 MB/sec    1.00      6.0±0.04ms     6.8 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1242.7±7.66µs    13.4 MB/sec    1.00   1244.3±6.16µs    13.4 MB/sec
linter/default-rules/numpy/globals.py      1.00    135.8±0.19µs    21.7 MB/sec    1.00    136.4±0.21µs    21.6 MB/sec
linter/default-rules/pydantic/types.py     1.00      2.6±0.03ms     9.7 MB/sec    1.01      2.6±0.03ms     9.6 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00     12.1±0.34ms     3.4 MB/sec    1.04     12.6±0.43ms     3.2 MB/sec
formatter/numpy/ctypeslib.py               1.00      2.4±0.09ms     7.0 MB/sec    1.03      2.4±0.06ms     6.8 MB/sec
formatter/numpy/globals.py                 1.00   269.2±16.30µs    11.0 MB/sec    1.05   282.5±14.75µs    10.4 MB/sec
formatter/pydantic/types.py                1.00      5.3±0.18ms     4.8 MB/sec    1.02      5.4±0.20ms     4.7 MB/sec
linter/all-rules/large/dataset.py          1.00     16.7±0.43ms     2.4 MB/sec    1.03     17.2±0.75ms     2.4 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      4.4±0.15ms     3.8 MB/sec    1.03      4.5±0.14ms     3.7 MB/sec
linter/all-rules/numpy/globals.py          1.00   532.1±19.71µs     5.5 MB/sec    1.06   566.1±22.66µs     5.2 MB/sec
linter/all-rules/pydantic/types.py         1.00      7.5±0.26ms     3.4 MB/sec    1.03      7.7±0.30ms     3.3 MB/sec
linter/default-rules/large/dataset.py      1.00      9.3±0.24ms     4.4 MB/sec    1.06      9.9±0.25ms     4.1 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1886.0±56.77µs     8.8 MB/sec    1.05  1989.7±58.01µs     8.4 MB/sec
linter/default-rules/numpy/globals.py      1.00   225.1±12.24µs    13.1 MB/sec    1.03    232.5±9.71µs    12.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      4.0±0.16ms     6.3 MB/sec    1.06      4.3±0.14ms     6.0 MB/sec

@charliermarsh
Copy link
Member Author

Line-breaking behavior in #6172.

@charliermarsh
Copy link
Member Author

Do we care about DRYing this up? If so, what would be the right home for that?

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.

Thank you. I'm surprised that there are no black tests...

Do we care about DRYing this up? If so, what would be the right home for that?

I do, for more complex implementation but the abstraction would probably require significantly more code than what we would safe.

The way I would re-use the code here is by creating a FormatNames struct inside either of the file that accepts the names and implements Format.

crates/ruff_python_formatter/src/statement/stmt_global.rs Outdated Show resolved Hide resolved
crates/ruff_python_formatter/src/statement/stmt_global.rs Outdated Show resolved Hide resolved
@charliermarsh charliermarsh enabled auto-merge (squash) July 29, 2023 14:30
@charliermarsh charliermarsh merged commit 76741ca into main Jul 29, 2023
16 checks passed
@charliermarsh charliermarsh deleted the charlie/global-nonlocal branch July 29, 2023 14:39
@konstin konstin mentioned this pull request Jul 31, 2023
72 tasks
charliermarsh added a commit that referenced this pull request Aug 2, 2023
## Summary

Builds on #6170 to break `global` and `nonlocal` statements, such that
we get:

```python
def f():
    global \
        analyze_featuremap_layer, \
        analyze_featuremapcompression_layer, \
        analyze_latencies_post, \
        analyze_motions_layer, \
        analyze_size_model
```

Instead of:

```python
def f():
    global analyze_featuremap_layer, analyze_featuremapcompression_layer, analyze_latencies_post, analyze_motions_layer, analyze_size_model
```

Notably, we avoid applying this formatting if the statement ends in a
comment. Otherwise, the comment would _need_ to be placed after the last
item, like:

```python
def f():
    global \
        analyze_featuremap_layer, \
        analyze_featuremapcompression_layer, \
        analyze_latencies_post, \
        analyze_motions_layer, \
        analyze_size_model  # noqa
```

To me, this seems wrong (and would break the `# noqa` comment). Ideally,
the items would be parenthesized, and the comment would be on the inner
parenthesis, like:

```python
def f():
    global (  # noqa
        analyze_featuremap_layer,
        analyze_featuremapcompression_layer,
        analyze_latencies_post,
        analyze_motions_layer,
        analyze_size_model
    )
```

But that's not valid syntax.
@MichaReiser MichaReiser added this to the Formatter: Alpha milestone Aug 16, 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