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 PatternMatchClass #6860

Merged
merged 3 commits into from
Aug 25, 2023

Conversation

LaBatata101
Copy link
Contributor

@LaBatata101 LaBatata101 commented Aug 24, 2023

Summary

Closes #6642

Test Plan

cargo test

@LaBatata101
Copy link
Contributor Author

I don't know how to solve the conflict for the crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap file

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.

So many removed lines ☺️

join.finish()
});

write!(f, [cls.format(), parenthesized("(", &items, ")")])
Copy link
Member

Choose a reason for hiding this comment

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

Try adding an extra group around items

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would the function group work here?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, group(&items) should do the trick if I'm not mistaken

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nothing changed for this crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap file when using group

Copy link
Member

Choose a reason for hiding this comment

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

See my comment below. This is fine, assuming we're talking about the same mismatch.

@LaBatata101 LaBatata101 changed the title Format PatternMatchClass Format PatternMatchClass Aug 24, 2023
@charliermarsh charliermarsh added the formatter Related to the formatter label Aug 25, 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.

This looks good to me. Thank you.

Could you add one test case to

https://github.com/astral-sh/ruff/blob/main/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py#L285

that tests for the following weird comment placement

match x:
	case (
		Point2D
		# awkward
		(x = 2)
	: ...

- ):
+ case NOT_YET_IMPLEMENTED_PatternMatchClass(0, 0):
+ normal=x,
+ perhaps=[list, {"NOT_YET_IMPLEMENTED_PatternMatchMapping": _, 2: _}] as y,
Copy link
Member

Choose a reason for hiding this comment

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

Okay, the problem here is that PatternMatchMapping isn't yet implemented and the text emitted by the "fake" implementation is much longer than the expected string.

We can ignore this.

join.finish()
});

write!(f, [cls.format(), parenthesized("(", &items, ")")])
Copy link
Member

Choose a reason for hiding this comment

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

See my comment below. This is fine, assuming we're talking about the same mismatch.

join.nodes(patterns.iter());
}

if !kwd_attrs.is_empty() {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need the if !kwd_attrs.is_empty()?

# trailing
# trailing
):
...
Copy link
Member

Choose a reason for hiding this comment

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

Could you add something like

match a:
    case A(
        b # b
        = # c
        2 # d
        # e
    ):
        pass

with the left hand side and the right hand side split of the assignment split by comment? I wanted to add an additional comment before the b but that's blocked by #6866

@konstin
Copy link
Member

konstin commented Aug 25, 2023

I don't know how to solve the conflict for the crates/ruff_python_formatter/tests/snapshots/black_compatibility@py_310__pattern_matching_extras.py.snap file

