diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 7f83db9f250b3..22cc4053e1df6 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -135,6 +135,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) { elts, ctx, range: _, + is_parenthesized: _, }) | Expr::List(ast::ExprList { elts, diff --git a/crates/ruff_linter/src/checkers/ast/mod.rs b/crates/ruff_linter/src/checkers/ast/mod.rs index 540745e5f8e12..6fedee018eee2 100644 --- a/crates/ruff_linter/src/checkers/ast/mod.rs +++ b/crates/ruff_linter/src/checkers/ast/mod.rs @@ -1232,6 +1232,7 @@ where elts, ctx, range: _, + is_parenthesized: _, }) = slice.as_ref() { let mut iter = elts.iter(); diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs index 8357b6670b38c..02e4a09393e89 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/duplicate_exceptions.rs @@ -109,6 +109,7 @@ fn type_pattern(elts: Vec<&Expr>) -> Expr { elts: elts.into_iter().cloned().collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, } .into() } diff --git a/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs b/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs index 8c95a77556a38..ca83cb4da2ebd 100644 --- a/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs +++ b/crates/ruff_linter/src/rules/flake8_pie/rules/multiple_starts_ends_with.rs @@ -153,6 +153,7 @@ pub(crate) fn multiple_starts_ends_with(checker: &mut Checker, expr: &Expr) { .collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }); let node1 = Expr::Name(ast::ExprName { id: arg_name.into(), diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs index 8fd0169f4bd79..5dad2537f46a0 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_literal_union.rs @@ -72,6 +72,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp elts, range: _, ctx: _, + is_parenthesized: _, }) = slice.as_ref() { for expr in elts { @@ -123,6 +124,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp elts: literal_exprs.into_iter().cloned().collect(), range: TextRange::default(), ctx: ExprContext::Load, + is_parenthesized: true, })), range: TextRange::default(), ctx: ExprContext::Load, @@ -148,6 +150,7 @@ pub(crate) fn unnecessary_literal_union<'a>(checker: &mut Checker, expr: &'a Exp elts, range: TextRange::default(), ctx: ExprContext::Load, + is_parenthesized: true, })), range: TextRange::default(), ctx: ExprContext::Load, diff --git a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs index caf00e7a219d6..0452155fa0767 100644 --- a/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs +++ b/crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs @@ -130,6 +130,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr) .collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, })), ctx: ExprContext::Load, range: TextRange::default(), @@ -151,6 +152,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr) elts: exprs.into_iter().cloned().collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, })), ctx: ExprContext::Load, range: TextRange::default(), diff --git a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs index 0f5a621f01eac..22c0cbc5d2767 100644 --- a/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs +++ b/crates/ruff_linter/src/rules/flake8_pytest_style/rules/parametrize.rs @@ -330,6 +330,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { .collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }); diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( format!("({})", checker.generator().expr(&node)), @@ -437,6 +438,7 @@ fn check_names(checker: &mut Checker, decorator: &Decorator, expr: &Expr) { elts: elts.clone(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }); diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( format!("({})", checker.generator().expr(&node)), diff --git a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs index c3d4a86d1a026..4bb2d53ff8ee7 100644 --- a/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs +++ b/crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs @@ -428,6 +428,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) { .collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }; let node1 = ast::ExprName { id: "isinstance".into(), @@ -543,6 +544,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) { elts: comparators.into_iter().map(Clone::clone).collect(), ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }; let node1 = ast::ExprName { id: id.into(), @@ -718,7 +720,7 @@ fn get_short_circuit_edit( generator.expr(expr) }; Edit::range_replacement( - if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _}) if !elts.is_empty()) + if matches!(expr, Expr::Tuple(ast::ExprTuple { elts, ctx: _, range: _, is_parenthesized: _}) if !elts.is_empty()) { format!("({content})") } else { diff --git a/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs b/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs index 92d6c7b6f336c..1d380c784823e 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/redefined_loop_name.rs @@ -301,6 +301,7 @@ fn assignment_targets_from_expr<'a>( ctx: ExprContext::Store, elts, range: _, + is_parenthesized: _, }) => Box::new( elts.iter() .flat_map(|elt| assignment_targets_from_expr(elt, dummy_variable_rgx)), diff --git a/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs b/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs index d3077539c8a34..adeb637548c8a 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/repeated_equality_comparison.rs @@ -143,6 +143,7 @@ pub(crate) fn repeated_equality_comparison(checker: &mut Checker, bool_op: &ast: elts: comparators.iter().copied().cloned().collect(), range: TextRange::default(), ctx: ExprContext::Load, + is_parenthesized: true, })], range: bool_op.range(), })), diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs index 8477e775b7649..71fef6600657f 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs @@ -127,6 +127,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E elts: remaining, ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }; format!("({})", checker.generator().expr(&node.into())) }; diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs index 419e6bacacbc7..64d4c9f2c8873 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/timeout_error_alias.rs @@ -141,6 +141,7 @@ fn tuple_diagnostic(checker: &mut Checker, tuple: &ast::ExprTuple, aliases: &[&E elts: remaining, ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }; format!("({})", checker.generator().expr(&node.into())) }; diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs index 547e40f7d87f1..9a6dee58bede3 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/use_pep695_type_alias.rs @@ -118,6 +118,7 @@ pub(crate) fn non_pep695_type_alias(checker: &mut Checker, stmt: &StmtAnnAssign) range: TextRange::default(), elts: constraints.into_iter().cloned().collect(), ctx: ast::ExprContext::Load, + is_parenthesized: true, }))) } None => None, diff --git a/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs b/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs index ba6db71e4a07f..06c34d799ea34 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/repeated_append.rs @@ -347,6 +347,7 @@ fn make_suggestion(group: &AppendGroup, generator: Generator) -> String { elts, ctx: ast::ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }; // Make `var.extend`. // NOTE: receiver is the same for all appends and that's why we can take the first. diff --git a/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs b/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs index 263ff68dc9258..f9a742978e60d 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/collection_literal_concatenation.rs @@ -162,6 +162,7 @@ fn concatenate_expressions(expr: &Expr) -> Option<(Expr, Type)> { elts: new_elts, ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, } .into(), }; diff --git a/crates/ruff_linter/src/rules/ruff/rules/never_union.rs b/crates/ruff_linter/src/rules/ruff/rules/never_union.rs index 0258c454c2ce2..95ff6562ef98e 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/never_union.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/never_union.rs @@ -116,6 +116,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) { elts, ctx: _, range: _, + is_parenthesized: _, }) = slice.as_ref() else { return; @@ -157,6 +158,7 @@ pub(crate) fn never_union(checker: &mut Checker, expr: &Expr) { elts: rest, ctx: ast::ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, })), ctx: ast::ExprContext::Load, range: TextRange::default(), diff --git a/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs b/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs index 9648fed7eac13..405e373914cbd 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs @@ -145,12 +145,14 @@ impl SequenceKind<'_> { // N.B. We only need the source code for the Tuple variant here, // but if you already have a `Locator` instance handy, // getting the source code is very cheap. - fn surrounding_brackets(&self, source: &str) -> (&'static str, &'static str) { + fn surrounding_brackets(&self) -> (&'static str, &'static str) { match self { Self::List => ("[", "]"), Self::Set => ("{", "}"), - Self::Tuple(ast_node) => { - if ast_node.is_parenthesized(source) { + Self::Tuple(ast::ExprTuple { + is_parenthesized, .. + }) => { + if *is_parenthesized { ("(", ")") } else { ("", "") @@ -225,7 +227,7 @@ pub(super) fn sort_single_line_elements_sequence( ) -> String { let element_pairs = SequenceElements::new(elements, elts); let last_item_index = element_pairs.last_item_index(); - let (opening_paren, closing_paren) = kind.surrounding_brackets(locator.contents()); + let (opening_paren, closing_paren) = kind.surrounding_brackets(); let mut result = String::from(opening_paren); // We grab the original source-code ranges using `locator.slice()` // rather than using the expression generator, as this approach allows diff --git a/crates/ruff_python_ast/src/comparable.rs b/crates/ruff_python_ast/src/comparable.rs index b3c7faf116a5c..6a6b79b9b1535 100644 --- a/crates/ruff_python_ast/src/comparable.rs +++ b/crates/ruff_python_ast/src/comparable.rs @@ -1072,6 +1072,7 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> { elts, ctx: _, range: _, + is_parenthesized: _, }) => Self::Tuple(ExprTuple { elts: elts.iter().map(Into::into).collect(), }), diff --git a/crates/ruff_python_ast/src/helpers.rs b/crates/ruff_python_ast/src/helpers.rs index 98370dfa9b1bc..57f3db1c9446d 100644 --- a/crates/ruff_python_ast/src/helpers.rs +++ b/crates/ruff_python_ast/src/helpers.rs @@ -1419,6 +1419,7 @@ pub fn pep_604_union(elts: &[Expr]) -> Expr { elts: vec![], ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }), [Expr::Tuple(ast::ExprTuple { elts, .. })] => pep_604_union(elts), [elt] => elt.clone(), @@ -1453,6 +1454,7 @@ pub fn typing_union(elts: &[Expr], binding: String) -> Expr { elts: vec![], ctx: ExprContext::Load, range: TextRange::default(), + is_parenthesized: true, }), [Expr::Tuple(ast::ExprTuple { elts, .. })] => typing_union(elts, binding), [elt] => elt.clone(), diff --git a/crates/ruff_python_ast/src/node.rs b/crates/ruff_python_ast/src/node.rs index 35a536004c2a3..6953136cdcb00 100644 --- a/crates/ruff_python_ast/src/node.rs +++ b/crates/ruff_python_ast/src/node.rs @@ -3212,6 +3212,7 @@ impl AstNode for ast::ExprTuple { elts, ctx: _, range: _, + is_parenthesized: _, } = self; for expr in elts { diff --git a/crates/ruff_python_ast/src/nodes.rs b/crates/ruff_python_ast/src/nodes.rs index 58d965660717e..65f3c62d9aa7b 100644 --- a/crates/ruff_python_ast/src/nodes.rs +++ b/crates/ruff_python_ast/src/nodes.rs @@ -8,7 +8,6 @@ use std::slice::{Iter, IterMut}; use itertools::Itertools; -use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer}; use ruff_text_size::{Ranged, TextRange, TextSize}; use crate::{int, LiteralExpressionRef}; @@ -1794,6 +1793,9 @@ pub struct ExprTuple { pub range: TextRange, pub elts: Vec, pub ctx: ExprContext, + + /// Whether the tuple is parenthesized in the source code. + pub is_parenthesized: bool, } impl From for Expr { @@ -1802,37 +1804,6 @@ impl From for Expr { } } -impl ExprTuple { - /// Return `true` if a tuple is parenthesized in the source code. - pub fn is_parenthesized(&self, source: &str) -> bool { - let Some(elt) = self.elts.first() else { - return true; - }; - - // Count the number of open parentheses between the start of the tuple and the first element. - let open_parentheses_count = - SimpleTokenizer::new(source, TextRange::new(self.start(), elt.start())) - .skip_trivia() - .filter(|token| token.kind() == SimpleTokenKind::LParen) - .count(); - if open_parentheses_count == 0 { - return false; - } - - // Count the number of parentheses between the end of the first element and its trailing comma. - let close_parentheses_count = - SimpleTokenizer::new(source, TextRange::new(elt.end(), self.end())) - .skip_trivia() - .take_while(|token| token.kind() != SimpleTokenKind::Comma) - .filter(|token| token.kind() == SimpleTokenKind::RParen) - .count(); - - // If the number of open parentheses is greater than the number of close parentheses, the tuple - // is parenthesized. - open_parentheses_count > close_parentheses_count - } -} - /// See also [Slice](https://docs.python.org/3/library/ast.html#ast.Slice) #[derive(Clone, Debug, PartialEq)] pub struct ExprSlice { diff --git a/crates/ruff_python_ast/src/visitor.rs b/crates/ruff_python_ast/src/visitor.rs index 2d8773fcfdcb0..b8d77ae3da327 100644 --- a/crates/ruff_python_ast/src/visitor.rs +++ b/crates/ruff_python_ast/src/visitor.rs @@ -539,6 +539,7 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) { elts, ctx, range: _, + is_parenthesized: _, }) => { for expr in elts { visitor.visit_expr(expr); diff --git a/crates/ruff_python_ast/src/visitor/transformer.rs b/crates/ruff_python_ast/src/visitor/transformer.rs index caa111c43f95b..aeb6561f1401a 100644 --- a/crates/ruff_python_ast/src/visitor/transformer.rs +++ b/crates/ruff_python_ast/src/visitor/transformer.rs @@ -528,6 +528,7 @@ pub fn walk_expr(visitor: &V, expr: &mut Expr) { elts, ctx, range: _, + is_parenthesized: _, }) => { for expr in elts { visitor.visit_expr(expr); diff --git a/crates/ruff_python_formatter/src/comments/placement.rs b/crates/ruff_python_formatter/src/comments/placement.rs index 2d958ebee98f3..1f4bb24a0b0e9 100644 --- a/crates/ruff_python_formatter/src/comments/placement.rs +++ b/crates/ruff_python_formatter/src/comments/placement.rs @@ -293,9 +293,10 @@ fn handle_enclosed_comment<'a>( | AnyNodeRef::ExprSet(_) | AnyNodeRef::ExprListComp(_) | AnyNodeRef::ExprSetComp(_) => handle_bracketed_end_of_line_comment(comment, locator), - AnyNodeRef::ExprTuple(tuple) if tuple.is_parenthesized(locator.contents()) => { - handle_bracketed_end_of_line_comment(comment, locator) - } + AnyNodeRef::ExprTuple(ast::ExprTuple { + is_parenthesized: true, + .. + }) => handle_bracketed_end_of_line_comment(comment, locator), AnyNodeRef::ExprGeneratorExp(generator) if is_generator_parenthesized(generator, locator.contents()) => { diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index 9db272c611ccd..399016ca091e9 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -116,6 +116,7 @@ impl FormatNodeRule for FormatExprTuple { elts, ctx: _, range: _, + is_parenthesized, } = item; let comments = f.context().comments().clone(); @@ -136,7 +137,7 @@ impl FormatNodeRule for FormatExprTuple { return empty_parenthesized("(", dangling, ")").fmt(f); } [single] => match self.parentheses { - TupleParentheses::Preserve if !item.is_parenthesized(f.context().source()) => { + TupleParentheses::Preserve if !is_parenthesized => { write!(f, [single.format(), token(",")]) } _ => @@ -152,7 +153,7 @@ impl FormatNodeRule for FormatExprTuple { // // Unlike other expression parentheses, tuple parentheses are part of the range of the // tuple itself. - _ if item.is_parenthesized(f.context().source()) + _ if *is_parenthesized && !(self.parentheses == TupleParentheses::NeverPreserve && dangling.is_empty()) => { diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index b63ae81ed1fb0..ce775002bba98 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -669,7 +669,10 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> { return; } - Expr::Tuple(tuple) if tuple.is_parenthesized(self.context.source()) => { + Expr::Tuple(ast::ExprTuple { + is_parenthesized: true, + .. + }) => { self.any_parenthesized_expressions = true; // The values are always parenthesized, don't visit. return; @@ -1058,7 +1061,12 @@ pub(crate) fn has_own_parentheses( } } - Expr::Tuple(tuple) if tuple.is_parenthesized(context.source()) => { + Expr::Tuple( + tuple @ ast::ExprTuple { + is_parenthesized: true, + .. + }, + ) => { if !tuple.elts.is_empty() || context.comments().has_dangling(AnyNodeRef::from(expr)) { Some(OwnParentheses::NonEmpty) } else { diff --git a/crates/ruff_python_parser/src/context.rs b/crates/ruff_python_parser/src/context.rs index 8620c5849ebfb..e7c101b0c0deb 100644 --- a/crates/ruff_python_parser/src/context.rs +++ b/crates/ruff_python_parser/src/context.rs @@ -3,10 +3,16 @@ use ruff_python_ast::{self as ast, Expr, ExprContext}; pub(crate) fn set_context(expr: Expr, ctx: ExprContext) -> Expr { match expr { Expr::Name(ast::ExprName { id, range, .. }) => ast::ExprName { range, id, ctx }.into(), - Expr::Tuple(ast::ExprTuple { elts, range, .. }) => ast::ExprTuple { + Expr::Tuple(ast::ExprTuple { + elts, + range, + is_parenthesized, + ctx: _, + }) => ast::ExprTuple { elts: elts.into_iter().map(|elt| set_context(elt, ctx)).collect(), range, ctx, + is_parenthesized, } .into(), diff --git a/crates/ruff_python_parser/src/parser.rs b/crates/ruff_python_parser/src/parser.rs index c0f6c7d18d2cb..fc6f80031c782 100644 --- a/crates/ruff_python_parser/src/parser.rs +++ b/crates/ruff_python_parser/src/parser.rs @@ -1507,4 +1507,20 @@ u"foo" f"bar {baz} really" u"bar" "no" let parse_ast = parse_suite(r#"x = "\N{BACKSPACE}another cool trick""#).unwrap(); insta::assert_debug_snapshot!(parse_ast); } + + #[test] + fn test_tuple() { + let parse_ast = parse_suite( + r#" +a,b +(a,b) +() +(a,) +((a,b)) +"# + .trim(), + ) + .unwrap(); + insta::assert_debug_snapshot!(parse_ast); + } } diff --git a/crates/ruff_python_parser/src/python.lalrpop b/crates/ruff_python_parser/src/python.lalrpop index aa87fcd72d4f0..0495212bf7080 100644 --- a/crates/ruff_python_parser/src/python.lalrpop +++ b/crates/ruff_python_parser/src/python.lalrpop @@ -483,7 +483,8 @@ MatchStatement: ast::Stmt = { ast::ExprTuple { elts: vec![subject.into()], ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + is_parenthesized: false }, )), cases, @@ -506,7 +507,8 @@ MatchStatement: ast::Stmt = { ast::ExprTuple { elts, ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + is_parenthesized: false }, )), cases, @@ -1563,6 +1565,7 @@ SubscriptList: crate::parser::ParenthesizedExpr = { elts: vec![s1.into()], ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: false }.into() }, > ","? => { @@ -1571,6 +1574,7 @@ SubscriptList: crate::parser::ParenthesizedExpr = { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: false }.into() } }; @@ -1716,7 +1720,12 @@ Atom: crate::parser::ParenthesizedExpr = { } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: true + }.into() } }, "(" >> ",")?> )*> ")" =>? { @@ -1733,13 +1742,19 @@ Atom: crate::parser::ParenthesizedExpr = { }) } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect(); - Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()) + Ok(ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: true + }.into()) } }, "(" ")" => ast::ExprTuple { elts: Vec::new(), ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: true }.into(), "(" ")" => crate::parser::ParenthesizedExpr { expr: e.into(), @@ -1842,7 +1857,12 @@ GenericList: crate::parser::ParenthesizedExpr = { } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: false + }.into() } } } diff --git a/crates/ruff_python_parser/src/python.rs b/crates/ruff_python_parser/src/python.rs index 5771d7099f737..fcc3ddafb3611 100644 --- a/crates/ruff_python_parser/src/python.rs +++ b/crates/ruff_python_parser/src/python.rs @@ -1,5 +1,5 @@ // auto-generated: "lalrpop 0.20.0" -// sha3: 031689e389556292d9dbd8a1b1ff8ca29bac76d83f1b345630481d620b89e1c2 +// sha3: 3353c821455a3db6ff720d8362b578c72572ddfe8f853b7025b648b07c8d1ec7 use ruff_text_size::{Ranged, TextLen, TextRange, TextSize}; use ruff_python_ast::{self as ast, Int, IpyEscapeKind}; use crate::{ @@ -33938,7 +33938,8 @@ fn __action86< ast::ExprTuple { elts: vec![subject.into()], ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + is_parenthesized: false }, )), cases, @@ -33982,7 +33983,8 @@ fn __action87< ast::ExprTuple { elts, ctx: ast::ExprContext::Load, - range: (tuple_location..tuple_end_location).into() + range: (tuple_location..tuple_end_location).into(), + is_parenthesized: false }, )), cases, @@ -36227,6 +36229,7 @@ fn __action208< elts: vec![s1.into()], ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: false }.into() } } @@ -36249,6 +36252,7 @@ fn __action209< elts, ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: false }.into() } } @@ -37049,7 +37053,12 @@ fn __action259< } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: false + }.into() } } } @@ -37103,7 +37112,12 @@ fn __action262< } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: false + }.into() } } } @@ -41272,7 +41286,12 @@ fn __action553< } } else { let elts = elts.into_iter().map(ast::Expr::from).collect(); - ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into() + ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: true + }.into() } } } @@ -41307,7 +41326,12 @@ fn __action554< }) } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect(); - Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()) + Ok(ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: true + }.into()) } } } @@ -41328,6 +41352,7 @@ fn __action555< elts: Vec::new(), ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: true }.into() } @@ -42005,7 +42030,12 @@ fn __action596< }) } else { let elts = left.into_iter().flatten().chain([mid]).chain(right).map(ast::Expr::from).collect(); - Ok(ast::ExprTuple { elts, ctx: ast::ExprContext::Load, range: (location..end_location).into() }.into()) + Ok(ast::ExprTuple { + elts, + ctx: ast::ExprContext::Load, + range: (location..end_location).into(), + is_parenthesized: true + }.into()) } } } @@ -42026,6 +42056,7 @@ fn __action597< elts: Vec::new(), ctx: ast::ExprContext::Load, range: (location..end_location).into(), + is_parenthesized: true }.into() } diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap index e269b2e4d45ed..af9de33825bcc 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_attribute.snap @@ -55,6 +55,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap index d7775fae99876..a9b63f3d444fa 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_for.snap @@ -44,6 +44,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), body: [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap index 436e3f449f885..5d9d5fbbbb9b3 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list.snap @@ -60,6 +60,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap index d73b326211c80..c0d52f8c77e30 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_list_comp.snap @@ -65,6 +65,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), ifs: [], diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap index c3b835c27e2ec..191a8f2a9ae0e 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_name.snap @@ -45,6 +45,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap index f5c256cd2541a..a8f8ceb524ceb 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_set_comp.snap @@ -65,6 +65,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), ifs: [], diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap index d6401710ed926..ed0ee653409b5 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_starred.snap @@ -33,6 +33,7 @@ expression: parse_ast ), ], ctx: Store, + is_parenthesized: true, }, ), ], @@ -66,6 +67,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap index abdb9e8088c9c..febf297987793 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_subscript.snap @@ -58,6 +58,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap index ac332e78f70e9..a4680be133a32 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__assign_tuple.snap @@ -27,6 +27,7 @@ expression: parse_ast ), ], ctx: Store, + is_parenthesized: true, }, ), ], @@ -60,6 +61,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap index 6286c12485136..77bd48860b73e 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_attribute.snap @@ -54,6 +54,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap index 6b1feb1798485..0cde13317cab3 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__context__tests__aug_assign_subscript.snap @@ -57,6 +57,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap index 88e7d791f0114..d77047278e83d 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__invalid__tests__ok_tuple.snap @@ -35,6 +35,7 @@ Ok( ), ], ctx: Store, + is_parenthesized: true, }, ), ], @@ -68,6 +69,7 @@ Ok( ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap index 58c33b7302c9d..d91bb0cd5e342 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap @@ -111,6 +111,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), debug_text: None, @@ -501,6 +502,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), debug_text: Some( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap index 08f16c5d09f78..87a7eb9f8089c 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__generator_expression_argument.snap @@ -146,6 +146,7 @@ Call( ), ], ctx: Load, + is_parenthesized: true, }, ), ifs: [], diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap index 545c9396f93b0..c6fd80ac699c8 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match.snap @@ -469,6 +469,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), cases: [ @@ -521,6 +522,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), cases: [ @@ -573,6 +575,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), cases: [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap index 6591b7c9d0baa..a7b61e936d3cd 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__match_as_identifier.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ Expr( @@ -52,6 +52,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), }, @@ -105,6 +106,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), }, @@ -385,6 +387,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 298..300, elts: [], ctx: Load, + is_parenthesized: true, }, ), ], @@ -425,6 +428,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 329..331, elts: [], ctx: Load, + is_parenthesized: true, }, ), ], @@ -505,6 +509,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), ctx: Load, @@ -548,6 +553,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), ctx: Load, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap index 0e703191724d8..33f8e9f1037eb 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parenthesized_with_statement.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ With( @@ -171,6 +171,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( @@ -220,6 +221,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap index 8d868ecef23a3..321abd4ba2329 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_class_generic_types.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ ClassDef( @@ -143,6 +143,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), ), diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap index 0440c2f331309..def5b3384f9f4 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_double_list_comprehension.snap @@ -35,6 +35,7 @@ ListComp( ), ], ctx: Store, + is_parenthesized: false, }, ), iter: Name( diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap index c673e95d9658c..97937d913481d 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_function_definition.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ FunctionDef( @@ -254,6 +254,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), ), diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap index 3dd0a471b213c..bd90f04a7f541 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_tuples.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ Assign( @@ -27,6 +27,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Store, + is_parenthesized: false, }, ), ], @@ -52,6 +53,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap index 7730469ff75ef..df5282e180aa3 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__parse_type_declaration.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ TypeAlias( @@ -354,6 +354,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), }, @@ -438,6 +439,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), }, @@ -484,6 +486,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), ), @@ -537,6 +540,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), }, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap index c90e9f1e65a04..d25e5fefe94a2 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__patma.snap @@ -1659,6 +1659,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), cases: [ @@ -2148,6 +2149,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), cases: [ @@ -3287,6 +3289,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), cases: [ @@ -3385,6 +3388,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: true, }, ), cases: [ @@ -3466,6 +3470,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), cases: [ @@ -3542,6 +3547,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), cases: [ @@ -3635,6 +3641,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), cases: [ diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap index 3cd99121dedd8..aad39083aeb66 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__star_index.snap @@ -66,6 +66,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), ctx: Load, @@ -128,6 +129,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), ctx: Store, @@ -188,6 +190,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), ctx: Load, @@ -253,6 +256,7 @@ expression: parse_ast ), ], ctx: Load, + is_parenthesized: false, }, ), ctx: Load, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__tuple.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__tuple.snap new file mode 100644 index 0000000000000..46c04c10ba20f --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__tuple.snap @@ -0,0 +1,124 @@ +--- +source: crates/ruff_python_parser/src/parser.rs +expression: parse_ast +--- +[ + Expr( + StmtExpr { + range: 0..3, + value: Tuple( + ExprTuple { + range: 0..3, + elts: [ + Name( + ExprName { + range: 0..1, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 2..3, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + is_parenthesized: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 4..9, + value: Tuple( + ExprTuple { + range: 4..9, + elts: [ + Name( + ExprName { + range: 5..6, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 7..8, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + is_parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..12, + value: Tuple( + ExprTuple { + range: 10..12, + elts: [], + ctx: Load, + is_parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 13..17, + value: Tuple( + ExprTuple { + range: 13..17, + elts: [ + Name( + ExprName { + range: 14..15, + id: "a", + ctx: Load, + }, + ), + ], + ctx: Load, + is_parenthesized: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..25, + value: Tuple( + ExprTuple { + range: 19..24, + elts: [ + Name( + ExprName { + range: 20..21, + id: "a", + ctx: Load, + }, + ), + Name( + ExprName { + range: 22..23, + id: "b", + ctx: Load, + }, + ), + ], + ctx: Load, + is_parenthesized: true, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap index 0296e7ad0511e..a735975631041 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__type_as_identifier.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ Expr( @@ -52,6 +52,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), }, @@ -105,6 +106,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), }, @@ -385,6 +387,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 283..285, elts: [], ctx: Load, + is_parenthesized: true, }, ), ], @@ -425,6 +428,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 312..314, elts: [], ctx: Load, + is_parenthesized: true, }, ), ], @@ -505,6 +509,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: false, }, ), ctx: Load, @@ -548,6 +553,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), ctx: Load, diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap index dec15e10e878d..3dc0ccdb0e1ed 100644 --- a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__with_statement.snap @@ -1,6 +1,6 @@ --- source: crates/ruff_python_parser/src/parser.rs -expression: "parse_suite(source, \"\").unwrap()" +expression: parse_suite(source).unwrap() --- [ With( @@ -275,6 +275,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 133..135, elts: [], ctx: Load, + is_parenthesized: true, }, ), optional_vars: None, @@ -301,6 +302,7 @@ expression: "parse_suite(source, \"\").unwrap()" range: 147..149, elts: [], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( @@ -433,6 +435,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( @@ -523,6 +526,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( @@ -571,6 +575,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: None, @@ -611,6 +616,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( @@ -667,6 +673,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: None, @@ -715,6 +722,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some( @@ -876,6 +884,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: None, @@ -943,6 +952,7 @@ expression: "parse_suite(source, \"\").unwrap()" ), ], ctx: Load, + is_parenthesized: true, }, ), optional_vars: Some(