Skip to content

Refactor with statement formatting to have explicit layouts#10296

Merged
charliermarsh merged 1 commit intomainfrom
refactor-with-item-formatting
Mar 8, 2024
Merged

Refactor with statement formatting to have explicit layouts#10296
charliermarsh merged 1 commit intomainfrom
refactor-with-item-formatting

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Mar 8, 2024

Summary

This PR refactors the with item formatting to use more explicit layouts to make it easier to understand the different formatting cases.

The benefit of the explicit layout is that it makes it easier to reasons about layout transition between format runs. For example, today it's possible that SingleWithTarget or ParenthesizeIfExpands add parentheses around the with items for with aaaaaaaaaa + bbbbbbbbbbbb: pass, resulting in with (aaaaaaaaaa + bbbbbbbbbbbb): pass. The problem with this is that the next formatting pass uses the SingleParenthesizedContextExpression layout that uses maybe_parenthesize_expression which is different from parenthesize_if_expands(&expr) or optional_parentheses(&expr).

Test Plan

cargo test

I ran the ecosystem checks locally and there are no changes.

@MichaReiser MichaReiser added internal An internal refactor or improvement formatter Related to the formatter labels Mar 8, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Mar 8, 2024

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

/// ```
///
/// Only used for Python 3.9+
SingleWithTarget(&'a WithItem),
Copy link
Member Author

Choose a reason for hiding this comment

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

The name here doesn't make sense as of this PR because it could as well be a with item without a target. This gets changed in #10274, and I didn't want to come up with new terminology just for the state between the two PRs.

@MichaReiser MichaReiser changed the title Refactor stmt_with formatting to have explicit layouts Refactor with statement formatting to have explicit layouts Mar 8, 2024
@charliermarsh charliermarsh merged commit a56d42f into main Mar 8, 2024
@charliermarsh charliermarsh deleted the refactor-with-item-formatting branch March 8, 2024 23:40
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
…h#10296)

## Summary

This PR refactors the with item formatting to use more explicit layouts
to make it easier to understand the different formatting cases.

The benefit of the explicit layout is that it makes it easier to reasons
about layout transition between format runs. For example, today it's
possible that `SingleWithTarget` or `ParenthesizeIfExpands` add
parentheses around the with items for `with aaaaaaaaaa + bbbbbbbbbbbb:
pass`, resulting in `with (aaaaaaaaaa + bbbbbbbbbbbb): pass`. The
problem with this is that the next formatting pass uses the
`SingleParenthesizedContextExpression` layout that uses
`maybe_parenthesize_expression` which is different from
`parenthesize_if_expands(&expr)` or `optional_parentheses(&expr)`.

## Test Plan

`cargo test`

I ran the ecosystem checks locally and there are no changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants