ruff/crates
Micha Reiser 4bce801065
Fix unstable with-items formatting (#10274)
## Summary

Fixes https://github.com/astral-sh/ruff/issues/10267

The issue with the current formatting is that the formatter flips
between the `SingleParenthesizedContextManager` and
`ParenthesizeIfExpands` or `SingleWithTarget` because the layouts use
incompatible formatting ( `SingleParenthesizedContextManager`:
`maybe_parenthesize_expression(context)` vs `ParenthesizeIfExpands`:
`parenthesize_if_expands(item)`, `SingleWithTarget`:
`optional_parentheses(item)`.

The fix is to ensure that the layouts between which the formatter flips
when adding or removing parentheses are the same. I do this by
introducing a new `SingleWithoutTarget` layout that uses the same
formatting as `SingleParenthesizedContextManager` if it has no target
and prefer `SingleWithoutTarget` over using `ParenthesizeIfExpands` or
`SingleWithTarget`.

## Formatting change

The downside is that we now use `maybe_parenthesize_expression` over
`parenthesize_if_expands` for expressions where
`can_omit_optional_parentheses` returns `false`. This can lead to stable
formatting changes. I only found one formatting change in our ecosystem
check and, unfortunately, this is necessary to fix the instability (and
instability fixes are okay to have as part of minor changes according to
our versioning policy)

The benefit of the change is that `with` items with a single context
manager and without a target are now formatted identically to how the
same expression would be formatted in other clause headers.

## Test Plan

I ran the ecosystem check locally
2024-03-08 23:48:47 +00:00
..
ruff Bump version to v0.3.1 (#10252) 2024-03-06 19:59:04 +00:00
ruff_benchmark Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_cache Make all dependencies workspace dependencies (#9333) 2024-01-02 13:41:59 +00:00
ruff_dev Make --config and --isolated global flags (#10150) 2024-03-04 11:19:40 +00:00
ruff_diagnostics Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_formatter Add cold attribute to less likely printer queue branches (#10121) 2024-02-26 08:19:40 +01:00
ruff_index Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_linter Start tracking quoting style in the AST (#10298) 2024-03-08 19:11:47 +00:00
ruff_macros Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_notebook Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_python_ast Start tracking quoting style in the AST (#10298) 2024-03-08 19:11:47 +00:00
ruff_python_codegen Start tracking quoting style in the AST (#10298) 2024-03-08 19:11:47 +00:00
ruff_python_formatter Fix unstable with-items formatting (#10274) 2024-03-08 23:48:47 +00:00
ruff_python_index Track quoting style in the tokenizer (#10256) 2024-03-08 08:40:06 +00:00
ruff_python_literal Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_python_parser Start tracking quoting style in the AST (#10298) 2024-03-08 19:11:47 +00:00
ruff_python_resolver Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_python_semantic refactor: Use QualifiedName for Imported::call_path (#10214) 2024-03-06 09:55:59 +01:00
ruff_python_stdlib Split CallPath into QualifiedName and UnqualifiedName (#10210) 2024-03-04 09:06:51 +00:00
ruff_python_trivia Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
ruff_shrinking Bump version to v0.3.1 (#10252) 2024-03-06 19:59:04 +00:00
ruff_source_file [pycodestyle] Respect isort settings in blank line rules (E3*) (#10096) 2024-03-05 10:09:15 +00:00
ruff_text_size Range formatting: Fix invalid syntax after parenthesizing expression (#9751) 2024-02-02 17:56:25 +01:00
ruff_wasm Run doctests as part of CI pipeline (#9939) 2024-02-12 10:18:58 +01:00
ruff_workspace Accept a PEP 440 version specifier for required-version (#10216) 2024-03-03 18:43:49 -05:00