mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
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:
parent
ddfdc3bb01
commit
36e01ad6eb
103 changed files with 1291 additions and 1165 deletions
28
crates/ruff_python_formatter/src/other/arg_with_default.rs
Normal file
28
crates/ruff_python_formatter/src/other/arg_with_default.rs
Normal 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(())
|
||||
}
|
||||
}
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)])
|
|
@ -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;
|
||||
|
|
|
@ -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)])
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue