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

Allow parenthesized content exceed configured line width #7490

Merged
merged 2 commits into from
Sep 20, 2023

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Sep 18, 2023

Summary

This PR changes the formatting for all parenthesized expressions to exceed the configured line width without breaking the enclosing expression. This is only relevant for the optional parentheses (can_omit_optional_parentheses) layout (used by maybe_parenthesize_expression).

The basic idea is that when the content of a parenthesized expression doesn't fit when breaking after the opening (, [, or {, then adding an extra indentation level won't help either.

# Input
assert items == [
    "a very very very very very very very very very very very very very very very long string",
]

# Ruff before

assert (
    items
    == [
        "a very very very very very very very very very very very very very very very long string",
    ]
)

# After this PR (same as black)
assert items == [
    "a very very very very very very very very very very very very very very very long string",
]

You can see in the above example that adding the extra parentheses can never make the string fit, it's simply too long (this becomes less of a problem with the preview string splitting but can still happen with very long identifier names)

This change is implemented by changing the definition of fits_expanded to allow for text overflows when the condition is met (the optional parentheses are omitted).

Closes #7431

Fixes Poetry's assert diffs python-poetry/poetry@master...Secrus:poetry:ruff-formatter#diff-5f9be6a1e39172bd52265fbad21be3b1d184c0856263f30c21ff941e73dfb594L425

Test Plan

Added tests

Main

project similarity index total files changed files
cpython 0.76083 1789 1631
django 0.99983 2760 36
transformers 0.99956 2587 404
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99929 648 16
zulip 0.99969 1437 21

PR

project similarity index total files changed files
cpython 0.76083 1789 1631
django 0.99983 2760 36
transformers 0.99956 2587 404
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99967 648 15
zulip 0.99972 1437 21

@MichaReiser
Copy link
Member Author

MichaReiser commented Sep 18, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

@MichaReiser MichaReiser added the formatter Related to the formatter label Sep 18, 2023
@MichaReiser MichaReiser force-pushed the allow-overflow branch 2 times, most recently from 4745612 to cf7cd83 Compare September 19, 2023 06:18
@codspeed-hq
Copy link

codspeed-hq bot commented Sep 19, 2023

CodSpeed Performance Report

Merging #7490 will not alter performance

Comparing allow-overflow (86bc539) with main (6a4dbd6)

Summary

✅ 25 untouched benchmarks

@MichaReiser MichaReiser changed the title Potential fix for overlong parenthesized content Allow parenthesized content exceed configured line width Sep 19, 2023
// Measure in fully expanded mode and allow overflows
self.stack.push(
TagKind::FitsExpanded,
args.with_measure_mode(MeasureMode::AllLinesAllowTextOverflow)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the main change. Adding a match on args.mode() is mainly to align it with how other group-like IR elements get formatted and avoids extra work in PrintMode::Expanded

// It's not necessary to wrap the content if it is not inside of an optional_parentheses group.
write!(f, [inner])
}
write!(f, [token(self.left), inner, token(self.right)])
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to move the parentheses out because we want the group outer group to break if the opening or closing parentheses don't fit on the line.

@MichaReiser MichaReiser marked this pull request as ready for review September 19, 2023 06:53
Comment on lines +399 to +403
if True:
if True:
if True:
if True:
msg += " " + _(
Copy link
Member

Choose a reason for hiding this comment

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

i'd flatten this and expand the lines instead

@MichaReiser MichaReiser merged commit 192463c into main Sep 20, 2023
16 checks passed
@MichaReiser MichaReiser deleted the allow-overflow branch September 20, 2023 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Formatter incompatibility: extra parentheses around binary expression
2 participants