Just pick anything (it's an autogenerated file) and recreate file, e.g. with INSTA_UPDATE=always cargo test -p ruff_python_formatter.

@charliermarsh
Copy link
Member

Will review and merge today. I can also follow-up with new tests once I fix #6866.

@charliermarsh charliermarsh self-assigned this Aug 25, 2023
@charliermarsh
Copy link
Member

Okay, I had to make some changes to the PR to adapt to our parenthesis handling for patterns that changed a bit upstream (mostly, to ensure we handle the single-argument case correctly, like we do for call expressions). We also now handle comments before the arguments, like we do for call expressions.

There are several comment cases that we're still getting wrong (valid syntax, but slightly incorrect placement), but that are really hard to fix without changing the AST. (Namely, we need an Arguments node, and we need a single node for x=1 -- we have both of these things for call expressions so we should have them here too.)

I'm going to update the AST, then do another pass on this node.

...

case A(
b=# b
Copy link
Member

Choose a reason for hiding this comment

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

This is not quite right, but very tedious to fix without a node for the whole argument.

...

case Point2D(
# end of line
Copy link
Member

@charliermarsh charliermarsh Aug 25, 2023

Choose a reason for hiding this comment

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

This is not quite right (it should be immediately after the (), but very hard to fix without a node for the arguments.

@charliermarsh charliermarsh enabled auto-merge (squash) August 25, 2023 19:01
@charliermarsh charliermarsh merged commit 91a780c into astral-sh:main Aug 25, 2023
16 checks passed
@charliermarsh
Copy link
Member

Filed here: #6880. I'm on it.

@github-actions
Copy link
Contributor

PR Check Results

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      5.3±0.04ms     7.7 MB/sec    1.00      5.3±0.04ms     7.7 MB/sec
formatter/numpy/ctypeslib.py               1.00   1047.0±4.58µs    15.9 MB/sec    1.00   1046.9±2.82µs    15.9 MB/sec
formatter/numpy/globals.py                 1.00     96.5±0.54µs    30.6 MB/sec    1.03     99.2±0.45µs    29.7 MB/sec
formatter/pydantic/types.py                1.00   1966.4±4.69µs    13.0 MB/sec    1.02  1999.7±11.74µs    12.8 MB/sec
linter/all-rules/large/dataset.py          1.00     12.1±0.09ms     3.4 MB/sec    1.00     12.1±0.06ms     3.4 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.2±0.01ms     5.2 MB/sec    1.00      3.2±0.02ms     5.1 MB/sec
linter/all-rules/numpy/globals.py          1.00    452.5±1.02µs     6.5 MB/sec    1.02    461.0±4.45µs     6.4 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.3±0.03ms     4.1 MB/sec    1.00      6.3±0.05ms     4.0 MB/sec
linter/default-rules/large/dataset.py      1.00      6.4±0.02ms     6.4 MB/sec    1.00      6.3±0.03ms     6.4 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1401.3±3.38µs    11.9 MB/sec    1.00  1407.2±16.48µs    11.8 MB/sec
linter/default-rules/numpy/globals.py      1.00    162.3±1.29µs    18.2 MB/sec    1.03    167.2±3.38µs    17.7 MB/sec
linter/default-rules/pydantic/types.py     1.00      2.9±0.01ms     8.9 MB/sec    1.00      2.9±0.03ms     8.9 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      5.7±0.37ms     7.1 MB/sec    1.03      5.9±0.29ms     6.9 MB/sec
formatter/numpy/ctypeslib.py               1.00  1122.3±64.82µs    14.8 MB/sec    1.01  1135.1±81.47µs    14.7 MB/sec
formatter/numpy/globals.py                 1.00    100.6±6.85µs    29.3 MB/sec    1.10    110.9±9.45µs    26.6 MB/sec
formatter/pydantic/types.py                1.00      2.1±0.13ms    11.9 MB/sec    1.11      2.4±0.11ms    10.8 MB/sec
linter/all-rules/large/dataset.py          1.00     14.1±0.62ms     2.9 MB/sec    1.05     14.7±0.83ms     2.8 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.8±0.18ms     4.4 MB/sec    1.02      3.9±0.24ms     4.3 MB/sec
linter/all-rules/numpy/globals.py          1.07   501.1±39.89µs     5.9 MB/sec    1.00   470.5±23.62µs     6.3 MB/sec
linter/all-rules/pydantic/types.py         1.02      7.4±0.23ms     3.4 MB/sec    1.00      7.3±0.50ms     3.5 MB/sec
linter/default-rules/large/dataset.py      1.01      7.8±0.33ms     5.2 MB/sec    1.00      7.7±0.42ms     5.3 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1658.6±90.66µs    10.0 MB/sec    1.06  1756.2±72.91µs     9.5 MB/sec
linter/default-rules/numpy/globals.py      1.02   190.0±12.97µs    15.5 MB/sec    1.00    185.7±9.52µs    15.9 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.6±0.14ms     7.1 MB/sec    1.01      3.6±0.14ms     7.0 MB/sec

@LaBatata101 LaBatata101 deleted the format-pattern-match-class branch August 25, 2023 20:44
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 PatternMatchClass
4 participants