From f5c69c1b340ee9383aa90647d7bd6462b58f3b31 Mon Sep 17 00:00:00 2001 From: Chris Pryer <14341145+cnpryer@users.noreply.github.com> Date: Tue, 25 Jul 2023 12:03:48 -0400 Subject: [PATCH] Update `ArgumentsParentheses` usage (#6070) --- .../src/expression/expr_lambda.rs | 3 +-- .../src/other/arguments.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/expr_lambda.rs b/crates/ruff_python_formatter/src/expression/expr_lambda.rs index c1f6100c59..abafdd3c26 100644 --- a/crates/ruff_python_formatter/src/expression/expr_lambda.rs +++ b/crates/ruff_python_formatter/src/expression/expr_lambda.rs @@ -27,8 +27,7 @@ impl FormatNodeRule for FormatExprLambda { f, [ space(), - args.format() - .with_options(ArgumentsParentheses::SkipInsideLambda), + args.format().with_options(ArgumentsParentheses::Never), ] )?; } diff --git a/crates/ruff_python_formatter/src/other/arguments.rs b/crates/ruff_python_formatter/src/other/arguments.rs index 0300d9913b..b90db94d19 100644 --- a/crates/ruff_python_formatter/src/other/arguments.rs +++ b/crates/ruff_python_formatter/src/other/arguments.rs @@ -18,11 +18,22 @@ use crate::FormatNodeRule; #[derive(Eq, PartialEq, Debug, Default)] pub enum ArgumentsParentheses { + /// By default, arguments will always preserve their surrounding parentheses. #[default] - Default, + Preserve, - /// Arguments should never be inside parentheses for lambda expressions. - SkipInsideLambda, + /// Handle special cases where parentheses should never be used. + /// + /// An example where parentheses are never used for arguments would be with lambda + /// expressions. The following is invalid syntax: + /// ```python + /// lambda (x, y, z): ... + /// ``` + /// Instead the lambda here should be: + /// ```python + /// lambda x, y, z: ... + /// ``` + Never, } #[derive(Default)] @@ -190,7 +201,7 @@ impl FormatNodeRule for FormatArguments { + kwonlyargs.len() + usize::from(kwarg.is_some()); - if self.parentheses == ArgumentsParentheses::SkipInsideLambda { + if self.parentheses == ArgumentsParentheses::Never { group(&format_inner).fmt(f)?; } else if num_arguments == 0 { // No arguments, format any dangling comments between `()`