mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Upgrade RustPython (#4900)
This commit is contained in:
parent
4b78141f6b
commit
39a1f3980f
51 changed files with 416 additions and 320 deletions
|
@ -1,4 +1,4 @@
|
|||
use rustpython_parser::ast::{self, Expr, Stmt};
|
||||
use rustpython_parser::ast::{self, Decorator, Stmt};
|
||||
|
||||
pub fn name(stmt: &Stmt) -> &str {
|
||||
match stmt {
|
||||
|
@ -8,7 +8,7 @@ pub fn name(stmt: &Stmt) -> &str {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn decorator_list(stmt: &Stmt) -> &[Expr] {
|
||||
pub fn decorator_list(stmt: &Stmt) -> &[Decorator] {
|
||||
match stmt {
|
||||
Stmt::FunctionDef(ast::StmtFunctionDef { decorator_list, .. })
|
||||
| Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef { decorator_list, .. }) => {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
//! ability to compare expressions for equality (via [`Eq`] and [`Hash`]).
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use rustpython_ast::Decorator;
|
||||
use rustpython_parser::ast::{
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, ConversionFlag,
|
||||
Excepthandler, Expr, ExprContext, Identifier, Int, Keyword, MatchCase, Operator, Pattern, Stmt,
|
||||
|
@ -267,6 +268,19 @@ impl<'a> From<&'a MatchCase> for ComparableMatchCase<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableDecorator<'a> {
|
||||
pub expression: ComparableExpr<'a>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Decorator> for ComparableDecorator<'a> {
|
||||
fn from(decorator: &'a Decorator) -> Self {
|
||||
Self {
|
||||
expression: (&decorator.expression).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableConstant<'a> {
|
||||
None,
|
||||
|
@ -777,7 +791,7 @@ pub enum ComparableStmt<'a> {
|
|||
name: &'a str,
|
||||
args: ComparableArguments<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableExpr<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
type_comment: Option<&'a str>,
|
||||
},
|
||||
|
@ -785,7 +799,7 @@ pub enum ComparableStmt<'a> {
|
|||
name: &'a str,
|
||||
args: ComparableArguments<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableExpr<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
type_comment: Option<&'a str>,
|
||||
},
|
||||
|
@ -794,7 +808,7 @@ pub enum ComparableStmt<'a> {
|
|||
bases: Vec<ComparableExpr<'a>>,
|
||||
keywords: Vec<ComparableKeyword<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableExpr<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
},
|
||||
Return {
|
||||
value: Option<ComparableExpr<'a>>,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::node::AnyNodeRef;
|
||||
use ruff_text_size::TextRange;
|
||||
use rustpython_ast::{
|
||||
Arguments, Expr, Identifier, Ranged, StmtAsyncFunctionDef, StmtFunctionDef, Suite,
|
||||
Arguments, Decorator, Expr, Identifier, Ranged, StmtAsyncFunctionDef, StmtFunctionDef, Suite,
|
||||
};
|
||||
|
||||
/// Enum that represents any python function definition.
|
||||
|
@ -65,7 +65,7 @@ impl<'a> AnyFunctionDefinition<'a> {
|
|||
}
|
||||
|
||||
/// Returns the decorators attributing the function.
|
||||
pub fn decorators(self) -> &'a [Expr] {
|
||||
pub fn decorators(self) -> &'a [Decorator] {
|
||||
match self {
|
||||
Self::FunctionDefinition(definition) => &definition.decorator_list,
|
||||
Self::AsyncFunctionDefinition(definition) => &definition.decorator_list,
|
||||
|
|
|
@ -363,7 +363,9 @@ where
|
|||
.map_or(false, |expr| any_over_expr(expr, func))
|
||||
})
|
||||
|| body.iter().any(|stmt| any_over_stmt(stmt, func))
|
||||
|| decorator_list.iter().any(|expr| any_over_expr(expr, func))
|
||||
|| decorator_list
|
||||
.iter()
|
||||
.any(|decorator| any_over_expr(&decorator.expression, func))
|
||||
|| returns
|
||||
.as_ref()
|
||||
.map_or(false, |value| any_over_expr(value, func))
|
||||
|
@ -380,7 +382,9 @@ where
|
|||
.iter()
|
||||
.any(|keyword| any_over_expr(&keyword.value, func))
|
||||
|| body.iter().any(|stmt| any_over_stmt(stmt, func))
|
||||
|| decorator_list.iter().any(|expr| any_over_expr(expr, func))
|
||||
|| decorator_list
|
||||
.iter()
|
||||
.any(|decorator| any_over_expr(&decorator.expression, func))
|
||||
}
|
||||
Stmt::Return(ast::StmtReturn {
|
||||
value,
|
||||
|
|
|
@ -92,6 +92,7 @@ pub enum AnyNode {
|
|||
Alias(Alias<TextRange>),
|
||||
Withitem(Withitem<TextRange>),
|
||||
MatchCase(MatchCase<TextRange>),
|
||||
Decorator(Decorator<TextRange>),
|
||||
}
|
||||
|
||||
impl AnyNode {
|
||||
|
@ -172,7 +173,8 @@ impl AnyNode {
|
|||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::MatchCase(_) => None,
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,7 +255,8 @@ impl AnyNode {
|
|||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::MatchCase(_) => None,
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -334,7 +337,8 @@ impl AnyNode {
|
|||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::MatchCase(_) => None,
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -415,7 +419,8 @@ impl AnyNode {
|
|||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::MatchCase(_) => None,
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -496,7 +501,8 @@ impl AnyNode {
|
|||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::MatchCase(_) => None,
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -577,7 +583,8 @@ impl AnyNode {
|
|||
| AnyNode::Keyword(_)
|
||||
| AnyNode::Alias(_)
|
||||
| AnyNode::Withitem(_)
|
||||
| AnyNode::MatchCase(_) => None,
|
||||
| AnyNode::MatchCase(_)
|
||||
| AnyNode::Decorator(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -682,6 +689,7 @@ impl AnyNode {
|
|||
Self::Alias(node) => AnyNodeRef::Alias(node),
|
||||
Self::Withitem(node) => AnyNodeRef::Withitem(node),
|
||||
Self::MatchCase(node) => AnyNodeRef::MatchCase(node),
|
||||
Self::Decorator(node) => AnyNodeRef::Decorator(node),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2793,6 +2801,35 @@ impl AstNode for MatchCase<TextRange> {
|
|||
}
|
||||
}
|
||||
|
||||
impl AstNode for Decorator<TextRange> {
|
||||
fn cast(kind: AnyNode) -> Option<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if let AnyNode::Decorator(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn cast_ref(kind: AnyNodeRef) -> Option<&Self> {
|
||||
if let AnyNodeRef::Decorator(node) = kind {
|
||||
Some(node)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn as_any_node_ref(&self) -> AnyNodeRef {
|
||||
AnyNodeRef::from(self)
|
||||
}
|
||||
|
||||
fn into_any_node(self) -> AnyNode {
|
||||
AnyNode::from(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Stmt> for AnyNode {
|
||||
fn from(stmt: Stmt) -> Self {
|
||||
match stmt {
|
||||
|
@ -3346,6 +3383,11 @@ impl From<MatchCase> for AnyNode {
|
|||
AnyNode::MatchCase(node)
|
||||
}
|
||||
}
|
||||
impl From<Decorator> for AnyNode {
|
||||
fn from(node: Decorator) -> Self {
|
||||
AnyNode::Decorator(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl Ranged for AnyNode {
|
||||
fn range(&self) -> TextRange {
|
||||
|
@ -3425,6 +3467,7 @@ impl Ranged for AnyNode {
|
|||
AnyNode::Alias(node) => node.range(),
|
||||
AnyNode::Withitem(node) => node.range(),
|
||||
AnyNode::MatchCase(node) => node.range(),
|
||||
AnyNode::Decorator(node) => node.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3506,6 +3549,7 @@ pub enum AnyNodeRef<'a> {
|
|||
Alias(&'a Alias<TextRange>),
|
||||
Withitem(&'a Withitem<TextRange>),
|
||||
MatchCase(&'a MatchCase<TextRange>),
|
||||
Decorator(&'a Decorator<TextRange>),
|
||||
}
|
||||
|
||||
impl AnyNodeRef<'_> {
|
||||
|
@ -3586,6 +3630,7 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::Alias(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Withitem(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::MatchCase(node) => NonNull::from(*node).cast(),
|
||||
AnyNodeRef::Decorator(node) => NonNull::from(*node).cast(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3672,6 +3717,7 @@ impl AnyNodeRef<'_> {
|
|||
AnyNodeRef::Alias(_) => NodeKind::Alias,
|
||||
AnyNodeRef::Withitem(_) => NodeKind::Withitem,
|
||||
AnyNodeRef::MatchCase(_) => NodeKind::MatchCase,
|
||||
AnyNodeRef::Decorator(_) => NodeKind::Decorator,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3752,7 +3798,8 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::MatchCase(_) => false,
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3833,7 +3880,8 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::MatchCase(_) => false,
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3914,7 +3962,8 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::MatchCase(_) => false,
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3995,7 +4044,8 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::MatchCase(_) => false,
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4076,7 +4126,8 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::MatchCase(_) => false,
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4157,7 +4208,8 @@ impl AnyNodeRef<'_> {
|
|||
| AnyNodeRef::Keyword(_)
|
||||
| AnyNodeRef::Alias(_)
|
||||
| AnyNodeRef::Withitem(_)
|
||||
| AnyNodeRef::MatchCase(_) => false,
|
||||
| AnyNodeRef::MatchCase(_)
|
||||
| AnyNodeRef::Decorator(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4570,6 +4622,12 @@ impl<'a> From<&'a TypeIgnoreTypeIgnore> for AnyNodeRef<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Decorator> for AnyNodeRef<'a> {
|
||||
fn from(node: &'a Decorator) -> Self {
|
||||
AnyNodeRef::Decorator(node)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a Stmt> for AnyNodeRef<'a> {
|
||||
fn from(stmt: &'a Stmt) -> Self {
|
||||
match stmt {
|
||||
|
@ -4796,6 +4854,7 @@ impl Ranged for AnyNodeRef<'_> {
|
|||
AnyNodeRef::Alias(node) => node.range(),
|
||||
AnyNodeRef::Withitem(node) => node.range(),
|
||||
AnyNodeRef::MatchCase(node) => node.range(),
|
||||
AnyNodeRef::Decorator(node) => node.range(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4877,4 +4936,5 @@ pub enum NodeKind {
|
|||
Alias,
|
||||
Withitem,
|
||||
MatchCase,
|
||||
Decorator,
|
||||
}
|
||||
|
|
|
@ -212,7 +212,7 @@ impl<'a> Generator<'a> {
|
|||
for decorator in decorator_list {
|
||||
statement!({
|
||||
self.p("@");
|
||||
self.unparse_expr(decorator, precedence::MAX);
|
||||
self.unparse_expr(&decorator.expression, precedence::MAX);
|
||||
});
|
||||
}
|
||||
statement!({
|
||||
|
@ -244,7 +244,7 @@ impl<'a> Generator<'a> {
|
|||
for decorator in decorator_list {
|
||||
statement!({
|
||||
self.p("@");
|
||||
self.unparse_expr(decorator, precedence::MAX);
|
||||
self.unparse_expr(&decorator.expression, precedence::MAX);
|
||||
});
|
||||
}
|
||||
statement!({
|
||||
|
@ -276,7 +276,7 @@ impl<'a> Generator<'a> {
|
|||
for decorator in decorator_list {
|
||||
statement!({
|
||||
self.p("@");
|
||||
self.unparse_expr(decorator, precedence::MAX);
|
||||
self.unparse_expr(&decorator.expression, precedence::MAX);
|
||||
});
|
||||
}
|
||||
statement!({
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
pub mod preorder;
|
||||
|
||||
use rustpython_ast::Decorator;
|
||||
use rustpython_parser::ast::{
|
||||
self, Alias, Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Excepthandler, Expr,
|
||||
ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, Unaryop, Withitem,
|
||||
|
@ -21,6 +22,9 @@ pub trait Visitor<'a> {
|
|||
fn visit_annotation(&mut self, expr: &'a Expr) {
|
||||
walk_expr(self, expr);
|
||||
}
|
||||
fn visit_decorator(&mut self, decorator: &'a Decorator) {
|
||||
walk_decorator(self, decorator);
|
||||
}
|
||||
fn visit_expr(&mut self, expr: &'a Expr) {
|
||||
walk_expr(self, expr);
|
||||
}
|
||||
|
@ -93,8 +97,8 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
..
|
||||
}) => {
|
||||
visitor.visit_arguments(args);
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
}
|
||||
for expr in returns {
|
||||
visitor.visit_annotation(expr);
|
||||
|
@ -109,8 +113,8 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
..
|
||||
}) => {
|
||||
visitor.visit_arguments(args);
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
}
|
||||
for expr in returns {
|
||||
visitor.visit_annotation(expr);
|
||||
|
@ -130,8 +134,8 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
for keyword in keywords {
|
||||
visitor.visit_keyword(keyword);
|
||||
}
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
}
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
|
@ -318,6 +322,10 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_decorator<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, decorator: &'a Decorator) {
|
||||
visitor.visit_expr(&decorator.expression);
|
||||
}
|
||||
|
||||
pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
|
||||
match expr {
|
||||
Expr::BoolOp(ast::ExprBoolOp {
|
||||
|
|
|
@ -18,6 +18,10 @@ pub trait PreorderVisitor<'a> {
|
|||
walk_expr(self, expr);
|
||||
}
|
||||
|
||||
fn visit_decorator(&mut self, decorator: &'a Decorator) {
|
||||
walk_decorator(self, decorator);
|
||||
}
|
||||
|
||||
fn visit_constant(&mut self, constant: &'a Constant) {
|
||||
walk_constant(self, constant);
|
||||
}
|
||||
|
@ -151,8 +155,8 @@ where
|
|||
returns,
|
||||
..
|
||||
}) => {
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
}
|
||||
|
||||
visitor.visit_arguments(args);
|
||||
|
@ -171,8 +175,8 @@ where
|
|||
decorator_list,
|
||||
..
|
||||
}) => {
|
||||
for expr in decorator_list {
|
||||
visitor.visit_expr(expr);
|
||||
for decorator in decorator_list {
|
||||
visitor.visit_decorator(decorator);
|
||||
}
|
||||
|
||||
for expr in bases {
|
||||
|
@ -387,6 +391,13 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn walk_decorator<'a, V>(visitor: &mut V, decorator: &'a Decorator)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
{
|
||||
visitor.visit_expr(&decorator.expression);
|
||||
}
|
||||
|
||||
pub fn walk_expr<'a, V>(visitor: &mut V, expr: &'a Expr)
|
||||
where
|
||||
V: PreorderVisitor<'a> + ?Sized,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue