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

Formatter: Parenthesize long expressions in dicts #8437

Closed
Tracked by #6935
zanieb opened this issue Nov 2, 2023 · 1 comment
Closed
Tracked by #6935

Formatter: Parenthesize long expressions in dicts #8437

zanieb opened this issue Nov 2, 2023 · 1 comment
Labels
formatter Related to the formatter preview Related to preview mode features style How should formatted code look

Comments

@zanieb
Copy link
Member

zanieb commented Nov 2, 2023

See #8436 for background.

Stable formatting of dicts with a long value results in a wrap that reduces readability

Unformatted:

if True:
    ___ = {
        "x": "---",
        "y": "--------------------------------------------------------------" if True else None,
        "z": "---",
    }

Formatted (Ruff, Black stable):

if True:
    ___ = {
        "x": "---",
        "y": "--------------------------------------------------------------"
        if True
        else None,
        "z": "---",
    }

Formatted (Black preview):

if True:
    ___ = {
        "x": "---",
        "y": (
            "--------------------------------------------------------------"
            if True
            else None
        ),
        "z": "---",
    }

With a shorter line, Ruff retains parenthesis when the lines are collapsed:

if True:
    ___ = {
        "x": "---",
        "y": ("----------------------------------------------------" if True else None),
        "z": "---",
    }

However, these parenthesis should be removed as they are superfluous.

Black's preview style will remove these parenthesis:

if True:
    ___ = {
        "x": "---",
        "y": "----------------------------------------------------" if True else None,
        "z": "---",
    }

See psf/black#620 psf/black#3440

Note that Black does not remove these parenthesis for calls or lists, see #8436 and #8438

@zanieb zanieb added formatter Related to the formatter preview Related to preview mode features style How should formatted code look labels Nov 2, 2023
@zanieb zanieb added this to the Formatter: Stable milestone Nov 2, 2023
@MichaReiser
Copy link
Member

I merged this into #8438 and create a new issue for parenthesizing long values.

@MichaReiser MichaReiser closed this as not planned Won't fix, can't repro, duplicate, stale Nov 29, 2023
@MichaReiser MichaReiser removed this from the Formatter: Stable milestone Nov 29, 2023
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 style How should formatted code look
Projects
None yet
Development

No branches or pull requests

2 participants