Fix StmtAnnAssign formatting by mirroring StmtAssign (#5732)
## Summary `StmtAnnAssign` would not insert parentheses when breaking the same way `StmtAssign` does, causing unstable formatting and likely some syntax errors. ## Test Plan I added a regression test.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
use ruff_formatter::write;
|
||||
@@ -18,11 +20,24 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
|
||||
|
||||
write!(
|
||||
f,
|
||||
[target.format(), text(":"), space(), annotation.format()]
|
||||
[
|
||||
target.format(),
|
||||
text(":"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(annotation, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
)?;
|
||||
|
||||
if let Some(value) = value {
|
||||
write!(f, [space(), text("="), space(), value.format()])?;
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
space(),
|
||||
text("="),
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user