-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect parenthesized generators in has_own_parentheses
#8100
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ use ruff_text_size::Ranged; | |
use crate::builders::parenthesize_if_expands; | ||
use crate::comments::{leading_comments, trailing_comments, LeadingDanglingTrailingComments}; | ||
use crate::context::{NodeLevel, WithNodeLevel}; | ||
use crate::expression::expr_generator_exp::is_generator_parenthesized; | ||
use crate::expression::expr_tuple::is_tuple_parenthesized; | ||
use crate::expression::parentheses::{ | ||
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, | ||
OptionalParentheses, Parentheses, Parenthesize, | ||
|
@@ -510,7 +512,7 @@ fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool | |
|
||
if visitor.max_precedence == OperatorPrecedence::None { | ||
true | ||
} else if visitor.pax_precedence_count > 1 { | ||
} else if visitor.max_precedence_count > 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this was a typo, but please correct me if I'm wrong. |
||
false | ||
} else if visitor.max_precedence == OperatorPrecedence::Attribute { | ||
true | ||
|
@@ -540,7 +542,7 @@ fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool | |
#[derive(Clone, Debug)] | ||
struct CanOmitOptionalParenthesesVisitor<'input> { | ||
max_precedence: OperatorPrecedence, | ||
pax_precedence_count: u32, | ||
max_precedence_count: u32, | ||
any_parenthesized_expressions: bool, | ||
last: Option<&'input Expr>, | ||
first: Option<&'input Expr>, | ||
|
@@ -552,7 +554,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { | |
Self { | ||
context, | ||
max_precedence: OperatorPrecedence::None, | ||
pax_precedence_count: 0, | ||
max_precedence_count: 0, | ||
any_parenthesized_expressions: false, | ||
last: None, | ||
first: None, | ||
|
@@ -566,11 +568,11 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { | |
fn update_max_precedence_with_count(&mut self, precedence: OperatorPrecedence, count: u32) { | ||
match self.max_precedence.cmp(&precedence) { | ||
Ordering::Less => { | ||
self.pax_precedence_count = count; | ||
self.max_precedence_count = count; | ||
self.max_precedence = precedence; | ||
} | ||
Ordering::Equal => { | ||
self.pax_precedence_count += count; | ||
self.max_precedence_count += count; | ||
} | ||
Ordering::Greater => {} | ||
} | ||
|
@@ -581,7 +583,6 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { | |
match expr { | ||
Expr::Dict(_) | ||
| Expr::List(_) | ||
| Expr::Tuple(_) | ||
| Expr::Set(_) | ||
| Expr::ListComp(_) | ||
| Expr::SetComp(_) | ||
|
@@ -590,6 +591,21 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { | |
// The values are always parenthesized, don't visit. | ||
return; | ||
} | ||
|
||
Expr::Tuple(tuple) if is_tuple_parenthesized(tuple, self.context.source()) => { | ||
self.any_parenthesized_expressions = true; | ||
// The values are always parenthesized, don't visit. | ||
return; | ||
} | ||
|
||
Expr::GeneratorExp(generator) | ||
if is_generator_parenthesized(generator, self.context.source()) => | ||
{ | ||
self.any_parenthesized_expressions = true; | ||
// The values are always parenthesized, don't visit. | ||
return; | ||
} | ||
|
||
// It's impossible for a file smaller or equal to 4GB to contain more than 2^32 comparisons | ||
// because each comparison requires a left operand, and `n` `operands` and right sides. | ||
#[allow(clippy::cast_possible_truncation)] | ||
|
@@ -690,7 +706,8 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { | |
self.update_max_precedence(OperatorPrecedence::String); | ||
} | ||
|
||
Expr::NamedExpr(_) | ||
Expr::Tuple(_) | ||
| Expr::NamedExpr(_) | ||
| Expr::GeneratorExp(_) | ||
| Expr::Lambda(_) | ||
| Expr::Await(_) | ||
|
@@ -914,17 +931,29 @@ pub(crate) fn has_own_parentheses( | |
Some(OwnParentheses::NonEmpty) | ||
} | ||
|
||
Expr::GeneratorExp(generator) | ||
if is_generator_parenthesized(generator, context.source()) => | ||
{ | ||
Some(OwnParentheses::NonEmpty) | ||
} | ||
|
||
// These expressions must contain _some_ child or trivia token in order to be non-empty. | ||
Expr::List(ast::ExprList { elts, .. }) | ||
| Expr::Set(ast::ExprSet { elts, .. }) | ||
| Expr::Tuple(ast::ExprTuple { elts, .. }) => { | ||
Expr::List(ast::ExprList { elts, .. }) | Expr::Set(ast::ExprSet { elts, .. }) => { | ||
if !elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { | ||
Some(OwnParentheses::NonEmpty) | ||
} else { | ||
Some(OwnParentheses::Empty) | ||
} | ||
} | ||
|
||
Expr::Tuple(tuple) if is_tuple_parenthesized(tuple, context.source()) => { | ||
if !tuple.elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { | ||
Some(OwnParentheses::NonEmpty) | ||
} else { | ||
Some(OwnParentheses::Empty) | ||
} | ||
} | ||
|
||
Expr::Dict(ast::ExprDict { keys, .. }) => { | ||
if !keys.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { | ||
Some(OwnParentheses::NonEmpty) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An empty tuple has to be parenthesized. Otherwise, it... can't exist?