Skip to content

Can omit parentheses layout for patterns #6933

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",
    ]:
        ...

Activity

  1. added this to the Formatter: Beta milestone on Aug 28, 2023
  2. dhruvmanila commented on Sep 25, 2023

    @dhruvmanila
    Member

    This seems to be fixed: https://play.ruff.rs/e7f95918-d71e-479f-8fae-29cdaa476463

    Closing the issue but feel free to re-open it if you think otherwise.

  3. MichaReiser commented on Sep 25, 2023

    @MichaReiser
    MemberAuthor

    This isn't fixed yet. Notice how the expression gets wrapped in parentheses.

    We need to avoid adding the parentheses in this case. I'm not sure why the function isn't using maybe_parenthesize(IfBreaks) directly

    if has_comments {
    pattern.format().with_options(Parentheses::Always).fmt(f)?;
    } else {
    match pattern.needs_parentheses(item.as_any_node_ref(), f.context()) {
    OptionalParentheses::Multiline => {
    parenthesize_if_expands(
    &pattern.format().with_options(Parentheses::Never),
    )
    .fmt(f)?;
    }
    OptionalParentheses::Always => {
    pattern.format().with_options(Parentheses::Always).fmt(f)?;
    }
    OptionalParentheses::Never => {
    pattern.format().with_options(Parentheses::Never).fmt(f)?;
    }
    OptionalParentheses::BestFit => {
    pattern.format().with_options(Parentheses::Never).fmt(f)?;
    }
    }
    }

  4. charliermarsh commented on Sep 27, 2023

    @charliermarsh
    Member

    We want to make this change (it improves consistency), but it's not blocking for the Beta.

    The challenge here is that we need to re-implement can_omit_parentheses for patterns, since that logic only applies to expressions.

  5. MichaReiser commented on Feb 8, 2024

    @MichaReiser
    MemberAuthor

    I'm going to remove this from the stable release since it hasn't come up often.

  6. removed this from the Formatter: Stable milestone on Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationformatterRelated to the Ruff formatterstyleHow should Ruff-formatted code look

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions