Skip to content

Can omit parentheses layout for patterns #6933

@MichaReiser

Description

@MichaReiser

Ruff avoids adding parentheses to expressions that start or end with a parenthesized node (see can_omit_parentheses).

This is implemented by using optional_parentheses in conjunction with the in_parentheses_only_* builders. We need something similar for patterns to support

match test:
    case A | B | ["Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]: ...

# Formatted
match test:
    case (
        A
        | B
        | ["Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]
    ):
        ...

# Instead of
match test:
    case A | B | [
        "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
    ]:
        ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationformatterRelated to the formatterstyleHow should formatted code look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions