Repository navigation
Can omit parentheses layout for patterns #6933
Description
Activity
- addedformatterRelated to the Ruff formatterRelated to the Ruff formatter
on Aug 28, 2023 This seems to be fixed: https://play.ruff.rs/e7f95918-d71e-479f-8fae-29cdaa476463
Closing the issue but feel free to re-open it if you think otherwise.
This isn't fixed yet. Notice how the expression gets wrapped in parentheses.
We need to avoid adding the parentheses in this case. I'm not sure why the function isn't using
maybe_parenthesize(IfBreaks)directlyruff/crates/ruff_python_formatter/src/other/match_case.rs
Lines 38 to 58 in c05e462
if has_comments { pattern.format().with_options(Parentheses::Always).fmt(f)?; } else { match pattern.needs_parentheses(item.as_any_node_ref(), f.context()) { OptionalParentheses::Multiline => { parenthesize_if_expands( &pattern.format().with_options(Parentheses::Never), ) .fmt(f)?; } OptionalParentheses::Always => { pattern.format().with_options(Parentheses::Always).fmt(f)?; } OptionalParentheses::Never => { pattern.format().with_options(Parentheses::Never).fmt(f)?; } OptionalParentheses::BestFit => { pattern.format().with_options(Parentheses::Never).fmt(f)?; } } } Reacted by Dhruv ManilawalaWe want to make this change (it improves consistency), but it's not blocking for the Beta.
The challenge here is that we need to re-implement
can_omit_parenthesesfor patterns, since that logic only applies to expressions.I'm going to remove this from the stable release since it hasn't come up often.
- addedstyleHow should Ruff-formatted code lookHow should Ruff-formatted code look
on Feb 8, 2024
Ruff avoids adding parentheses to expressions that start or end with a parenthesized node (see
can_omit_parentheses).This is implemented by using
optional_parenthesesin conjunction with thein_parentheses_only_*builders. We need something similar for patterns to support