Upgrade RustPython (#5192)

## Summary

This PR upgrade RustPython to pull in the changes to `Arguments` (zip
defaults with their identifiers) and all the renames to `CmpOp` and
friends.
This commit is contained in:
Charlie Marsh 2023-06-19 17:09:53 -04:00 committed by GitHub
parent ddfdc3bb01
commit 36e01ad6eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
103 changed files with 1291 additions and 1165 deletions

View file

@ -14,21 +14,21 @@ pub(super) fn place_comment<'a>(
comment: DecoratedComment<'a>,
locator: &Locator,
) -> CommentPlacement<'a> {
handle_in_between_excepthandlers_or_except_handler_and_else_or_finally_comment(comment, locator)
.or_else(|comment| handle_match_comment(comment, locator))
.or_else(|comment| handle_in_between_bodies_own_line_comment(comment, locator))
.or_else(|comment| handle_in_between_bodies_end_of_line_comment(comment, locator))
.or_else(|comment| handle_trailing_body_comment(comment, locator))
.or_else(handle_trailing_end_of_line_body_comment)
.or_else(|comment| handle_trailing_end_of_line_condition_comment(comment, locator))
.or_else(|comment| {
handle_module_level_own_line_comment_before_class_or_function_comment(comment, locator)
})
.or_else(|comment| handle_positional_only_arguments_separator_comment(comment, locator))
.or_else(|comment| {
handle_trailing_binary_expression_left_or_operator_comment(comment, locator)
})
.or_else(handle_leading_function_with_decorators_comment)
handle_in_between_except_handlers_or_except_handler_and_else_or_finally_comment(
comment, locator,
)
.or_else(|comment| handle_match_comment(comment, locator))
.or_else(|comment| handle_in_between_bodies_own_line_comment(comment, locator))
.or_else(|comment| handle_in_between_bodies_end_of_line_comment(comment, locator))
.or_else(|comment| handle_trailing_body_comment(comment, locator))
.or_else(handle_trailing_end_of_line_body_comment)
.or_else(|comment| handle_trailing_end_of_line_condition_comment(comment, locator))
.or_else(|comment| {
handle_module_level_own_line_comment_before_class_or_function_comment(comment, locator)
})
.or_else(|comment| handle_positional_only_arguments_separator_comment(comment, locator))
.or_else(|comment| handle_trailing_binary_expression_left_or_operator_comment(comment, locator))
.or_else(handle_leading_function_with_decorators_comment)
}
/// Handles leading comments in front of a match case or a trailing comment of the `match` statement.
@ -138,8 +138,8 @@ fn handle_match_comment<'a>(
}
}
/// Handles comments between excepthandlers and between the last except handler and any following `else` or `finally` block.
fn handle_in_between_excepthandlers_or_except_handler_and_else_or_finally_comment<'a>(
/// Handles comments between except handlers and between the last except handler and any following `else` or `finally` block.
fn handle_in_between_except_handlers_or_except_handler_and_else_or_finally_comment<'a>(
comment: DecoratedComment<'a>,
locator: &Locator,
) -> CommentPlacement<'a> {
@ -147,7 +147,7 @@ fn handle_in_between_excepthandlers_or_except_handler_and_else_or_finally_commen
return CommentPlacement::Default(comment);
}
if let Some(AnyNodeRef::ExcepthandlerExceptHandler(except_handler)) = comment.preceding_node() {
if let Some(AnyNodeRef::ExceptHandlerExceptHandler(except_handler)) = comment.preceding_node() {
// it now depends on the indentation level of the comment if it is a leading comment for e.g.
// the following `elif` or indeed a trailing comment of the previous body's last statement.
let comment_indentation =
@ -627,11 +627,8 @@ fn handle_positional_only_arguments_separator_comment<'a>(
// ```python
// def test(a=10, /, b): pass
// ```
|| arguments
.defaults
.iter()
.position(|default| AnyNodeRef::from(default).ptr_eq(last_argument_or_default))
== Some(arguments.posonlyargs.len().saturating_sub(1));
|| are_same_optional(last_argument_or_default, arguments
.posonlyargs.last().and_then(|arg| arg.default.as_deref()));
if !is_last_positional_argument {
return CommentPlacement::Default(comment);
@ -906,7 +903,7 @@ fn last_child_in_body(node: AnyNodeRef) -> Option<AnyNodeRef> {
| AnyNodeRef::StmtWith(StmtWith { body, .. })
| AnyNodeRef::StmtAsyncWith(StmtAsyncWith { body, .. })
| AnyNodeRef::MatchCase(MatchCase { body, .. })
| AnyNodeRef::ExcepthandlerExceptHandler(ExcepthandlerExceptHandler { body, .. }) => body,
| AnyNodeRef::ExceptHandlerExceptHandler(ExceptHandlerExceptHandler { body, .. }) => body,
AnyNodeRef::StmtIf(StmtIf { body, orelse, .. })
| AnyNodeRef::StmtFor(StmtFor { body, orelse, .. })
@ -988,7 +985,7 @@ fn is_first_statement_in_enclosing_alternate_body(
}) => {
are_same_optional(following, handlers.first())
// Comments between the handlers and the `else`, or comments between the `handlers` and the `finally`
// are already handled by `handle_in_between_excepthandlers_or_except_handler_and_else_or_finally_comment`
// are already handled by `handle_in_between_except_handlers_or_except_handler_and_else_or_finally_comment`
|| handlers.is_empty() && are_same_optional(following, orelse.first())
|| (handlers.is_empty() || !orelse.is_empty())
&& are_same_optional(following, finalbody.first())

View file

@ -34,7 +34,7 @@ expression: comments.debug(test_case.source_code)
],
},
Node {
kind: ExcepthandlerExceptHandler,
kind: ExceptHandlerExceptHandler,
range: 100..136,
source: `except Exception as ex:⏎`,
}: {

View file

@ -39,7 +39,7 @@ expression: comments.debug(test_case.source_code)
],
},
Node {
kind: ExcepthandlerExceptHandler,
kind: ExceptHandlerExceptHandler,
range: 68..100,
source: `except Exception as ex:⏎`,
}: {

View file

@ -208,11 +208,11 @@ impl<'ast> PreorderVisitor<'ast> for CommentsVisitor<'ast> {
self.finish_node(comprehension);
}
fn visit_excepthandler(&mut self, excepthandler: &'ast Excepthandler) {
if self.start_node(excepthandler).is_traverse() {
walk_excepthandler(self, excepthandler);
fn visit_except_handler(&mut self, except_handler: &'ast ExceptHandler) {
if self.start_node(except_handler).is_traverse() {
walk_except_handler(self, except_handler);
}
self.finish_node(excepthandler);
self.finish_node(except_handler);
}
fn visit_format_spec(&mut self, format_spec: &'ast Expr) {
@ -250,12 +250,12 @@ impl<'ast> PreorderVisitor<'ast> for CommentsVisitor<'ast> {
self.finish_node(alias);
}
fn visit_withitem(&mut self, withitem: &'ast Withitem) {
if self.start_node(withitem).is_traverse() {
walk_withitem(self, withitem);
fn visit_with_item(&mut self, with_item: &'ast WithItem) {
if self.start_node(with_item).is_traverse() {
walk_with_item(self, with_item);
}
self.finish_node(withitem);
self.finish_node(with_item);
}
fn visit_match_case(&mut self, match_case: &'ast MatchCase) {

View file

@ -10,7 +10,7 @@ use ruff_formatter::{
};
use ruff_python_ast::node::AstNode;
use rustpython_parser::ast::{
Constant, Expr, ExprAttribute, ExprBinOp, ExprConstant, ExprUnaryOp, Operator, Unaryop,
Constant, Expr, ExprAttribute, ExprBinOp, ExprConstant, ExprUnaryOp, Operator, UnaryOp,
};
#[derive(Default)]
@ -116,7 +116,7 @@ const fn is_simple_power_expression(expr: &ExprBinOp) -> bool {
const fn is_simple_power_operand(expr: &Expr) -> bool {
match expr {
Expr::UnaryOp(ExprUnaryOp {
op: Unaryop::Not, ..
op: UnaryOp::Not, ..
}) => false,
Expr::Constant(ExprConstant {
value: Constant::Complex { .. } | Constant::Float(_) | Constant::Int(_),

View file

@ -2253,42 +2253,44 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExprSlice {
}
}
impl FormatRule<ast::ExcepthandlerExceptHandler, PyFormatContext<'_>>
for crate::other::excepthandler_except_handler::FormatExcepthandlerExceptHandler
impl FormatRule<ast::ExceptHandlerExceptHandler, PyFormatContext<'_>>
for crate::other::except_handler_except_handler::FormatExceptHandlerExceptHandler
{
#[inline]
fn fmt(
&self,
node: &ast::ExcepthandlerExceptHandler,
node: &ast::ExceptHandlerExceptHandler,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::ExcepthandlerExceptHandler>::fmt(self, node, f)
FormatNodeRule::<ast::ExceptHandlerExceptHandler>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExcepthandlerExceptHandler {
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ExceptHandlerExceptHandler {
type Format<'a> = FormatRefWithRule<
'a,
ast::ExcepthandlerExceptHandler,
crate::other::excepthandler_except_handler::FormatExcepthandlerExceptHandler,
ast::ExceptHandlerExceptHandler,
crate::other::except_handler_except_handler::FormatExceptHandlerExceptHandler,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::other::excepthandler_except_handler::FormatExcepthandlerExceptHandler::default(),
crate::other::except_handler_except_handler::FormatExceptHandlerExceptHandler::default(
),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExcepthandlerExceptHandler {
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ExceptHandlerExceptHandler {
type Format = FormatOwnedWithRule<
ast::ExcepthandlerExceptHandler,
crate::other::excepthandler_except_handler::FormatExcepthandlerExceptHandler,
ast::ExceptHandlerExceptHandler,
crate::other::except_handler_except_handler::FormatExceptHandlerExceptHandler,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::other::excepthandler_except_handler::FormatExcepthandlerExceptHandler::default(),
crate::other::except_handler_except_handler::FormatExceptHandlerExceptHandler::default(
),
)
}
}
@ -2746,6 +2748,46 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::Arg {
}
}
impl FormatRule<ast::ArgWithDefault, PyFormatContext<'_>>
for crate::other::arg_with_default::FormatArgWithDefault
{
#[inline]
fn fmt(
&self,
node: &ast::ArgWithDefault,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::ArgWithDefault>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::ArgWithDefault {
type Format<'a> = FormatRefWithRule<
'a,
ast::ArgWithDefault,
crate::other::arg_with_default::FormatArgWithDefault,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(
self,
crate::other::arg_with_default::FormatArgWithDefault::default(),
)
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::ArgWithDefault {
type Format = FormatOwnedWithRule<
ast::ArgWithDefault,
crate::other::arg_with_default::FormatArgWithDefault,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(
self,
crate::other::arg_with_default::FormatArgWithDefault::default(),
)
}
}
impl FormatRule<ast::Keyword, PyFormatContext<'_>> for crate::other::keyword::FormatKeyword {
#[inline]
fn fmt(&self, node: &ast::Keyword, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
@ -2795,35 +2837,35 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::Alias {
}
}
impl FormatRule<ast::Withitem, PyFormatContext<'_>> for crate::other::withitem::FormatWithitem {
impl FormatRule<ast::WithItem, PyFormatContext<'_>> for crate::other::with_item::FormatWithItem {
#[inline]
fn fmt(
&self,
node: &ast::Withitem,
node: &ast::WithItem,
f: &mut Formatter<PyFormatContext<'_>>,
) -> FormatResult<()> {
FormatNodeRule::<ast::Withitem>::fmt(self, node, f)
FormatNodeRule::<ast::WithItem>::fmt(self, node, f)
}
}
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::Withitem {
impl<'ast> AsFormat<PyFormatContext<'ast>> for ast::WithItem {
type Format<'a> = FormatRefWithRule<
'a,
ast::Withitem,
crate::other::withitem::FormatWithitem,
ast::WithItem,
crate::other::with_item::FormatWithItem,
PyFormatContext<'ast>,
>;
fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(self, crate::other::withitem::FormatWithitem::default())
FormatRefWithRule::new(self, crate::other::with_item::FormatWithItem::default())
}
}
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::Withitem {
impl<'ast> IntoFormat<PyFormatContext<'ast>> for ast::WithItem {
type Format = FormatOwnedWithRule<
ast::Withitem,
crate::other::withitem::FormatWithitem,
ast::WithItem,
crate::other::with_item::FormatWithItem,
PyFormatContext<'ast>,
>;
fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, crate::other::withitem::FormatWithitem::default())
FormatOwnedWithRule::new(self, crate::other::with_item::FormatWithItem::default())
}
}

View file

@ -0,0 +1,28 @@
use rustpython_parser::ast::ArgWithDefault;
use ruff_formatter::write;
use crate::prelude::*;
use crate::FormatNodeRule;
#[derive(Default)]
pub struct FormatArgWithDefault;
impl FormatNodeRule<ArgWithDefault> for FormatArgWithDefault {
fn fmt_fields(&self, item: &ArgWithDefault, f: &mut PyFormatter) -> FormatResult<()> {
let ArgWithDefault {
range: _,
def,
default,
} = item;
write!(f, [def.format()])?;
if let Some(default) = default {
let space = def.annotation.is_some().then_some(space());
write!(f, [space, text("="), space, default.format()])?;
}
Ok(())
}
}

View file

@ -1,12 +1,15 @@
use std::usize;
use rustpython_parser::ast::{Arguments, Ranged};
use ruff_formatter::{format_args, write};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use crate::comments::{dangling_node_comments, leading_node_comments};
use crate::context::NodeLevel;
use crate::prelude::*;
use crate::trivia::{first_non_trivia_token, SimpleTokenizer, Token, TokenKind};
use crate::FormatNodeRule;
use ruff_formatter::{format_args, write, FormatError};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use rustpython_parser::ast::{Arg, Arguments, Expr, Ranged};
use std::usize;
#[derive(Default)]
pub struct FormatArguments;
@ -17,10 +20,8 @@ impl FormatNodeRule<Arguments> for FormatArguments {
range: _,
posonlyargs,
args,
defaults,
vararg,
kwonlyargs,
kw_defaults,
kwarg,
} = item;
@ -32,30 +33,30 @@ impl FormatNodeRule<Arguments> for FormatArguments {
let mut joiner = f.join_with(separator);
let mut last_node: Option<AnyNodeRef> = None;
let mut defaults = std::iter::repeat(None)
.take(posonlyargs.len() + args.len() - defaults.len())
.chain(defaults.iter().map(Some));
for arg_with_default in posonlyargs {
joiner.entry(&arg_with_default.into_format());
for positional in posonlyargs {
let default = defaults.next().ok_or(FormatError::SyntaxError)?;
joiner.entry(&ArgumentWithDefault {
argument: positional,
default,
});
last_node = Some(default.map_or_else(|| positional.into(), AnyNodeRef::from));
last_node = Some(
arg_with_default
.default
.as_deref()
.map_or_else(|| (&arg_with_default.def).into(), AnyNodeRef::from),
);
}
if !posonlyargs.is_empty() {
joiner.entry(&text("/"));
}
for argument in args {
let default = defaults.next().ok_or(FormatError::SyntaxError)?;
for arg_with_default in args {
joiner.entry(&arg_with_default.into_format());
joiner.entry(&ArgumentWithDefault { argument, default });
last_node = Some(default.map_or_else(|| argument.into(), AnyNodeRef::from));
last_node = Some(
arg_with_default
.default
.as_deref()
.map_or_else(|| (&arg_with_default.def).into(), AnyNodeRef::from),
);
}
// kw only args need either a `*args` ahead of them capturing all var args or a `*`
@ -72,24 +73,17 @@ impl FormatNodeRule<Arguments> for FormatArguments {
joiner.entry(&text("*"));
}
debug_assert!(defaults.next().is_none());
for arg_with_default in kwonlyargs {
joiner.entry(&arg_with_default.into_format());
let mut defaults = std::iter::repeat(None)
.take(kwonlyargs.len() - kw_defaults.len())
.chain(kw_defaults.iter().map(Some));
for keyword_argument in kwonlyargs {
let default = defaults.next().ok_or(FormatError::SyntaxError)?;
joiner.entry(&ArgumentWithDefault {
argument: keyword_argument,
default,
});
last_node = Some(default.map_or_else(|| keyword_argument.into(), AnyNodeRef::from));
last_node = Some(
arg_with_default
.default
.as_deref()
.map_or_else(|| (&arg_with_default.def).into(), AnyNodeRef::from),
);
}
debug_assert!(defaults.next().is_none());
if let Some(kwarg) = kwarg {
joiner.entry(&format_args![
leading_node_comments(kwarg.as_ref()),
@ -173,21 +167,3 @@ impl FormatNodeRule<Arguments> for FormatArguments {
Ok(())
}
}
struct ArgumentWithDefault<'a> {
argument: &'a Arg,
default: Option<&'a Expr>,
}
impl Format<PyFormatContext<'_>> for ArgumentWithDefault<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
write!(f, [self.argument.format()])?;
if let Some(default) = self.default {
let space = self.argument.annotation.is_some().then_some(space());
write!(f, [space, text("="), space, default.format()])?;
}
Ok(())
}
}

View file

@ -1,14 +1,14 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExcepthandlerExceptHandler;
use rustpython_parser::ast::ExceptHandlerExceptHandler;
#[derive(Default)]
pub struct FormatExcepthandlerExceptHandler;
pub struct FormatExceptHandlerExceptHandler;
impl FormatNodeRule<ExcepthandlerExceptHandler> for FormatExcepthandlerExceptHandler {
impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHandler {
fn fmt_fields(
&self,
item: &ExcepthandlerExceptHandler,
item: &ExceptHandlerExceptHandler,
f: &mut PyFormatter,
) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])

View file

@ -1,10 +1,11 @@
pub(crate) mod alias;
pub(crate) mod arg;
pub(crate) mod arg_with_default;
pub(crate) mod arguments;
pub(crate) mod comprehension;
pub(crate) mod decorator;
pub(crate) mod excepthandler_except_handler;
pub(crate) mod except_handler_except_handler;
pub(crate) mod keyword;
pub(crate) mod match_case;
pub(crate) mod type_ignore_type_ignore;
pub(crate) mod withitem;
pub(crate) mod with_item;

View file

@ -1,12 +1,12 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::Withitem;
use rustpython_parser::ast::WithItem;
#[derive(Default)]
pub struct FormatWithitem;
pub struct FormatWithItem;
impl FormatNodeRule<Withitem> for FormatWithitem {
fn fmt_fields(&self, item: &Withitem, f: &mut PyFormatter) -> FormatResult<()> {
impl FormatNodeRule<WithItem> for FormatWithItem {
fn fmt_fields(&self, item: &WithItem, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
}
}