Skip to content

Option to disable awkward line breaks for assert statements #13386

@0xjmux

Description

@0xjmux

Hi,
I'm using Ruff on a codebase with a good amount of assert statements - it's non-production code, and they're a good way to find bugs as you're testing things.

However, the way that Black and Ruff format assert statements once they pass a certain length doesn't make sense, as the active issues on Black show:

This seems to be a common issue with black that hasn't been addressed for some time, and the reason I started using Ruff is because I was hoping this behavior was something I could disable.

Example

Input:

    assert Path(filepath).exists(), f'longish assert string with filepath {filepath} not found!'

Formatted output:

    assert Path(
        filepath
    ).exists(), f'longish assert string with filepath {filepath} not found!'

Which is objectively less readable.

Desired behavior:
An option to either:

  • Disable formatting of assert statements entirely
  • Disable splitting parenthesis for specified keywords (assert, in this case)
  • Option to format assert statements like this, and split the error string instead of the conditional.
assert Path(filepath).exists(), \
    f'longish assert string with filepath {filepath} not found!'

Ruff Playground output and IR

Pasting the input into the Ruff Playground and checking out the IR gives this output:

Ruff playground output ``` empty_line, source_position(356), "filename = ", best_fit_parenthesize("#5", ["\"hello.txt\""]), source_position(378), empty_line, source_position(384), "assert ", best_fit_parenthesize("#6", [ "Path(", fits_expanded(propagate_expand: false, condition: if_group_fits_on_line("#6"), [ group([indent([soft_line_break, group(["filename"])]), soft_line_break]) ]), ").exists", group(["()"]) ]), ", ", best_fit_parenthesize("#7", [ "f\"longish assert string with filepath {filename} not found!\"" ]), source_position(477) ]), hard_line_break, ```

Other

  • Ruff version: 0.6.5. Behavior is same using --preview and --isolated flags.

Metadata

Metadata

Assignees

No one assigned

    Labels

    formatterRelated to the formatterstyleHow should formatted code look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions