Add formatter support for call and class definition Arguments (#6274)

## Summary

This PR leverages the `Arguments` AST node introduced in #6259 in the
formatter, which ensures that we correctly handle trailing comments in
calls, like:

```python
f(
  1,
  # comment
)

pass
```

(Previously, this was treated as a leading comment on `pass`.)

This also allows us to unify the argument handling across calls and
class definitions.

## Test Plan

A bunch of new fixture tests, plus improved Black compatibility.
This commit is contained in:
Charlie Marsh 2023-08-02 11:54:22 -04:00 committed by GitHub
parent b095b7204b
commit 4c53bfe896
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 640 additions and 252 deletions

View file

@ -2617,6 +2617,38 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::Comprehension {
}
}
impl FormatRule<ast::Arguments, PyFormatContext<'_>> for crate::other::arguments::FormatArguments {
#[inline]
fn fmt(
&self,
node: &ast::Arguments,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::Arguments>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::Arguments {
type Format<'a> = FormatRefWithRule<
'a,
ast::Arguments,
crate::other::arguments::FormatArguments,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(self, crate::other::arguments::FormatArguments::default())
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::Arguments {
type Format = FormatOwnedWithRule<
ast::Arguments,
crate::other::arguments::FormatArguments,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, crate::other::arguments::FormatArguments::default())
}
}
impl FormatRule<ast::Parameters, PyFormatContext<'_>>
for crate::other::parameters::FormatParameters
{