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

Incorrect formatting of comments in match statement for tuple without parentheses #8091

Closed
doraut opened this issue Oct 20, 2023 · 2 comments · Fixed by #8101
Closed

Incorrect formatting of comments in match statement for tuple without parentheses #8091

doraut opened this issue Oct 20, 2023 · 2 comments · Fixed by #8101
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@doraut
Copy link

doraut commented Oct 20, 2023

Version: 0.1.1
Command: ruff format --isolated example.py
Playground: Example

Example

def fizzbuzz(n):
    match n % 3, n % 5:
        case 0, 0:
            # n is divisible by both 3 and 5
            print("FizzBuzz")
        case 0, _:
            # n is divisible by 3, but not 5
            print("Fizz")
        case _, 0:
            # n is divisible by 5, but not 3
            print("Buzz")
        case _:
            print(n)

This is formatted to

def fizzbuzz(n):
    match (
        n % 3,
        n % 5,
        # n is divisible by both 3 and 5
        # n is divisible by 3, but not 5
        # n is divisible by 5, but not 3
    ):
        case 0, 0:
            print("FizzBuzz")
        case 0, _:
            print("Fizz")
        case _, 0:
            print("Buzz")
        case _:
            print(n)

Adding parentheses around the matched tuple (match n % 3, n % 5 -> match (n % 3, n % 5)) results in expected formatting.

@MichaReiser
Copy link
Member

Uhm that's weird. Thanks for reporting

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter labels Oct 20, 2023
@MichaReiser MichaReiser added this to the Formatter: Beta milestone Oct 20, 2023
@charliermarsh charliermarsh self-assigned this Oct 21, 2023
@charliermarsh
Copy link
Member

This is a parser bug -- the ranges are off.

charliermarsh added a commit that referenced this issue Oct 22, 2023
## Summary

This was just a bug in the parser ranges, probably since it was
initially implemented. Given `match n % 3, n % 5: ...`, the "subject"
(i.e., the tuple of two binary operators) was using the entire range of
the `match` statement.

Closes #8091.

## Test Plan

`cargo test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants