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 calls #8436

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

Formatter: Parenthesize long expressions in calls #8436

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

Stable formatting of calls with a long expression results in a wrap that reduces readability

Unformatted:

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

Formatted:

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

Adding parenthesis results in more readable code. Black's preview style does so.

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

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

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

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

Black's preview style will not remove these parenthesis for calls, but probably should.

Note Black's preview style will remove these parenthesis for dict literals, see #8437 but will not for list literals, see #8438

See also psf/black#620 psf/black#3440 psf/black#3510

@zanieb zanieb added formatter Related to the formatter preview Related to preview mode features style How should formatted code look labels Nov 2, 2023
@MichaReiser MichaReiser added this to the Formatter: Stable milestone Nov 2, 2023
@zanieb zanieb changed the title Formatter: Parenthesize long expressions in dicts and calls Formatter: Parenthesize long expressions in calls Nov 2, 2023
@MichaReiser
Copy link
Member

I merge this into #8438 because conditional expression should be parenthesized in call contexts (as far as I understand)

@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