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

Parenthesize match..case if guards #13513

Merged
merged 1 commit into from
Sep 26, 2024
Merged

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Sep 25, 2024

Summary

Implements the black remove_redundant_guard_parens and parens_for_long_if_clauses_in_case_block preview styles.

Black deviation

Like assignments, ruff prefers splitting the right side (if guard) over splitting the left (case).

Black

match 1:
    case (
        _
    ) if True:  # comment over the line limit and parens should go to next line
        pass

    case {
        "long_long_long_key": str(long_long_long_key)
    } if value := "long long long long long long long long long long long value":
        pass

Ruff

match 1:
    case _ if (
        True
    ):  # comment over the line limit and parens should go to next line
        pass

    case {"long_long_long_key": str(long_long_long_key)} if (
        value := "long long long long long long long long long long long value"
    ):
        pass

Test Plan

There are a few existing tests that cover this already nicely. I added a few new tests to ensure that comments are handled correctly.

@MichaReiser MichaReiser mentioned this pull request Sep 25, 2024
24 tasks
@MichaReiser MichaReiser added formatter Related to the formatter preview Related to preview mode features labels Sep 25, 2024
@MichaReiser MichaReiser changed the base branch from main to micha/maybe-parenthesize-pattern September 25, 2024 16:19
Copy link
Contributor

github-actions bot commented Sep 25, 2024

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser marked this pull request as ready for review September 25, 2024 16:40
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Nice.

Base automatically changed from micha/maybe-parenthesize-pattern to main September 26, 2024 06:35
An error occurred while trying to automatically change base from micha/maybe-parenthesize-pattern to main September 26, 2024 06:35
@MichaReiser MichaReiser enabled auto-merge (squash) September 26, 2024 06:40
@MichaReiser MichaReiser merged commit 9442cd8 into main Sep 26, 2024
17 checks passed
@MichaReiser MichaReiser deleted the micha/case-header-if-guard branch September 26, 2024 06: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 preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants