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

Format PatternMatchSequence #6676

Merged
merged 16 commits into from
Aug 23, 2023

Conversation

harupy
Copy link
Contributor

@harupy harupy commented Aug 18, 2023

Close #6645

Summary

Test Plan

Do I need to add extra tests?

.nodes(patterns.iter())
.finish()
});
parenthesized("[", &items, "]")
Copy link
Contributor Author

@harupy harupy Aug 18, 2023

Choose a reason for hiding this comment

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

Oh case (...) is also PatternMatchSequence. I need to fix this.

@MichaReiser
Copy link
Member

Do I need to add extra tests?

You can add additional tests to crates/ruff_python_formatter/resources/test/fixtures/ruff.

We like to add additional tests for handling comments because they tend to be tricky and black's test suite only has very few cases for it.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 18, 2023

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      4.0±0.03ms    10.2 MB/sec    1.00      4.0±0.02ms    10.2 MB/sec
formatter/numpy/ctypeslib.py               1.00    844.8±3.91µs    19.7 MB/sec    1.01    851.4±8.66µs    19.6 MB/sec
formatter/numpy/globals.py                 1.00     90.8±0.63µs    32.5 MB/sec    1.00     91.2±0.51µs    32.4 MB/sec
formatter/pydantic/types.py                1.00   1651.5±9.16µs    15.4 MB/sec    1.00  1657.8±26.39µs    15.4 MB/sec
linter/all-rules/large/dataset.py          1.00     12.0±0.04ms     3.4 MB/sec    1.00     12.1±0.05ms     3.4 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.3±0.02ms     5.1 MB/sec    1.00      3.3±0.01ms     5.1 MB/sec
linter/all-rules/numpy/globals.py          1.03    457.4±2.07µs     6.5 MB/sec    1.00    444.9±8.05µs     6.6 MB/sec
linter/all-rules/pydantic/types.py         1.02      6.4±0.23ms     4.0 MB/sec    1.00      6.3±0.08ms     4.1 MB/sec
linter/default-rules/large/dataset.py      1.03      6.5±0.02ms     6.3 MB/sec    1.00      6.3±0.09ms     6.5 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.01   1443.0±4.78µs    11.5 MB/sec    1.00  1429.7±12.72µs    11.6 MB/sec
linter/default-rules/numpy/globals.py      1.00    165.2±0.43µs    17.9 MB/sec    1.00    165.9±0.47µs    17.8 MB/sec
linter/default-rules/pydantic/types.py     1.00      2.9±0.01ms     8.7 MB/sec    1.00      3.0±0.01ms     8.6 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      3.7±0.07ms    11.0 MB/sec    1.00      3.7±0.06ms    10.9 MB/sec
formatter/numpy/ctypeslib.py               1.01   763.0±11.59µs    21.8 MB/sec    1.00   759.0±10.86µs    21.9 MB/sec
formatter/numpy/globals.py                 1.00     79.0±2.04µs    37.3 MB/sec    1.01     79.7±1.39µs    37.0 MB/sec
formatter/pydantic/types.py                1.00  1528.3±27.20µs    16.7 MB/sec    1.01  1542.0±53.78µs    16.5 MB/sec
linter/all-rules/large/dataset.py          1.00     12.5±0.18ms     3.3 MB/sec    1.02     12.7±0.44ms     3.2 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.5±0.05ms     4.8 MB/sec    1.02      3.5±0.07ms     4.7 MB/sec
linter/all-rules/numpy/globals.py          1.00    431.8±4.83µs     6.8 MB/sec    1.01    435.6±5.82µs     6.8 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.5±0.06ms     3.9 MB/sec    1.02      6.6±0.11ms     3.8 MB/sec
linter/default-rules/large/dataset.py      1.00      7.0±0.10ms     5.8 MB/sec    1.02      7.2±0.13ms     5.7 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1490.7±24.44µs    11.2 MB/sec    1.02  1526.4±29.92µs    10.9 MB/sec
linter/default-rules/numpy/globals.py      1.00    169.1±2.52µs    17.5 MB/sec    1.03    173.4±3.42µs    17.0 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.1±0.05ms     8.1 MB/sec    1.03      3.2±0.06ms     7.9 MB/sec

@harupy
Copy link
Contributor Author

harupy commented Aug 19, 2023

@MichaReiser Got it, I will add tests

@konstin konstin added the formatter Related to the formatter label Aug 21, 2023
@harupy harupy force-pushed the format-FormatPatternSequence branch from e9e5268 to 16fba95 Compare August 21, 2023 09:40
}
SequenceType::TupleWithoutParentheses => {
let items = format_with(|f| {
f.join_with(&format_args![text(","), space()])
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment why you're not using join_comma_separated here

@@ -141,3 +141,26 @@ def foo():
no_comments
):
pass

match foo:
case "a", "b":
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With join_comma_separated, this is formatted to:

    case "NOT_YET_IMPLEMENTED_PatternMatchValue",
    "NOT_YET_IMPLEMENTED_PatternMatchValue",:

which is a syntax error.

Copy link
Contributor Author

@harupy harupy Aug 21, 2023

Choose a reason for hiding this comment

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

Should this be formatted like this?

    case "NOT_YET_IMPLEMENTED_PatternMatchValue", "NOT_YET_IMPLEMENTED_PatternMatchValue":

Copy link
Contributor Author

Choose a reason for hiding this comment

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

or

    case (
        "NOT_YET_IMPLEMENTED_PatternMatchValue",
        "NOT_YET_IMPLEMENTED_PatternMatchValue",
    ):
        pass

?

Copy link
Member

Choose a reason for hiding this comment

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

not sure tbh. black does not add parentheses even if the line is too long, but i also think it would be good if we would break instead of writing an overlong lines. Also black's behavior is inconsistent with other overlong tuples, e.g.

match x:
    case "NOT_YET_IMPLEMENTED_PatternMatchValue", "NOT_YET_IMPLEMENTED_PatternMatchValue", "aslödjhkfö":
        pass

b = "NOT_YET_IMPLEMENTED_PatternMatchValue", "NOT_YET_IMPLEMENTED_PatternMatchValue", "aslödjhkfö"

becomes

match x:
    case "NOT_YET_IMPLEMENTED_PatternMatchValue", "NOT_YET_IMPLEMENTED_PatternMatchValue", "aslödjhkfö":
        pass

b = (
    "NOT_YET_IMPLEMENTED_PatternMatchValue",
    "NOT_YET_IMPLEMENTED_PatternMatchValue",
    "aslödjhkfö",
)

CC @MichaReiser what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not well at the moment and this is taking more brain capacity than I have right now. I hope to get back to this tomorrow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i also think it would be good if we would break instead of writing an overlong lines.

+1

Copy link
Member

Choose a reason for hiding this comment

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

The sequence pattern is a bit strange IMO because matching [a, b] or (a, b) or a, b are semantically identical. Meaning, the sequence is neither a list nor a tuple... it's its own thing.

But I agree, I would expect the sequences to break the same as tuples (and lists) do

Copy link
Contributor Author

@harupy harupy Aug 22, 2023

Choose a reason for hiding this comment

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

psf/black#3726 and psf/black#3403 seem related.

});
match sequence_type {
SequenceType::Tuple | SequenceType::TupleWithoutParentheses => {
parenthesized("(", &items, ")")
Copy link
Contributor Author

@harupy harupy Aug 21, 2023

Choose a reason for hiding this comment

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

This is still not correct because parenthesized always wraps the sequence with ().

@charliermarsh charliermarsh enabled auto-merge (squash) August 23, 2023 00:35
@charliermarsh
Copy link
Member

Did a quick read -- gonna merge to keep things moving along. Can always address follow-ups in new PRs.

@charliermarsh charliermarsh merged commit 94f5f18 into astral-sh:main Aug 23, 2023
16 checks passed
@harupy harupy deleted the format-FormatPatternSequence branch August 23, 2023 00:57
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 PatternMatchSequence
4 participants