Skip to content

Add support for PatternMatchMapping formatting#6836

Merged
charliermarsh merged 1 commit intomainfrom
charlie/mapping
Aug 25, 2023
Merged

Add support for PatternMatchMapping formatting#6836
charliermarsh merged 1 commit intomainfrom
charlie/mapping

Conversation

@charliermarsh
Copy link
Member

Summary

Adds support for PatternMatchMapping -- i.e., cases like:

match foo:
    case {"a": 1, "b": 2, **rest}:
        pass

Unfortunately, this node has three kinds of dangling comments:

{  # "open parenthesis comment"
   key: pattern,
   **  # end-of-line "double star comment"
   # own-line "double star comment"
   rest  # end-of-line "after rest comment"
   # own-line "after rest comment"
}

Some of the complexity comes from the fact that in **rest, rest is an identifier, not a node, so we have to handle comments after it as dangling on the enclosing node, rather than trailing on **rest. (We could change the AST to use PatternMatchAs there, which would be more permissive than the grammar but not totally crazy -- PatternMatchAs is used elsewhere to mean "a single identifier".)

Closes #6644.

Test Plan

cargo test

@charliermarsh charliermarsh force-pushed the charlie/mapping branch 3 times, most recently from fe109a9 to 507f5bc Compare August 24, 2023 04:43
@charliermarsh charliermarsh added the formatter Related to the formatter label Aug 24, 2023
@charliermarsh
Copy link
Member Author

We may want to instead change the rest in **rest to a Pattern, to simplify the comment handling -- I've put that change up here (#6838). If that seems like a reasonable idea, I'll merge that first, then rework this PR.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 24, 2023

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      4.5±0.13ms     9.1 MB/sec    1.00      4.5±0.11ms     9.1 MB/sec
formatter/numpy/ctypeslib.py               1.00   947.3±28.18µs    17.6 MB/sec    1.00   946.5±43.83µs    17.6 MB/sec
formatter/numpy/globals.py                 1.00     87.4±3.65µs    33.8 MB/sec    1.02     89.1±3.84µs    33.1 MB/sec
formatter/pydantic/types.py                1.00  1852.6±57.87µs    13.8 MB/sec    1.02  1885.8±97.98µs    13.5 MB/sec
linter/all-rules/large/dataset.py          1.04     12.5±0.27ms     3.2 MB/sec    1.00     12.1±0.22ms     3.4 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.03      3.3±0.07ms     5.0 MB/sec    1.00      3.2±0.07ms     5.2 MB/sec
linter/all-rules/numpy/globals.py          1.02   472.4±18.36µs     6.2 MB/sec    1.00   462.7±15.09µs     6.4 MB/sec
linter/all-rules/pydantic/types.py         1.04      6.5±0.14ms     3.9 MB/sec    1.00      6.3±0.10ms     4.1 MB/sec
linter/default-rules/large/dataset.py      1.05      6.7±0.14ms     6.0 MB/sec    1.00      6.4±0.12ms     6.4 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1440.8±27.79µs    11.6 MB/sec    1.01  1454.9±97.40µs    11.4 MB/sec
linter/default-rules/numpy/globals.py      1.05    177.8±5.98µs    16.6 MB/sec    1.00    169.6±3.12µs    17.4 MB/sec
linter/default-rules/pydantic/types.py     1.02      3.0±0.08ms     8.4 MB/sec    1.00      3.0±0.06ms     8.6 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      4.7±0.06ms     8.6 MB/sec    1.02      4.8±0.07ms     8.5 MB/sec
formatter/numpy/ctypeslib.py               1.00   970.0±18.87µs    17.2 MB/sec    1.01   980.3±19.30µs    17.0 MB/sec
formatter/numpy/globals.py                 1.00     92.9±1.92µs    31.7 MB/sec    1.04     96.3±4.72µs    30.6 MB/sec
formatter/pydantic/types.py                1.00  1937.4±25.48µs    13.2 MB/sec    1.01  1960.0±36.18µs    13.0 MB/sec
linter/all-rules/large/dataset.py          1.00     13.0±0.15ms     3.1 MB/sec    1.00     13.0±0.16ms     3.1 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.5±0.04ms     4.7 MB/sec    1.01      3.5±0.04ms     4.7 MB/sec
linter/all-rules/numpy/globals.py          1.00    436.9±5.45µs     6.8 MB/sec    1.00    438.1±6.38µs     6.7 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.6±0.09ms     3.9 MB/sec    1.01      6.7±0.10ms     3.8 MB/sec
linter/default-rules/large/dataset.py      1.00      7.0±0.07ms     5.8 MB/sec    1.01      7.1±0.09ms     5.7 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1508.7±23.86µs    11.0 MB/sec    1.01  1524.4±17.70µs    10.9 MB/sec
linter/default-rules/numpy/globals.py      1.00    175.7±2.63µs    16.8 MB/sec    1.00    176.2±3.73µs    16.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.2±0.04ms     7.9 MB/sec    1.00      3.2±0.04ms     7.9 MB/sec

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.

I love it how you came in and are pushing match formatting over the line!

let dangling = comments.dangling(item);

if keys.is_empty() && rest.is_none() {
return empty_parenthesized("{", dangling, "}").fmt(f);
Copy link
Member

Choose a reason for hiding this comment

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

Lol, Our DSL sometimes is too powerful. I was like, whoot, since when does &[SourceComment] implement Format but it's empty_parenthesized that accepts comments.

@charliermarsh charliermarsh force-pushed the charlie/mapping branch 2 times, most recently from 43d68ac to 23ec21d Compare August 25, 2023 04:24
@charliermarsh charliermarsh enabled auto-merge (squash) August 25, 2023 04:27
@charliermarsh charliermarsh merged commit 1044d66 into main Aug 25, 2023
@charliermarsh charliermarsh deleted the charlie/mapping branch August 25, 2023 04:33
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.

Format PatternMatchMapping

3 participants