Fix invalid syntax for binary expression in unary op (#5370)

This commit is contained in:
Micha Reiser 2023-06-29 08:09:26 +02:00 committed by GitHub
parent 38189ed913
commit 955e9ef821
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 200 additions and 116 deletions

View file

@ -34,10 +34,10 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {
impl<'ast> FormatBinaryLike<'ast> for ExprCompare {
type FormatOperator = FormatOwnedWithRule<CmpOp, FormatCmpOp, PyFormatContext<'ast>>;
fn binary_layout(&self) -> BinaryLayout {
fn binary_layout(&self, source: &str) -> BinaryLayout {
if self.ops.len() == 1 {
match self.comparators.as_slice() {
[right] => BinaryLayout::from_left_right(&self.left, right),
[right] => BinaryLayout::from_left_right(&self.left, right, source),
[..] => BinaryLayout::Default,
}
} else {
@ -102,7 +102,7 @@ impl NeedsParentheses for ExprCompare {
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
parentheses @ Parentheses::Optional => match self.binary_layout() {
parentheses @ Parentheses::Optional => match self.binary_layout(source) {
BinaryLayout::Default => parentheses,
BinaryLayout::ExpandRight