Pass parent to NeedsParentheses (#5708)

This commit is contained in:
Micha Reiser 2023-07-13 08:57:29 +02:00 committed by GitHub
parent 30702c2977
commit 067b2a6ce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 562 additions and 606 deletions

View file

@ -1,3 +1,4 @@
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::FormatNodeRule;
@ -18,7 +19,7 @@ impl FormatNodeRule<Decorator> for FormatDecorator {
f,
[
text("@"),
expression.format().with_options(Parenthesize::Optional)
maybe_parenthesize_expression(expression, item, Parenthesize::Optional)
]
)
}

View file

@ -1,4 +1,5 @@
use crate::comments::trailing_comments;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
@ -60,7 +61,10 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
if let Some(type_) = type_ {
write!(
f,
[space(), type_.format().with_options(Parenthesize::IfBreaks)]
[
space(),
maybe_parenthesize_expression(type_, item, Parenthesize::IfBreaks)
]
)?;
if let Some(name) = name {
write!(f, [space(), text("as"), space(), name.format()])?;

View file

@ -1,3 +1,4 @@
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
@ -18,7 +19,11 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
let inner = format_with(|f| {
write!(
f,
[context_expr.format().with_options(Parenthesize::IfBreaks)]
[maybe_parenthesize_expression(
context_expr,
item,
Parenthesize::IfBreaks
)]
)?;
if let Some(optional_vars) = optional_vars {
write!(f, [space(), text("as"), space(), optional_vars.format()])?;