Update ArgumentsParentheses usage (#6070)

This commit is contained in:
Chris Pryer 2023-07-25 12:03:48 -04:00 committed by GitHub
parent 5f63b8bfb8
commit f5c69c1b34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View file

@ -27,8 +27,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
f, f,
[ [
space(), space(),
args.format() args.format().with_options(ArgumentsParentheses::Never),
.with_options(ArgumentsParentheses::SkipInsideLambda),
] ]
)?; )?;
} }

View file

@ -18,11 +18,22 @@ use crate::FormatNodeRule;
#[derive(Eq, PartialEq, Debug, Default)] #[derive(Eq, PartialEq, Debug, Default)]
pub enum ArgumentsParentheses { pub enum ArgumentsParentheses {
/// By default, arguments will always preserve their surrounding parentheses.
#[default] #[default]
Default, Preserve,
/// Arguments should never be inside parentheses for lambda expressions. /// Handle special cases where parentheses should never be used.
SkipInsideLambda, ///
/// 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)] #[derive(Default)]
@ -190,7 +201,7 @@ impl FormatNodeRule<Arguments> for FormatArguments {
+ kwonlyargs.len() + kwonlyargs.len()
+ usize::from(kwarg.is_some()); + usize::from(kwarg.is_some());
if self.parentheses == ArgumentsParentheses::SkipInsideLambda { if self.parentheses == ArgumentsParentheses::Never {
group(&format_inner).fmt(f)?; group(&format_inner).fmt(f)?;
} else if num_arguments == 0 { } else if num_arguments == 0 {
// No arguments, format any dangling comments between `()` // No arguments, format any dangling comments between `()`