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 PatternMatchMapping #6644

Closed
Tracked by #5834
MichaReiser opened this issue Aug 17, 2023 · 0 comments · Fixed by #6836
Closed
Tracked by #5834

Format PatternMatchMapping #6644

MichaReiser opened this issue Aug 17, 2023 · 0 comments · Fixed by #6836
Assignees
Labels
formatter Related to the formatter help wanted Contributions especially welcome

Comments

@MichaReiser
Copy link
Member

MichaReiser commented Aug 17, 2023

Implement formatting for {...} patterns

match x:
    case {1: _, 2: _}:
        ...
    case {**rest}:
        ..

This should probably be similar to dict formatting.

@MichaReiser MichaReiser added formatter Related to the formatter help wanted Contributions especially welcome labels Aug 17, 2023
@MichaReiser MichaReiser added this to the Formatter: Alpha milestone Aug 22, 2023
@charliermarsh charliermarsh self-assigned this Aug 24, 2023
charliermarsh added a commit that referenced this issue Aug 25, 2023
<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

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

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

Unfortunately, this node has _three_ kinds of dangling comments:

```python
{  # "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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants