Skip to content

Formatter should avoid splitting arguments into their own line #21581

@injust

Description

@injust

Summary

This is a silly contrived example, but unfortunately I cannot share the original code.

aa and bb are identical, except the comparisons are reordered. Both lines are 61 characters long.

foo = 5

aa = float("16.0") <= foo <= float("20.0") and 5 <= foo <= 10
bb = 5 <= foo <= 10 and float("16.0") <= foo <= float("20.0")

When the line length is set to 60, this is the result (https://play.ruff.rs/56dffb27-828b-4cff-8903-5153da7ad31b?secondary=Format):

foo = 5

aa = (
    float("16.0") <= foo <= float("20.0") and 5 <= foo <= 10
)
bb = 5 <= foo <= 10 and float("16.0") <= foo <= float(
    "20.0"
)

When the line length is set to 59, this is the result(https://play.ruff.rs/a9d7d7fb-1535-4600-9dde-1bd94bca876f?secondary=Format):

foo = 5

aa = (
    float("16.0") <= foo <= float("20.0")
    and 5 <= foo <= 10
)
bb = 5 <= foo <= 10 and float("16.0") <= foo <= float(
    "20.0"
)

In both cases, aa is obviously easier to read. Ideally, the formatter would format bb the same way.

In other words, putting an expression on a new line (in parentheses) or splitting on and should be preferred over splitting a function call's arguments into their own line.

However, this would be a deviation from Black, so I'm not really sure what could be done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions