Parenthesize breaking named expressions in match guards (#9396)

## Summary

This is an attempt to solve
https://github.com/astral-sh/ruff/issues/9394 by avoiding breaks in
named expressions when invalid.
This commit is contained in:
Charlie Marsh 2024-01-08 09:47:01 -05:00 committed by GitHub
parent b1a5df8694
commit ba71772d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 2 deletions

View file

@ -109,6 +109,11 @@ match long_lines:
): # another comment
pass
case {
"long_long_long_key": str(long_long_long_key)
} if value := "long long long long long long long long long long long value":
pass
match pattern_comments:
case (

View file

@ -3,7 +3,9 @@ use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprNamedExpr;
use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{
in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses,
};
use crate::prelude::*;
#[derive(Default)]
@ -24,7 +26,10 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
write!(
f,
[
group(&format_args!(target.format(), soft_line_break_or_space())),
group(&format_args![
target.format(),
in_parentheses_only_soft_line_break_or_space()
]),
token(":=")
]
)?;

View file

@ -115,6 +115,11 @@ match long_lines:
): # another comment
pass
case {
"long_long_long_key": str(long_long_long_key)
} if value := "long long long long long long long long long long long value":
pass
match pattern_comments:
case (
@ -696,6 +701,11 @@ match long_lines:
): # another comment
pass
case {
"long_long_long_key": str(long_long_long_key)
} if value := "long long long long long long long long long long long value":
pass
match pattern_comments:
case (