Skip to content

Commit

Permalink
Introduce an arguments type
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 1, 2023
1 parent 9c708d8 commit 485f6b5
Show file tree
Hide file tree
Showing 12 changed files with 13,265 additions and 13,376 deletions.
3 changes: 1 addition & 2 deletions crates/ruff_python_ast/src/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ where
}
Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments: ast::Arguments { args, keywords, .. },
..
}) => {
// Allow `tuple()`, `list()`, and their generic forms, like `list[int]()`.
Expand Down
39 changes: 27 additions & 12 deletions crates/ruff_python_ast/src/comparable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,27 @@ impl<'a> From<&'a ast::Constant> for ComparableConstant<'a> {
}
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ComparableArguments<'a> {
args: Vec<ComparableExpr<'a>>,
keywords: Vec<ComparableKeyword<'a>>,
}

impl<'a> From<&'a ast::Arguments> for ComparableArguments<'a> {
fn from(arguments: &'a ast::Arguments) -> Self {
Self {
args: arguments.args.iter().map(Into::into).collect(),
keywords: arguments.keywords.iter().map(Into::into).collect(),
}
}
}

impl<'a> From<&'a Box<ast::Arguments>> for ComparableArguments<'a> {
fn from(arguments: &'a Box<ast::Arguments>) -> Self {
(arguments.as_ref()).into()
}
}

#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ComparableParameters<'a> {
posonlyargs: Vec<ComparableParameterWithDefault<'a>>,
Expand Down Expand Up @@ -583,8 +604,7 @@ pub struct ExprCompare<'a> {
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprCall<'a> {
func: Box<ComparableExpr<'a>>,
args: Vec<ComparableExpr<'a>>,
keywords: Vec<ComparableKeyword<'a>>,
arguments: ComparableArguments<'a>,
}

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -837,13 +857,11 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
}),
ast::Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments,
range: _range,
}) => Self::Call(ExprCall {
func: func.into(),
args: args.iter().map(Into::into).collect(),
keywords: keywords.iter().map(Into::into).collect(),
arguments: arguments.into(),
}),
ast::Expr::FormattedValue(ast::ExprFormattedValue {
value,
Expand Down Expand Up @@ -968,8 +986,7 @@ pub struct StmtAsyncFunctionDef<'a> {
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct StmtClassDef<'a> {
name: &'a str,
bases: Vec<ComparableExpr<'a>>,
keywords: Vec<ComparableKeyword<'a>>,
arguments: Option<ComparableArguments<'a>>,
body: Vec<ComparableStmt<'a>>,
decorator_list: Vec<ComparableDecorator<'a>>,
type_params: Vec<ComparableTypeParam<'a>>,
Expand Down Expand Up @@ -1240,16 +1257,14 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
}),
ast::Stmt::ClassDef(ast::StmtClassDef {
name,
bases,
keywords,
arguments,
body,
decorator_list,
type_params,
range: _range,
}) => Self::ClassDef(StmtClassDef {
name: name.as_str(),
bases: bases.iter().map(Into::into).collect(),
keywords: keywords.iter().map(Into::into).collect(),
arguments: arguments.as_ref().map(Into::into),
body: body.iter().map(Into::into).collect(),
decorator_list: decorator_list.iter().map(Into::into).collect(),
type_params: type_params.iter().map(Into::into).collect(),
Expand Down
53 changes: 19 additions & 34 deletions crates/ruff_python_ast/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::borrow::Cow;
use std::path::Path;

use crate::{
self as ast, Constant, ExceptHandler, Expr, Keyword, MatchCase, Parameters, Pattern, Ranged,
Stmt, TypeParam,
self as ast, Arguments, Constant, ExceptHandler, Expr, Keyword, MatchCase, Parameters, Pattern,
Ranged, Stmt, TypeParam,
};
use num_traits::Zero;
use ruff_text_size::TextRange;
Expand Down Expand Up @@ -50,8 +50,7 @@ where
// Accept empty initializers.
if let Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments: Arguments { args, keywords, .. },
range: _range,
}) = expr
{
Expand Down Expand Up @@ -237,8 +236,7 @@ where
}) => any_over_expr(left, func) || comparators.iter().any(|expr| any_over_expr(expr, func)),
Expr::Call(ast::ExprCall {
func: call_func,
args,
keywords,
arguments: Arguments { args, keywords, .. },
range: _range,
}) => {
any_over_expr(call_func, func)
Expand Down Expand Up @@ -396,16 +394,19 @@ where
.is_some_and(|value| any_over_expr(value, func))
}
Stmt::ClassDef(ast::StmtClassDef {
bases,
keywords,
arguments,
body,
decorator_list,
..
}) => {
bases.iter().any(|expr| any_over_expr(expr, func))
|| keywords
.iter()
.any(|keyword| any_over_expr(&keyword.value, func))
arguments
.as_ref()
.is_some_and(|Arguments { args, keywords, .. }| {
args.iter().any(|expr| any_over_expr(expr, func))
|| keywords
.iter()
.any(|keyword| any_over_expr(&keyword.value, func))
})
|| body.iter().any(|stmt| any_over_stmt(stmt, func))
|| decorator_list
.iter()
Expand Down Expand Up @@ -1229,30 +1230,14 @@ impl Truthiness {
None
}
}
Expr::List(ast::ExprList {
elts,
range: _range,
..
})
| Expr::Set(ast::ExprSet {
elts,
range: _range,
})
| Expr::Tuple(ast::ExprTuple {
elts,
range: _range,
..
}) => Some(!elts.is_empty()),
Expr::Dict(ast::ExprDict {
keys,
range: _range,
..
}) => Some(!keys.is_empty()),
Expr::List(ast::ExprList { elts, .. })
| Expr::Set(ast::ExprSet { elts, .. })
| Expr::Tuple(ast::ExprTuple { elts, .. }) => Some(!elts.is_empty()),
Expr::Dict(ast::ExprDict { keys, .. }) => Some(!keys.is_empty()),
Expr::Call(ast::ExprCall {
func,
args,
keywords,
range: _range,
arguments: Arguments { args, keywords, .. },
..
}) => {
if let Expr::Name(ast::ExprName { id, .. }) = func.as_ref() {
if is_iterable_initializer(id.as_str(), |id| is_builtin(id)) {
Expand Down
17 changes: 11 additions & 6 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ impl From<StmtAsyncFunctionDef> for Stmt {
pub struct StmtClassDef {
pub range: TextRange,
pub name: Identifier,
pub bases: Vec<Expr>,
pub keywords: Vec<Keyword>,
pub arguments: Option<Arguments>,
pub body: Vec<Stmt>,
pub type_params: Vec<TypeParam>,
pub decorator_list: Vec<Decorator>,
Expand Down Expand Up @@ -836,8 +835,7 @@ impl From<ExprCompare> for Expr {
pub struct ExprCall {
pub range: TextRange,
pub func: Box<Expr>,
pub args: Vec<Expr>,
pub keywords: Vec<Keyword>,
pub arguments: Arguments,
}

impl From<ExprCall> for Expr {
Expand Down Expand Up @@ -2073,6 +2071,13 @@ pub struct ParameterWithDefault {
pub default: Option<Box<Expr>>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Arguments {
pub range: TextRange,
pub args: Vec<Expr>,
pub keywords: Vec<Keyword>,
}

pub type Suite = Vec<Stmt>;

impl CmpOp {
Expand Down Expand Up @@ -2951,9 +2956,9 @@ mod size_assertions {
use super::*;
use static_assertions::assert_eq_size;

assert_eq_size!(Stmt, [u8; 168]);
assert_eq_size!(Stmt, [u8; 176]);
assert_eq_size!(StmtFunctionDef, [u8; 128]);
assert_eq_size!(StmtClassDef, [u8; 160]);
assert_eq_size!(StmtClassDef, [u8; 168]);
assert_eq_size!(StmtTry, [u8; 104]);
assert_eq_size!(Expr, [u8; 80]);
assert_eq_size!(Constant, [u8; 32]);
Expand Down
5 changes: 2 additions & 3 deletions crates/ruff_python_ast/src/relocate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{nodes, Expr, Keyword};
use crate::{nodes, Arguments, Expr, Keyword};
use ruff_text_size::TextRange;

fn relocate_keyword(keyword: &mut Keyword, location: TextRange) {
Expand Down Expand Up @@ -116,8 +116,7 @@ pub fn relocate_expr(expr: &mut Expr, location: TextRange) {
}
Expr::Call(nodes::ExprCall {
func,
args,
keywords,
arguments: Arguments { args, keywords, .. },
range,
}) => {
*range = location;
Expand Down
38 changes: 20 additions & 18 deletions crates/ruff_python_ast/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
pub mod preorder;

use crate::{
self as ast, Alias, BoolOp, CmpOp, Comprehension, Decorator, ElifElseClause, ExceptHandler,
Expr, ExprContext, Keyword, MatchCase, Operator, Parameter, Parameters, Pattern, Stmt,
TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
self as ast, Alias, Arguments, BoolOp, CmpOp, Comprehension, Decorator, ElifElseClause,
ExceptHandler, Expr, ExprContext, Keyword, MatchCase, Operator, Parameter, Parameters, Pattern,
Stmt, TypeParam, TypeParamTypeVar, UnaryOp, WithItem,
};

/// A trait for AST visitors. Visits all nodes in the AST recursively in evaluation-order.
Expand Down Expand Up @@ -52,6 +52,9 @@ pub trait Visitor<'a> {
fn visit_format_spec(&mut self, format_spec: &'a Expr) {
walk_format_spec(self, format_spec);
}
fn visit_arguments(&mut self, arguments: &'a Arguments) {
walk_arguments(self, arguments);
}
fn visit_parameters(&mut self, parameters: &'a Parameters) {
walk_parameters(self, parameters);
}
Expand Down Expand Up @@ -143,8 +146,7 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
visitor.visit_body(body);
}
Stmt::ClassDef(ast::StmtClassDef {
bases,
keywords,
arguments,
body,
decorator_list,
type_params,
Expand All @@ -156,11 +158,8 @@ pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
for type_param in type_params {
visitor.visit_type_param(type_param);
}
for expr in bases {
visitor.visit_expr(expr);
}
for keyword in keywords {
visitor.visit_keyword(keyword);
if let Some(arguments) = arguments {
visitor.visit_arguments(arguments);
}
visitor.visit_body(body);
}
Expand Down Expand Up @@ -522,17 +521,11 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
}
Expr::Call(ast::ExprCall {
func,
args,
keywords,
arguments,
range: _range,
}) => {
visitor.visit_expr(func);
for expr in args {
visitor.visit_expr(expr);
}
for keyword in keywords {
visitor.visit_keyword(keyword);
}
visitor.visit_arguments(arguments);
}
Expr::FormattedValue(ast::ExprFormattedValue {
value, format_spec, ..
Expand Down Expand Up @@ -645,6 +638,15 @@ pub fn walk_format_spec<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, format_spe
visitor.visit_expr(format_spec);
}

pub fn walk_arguments<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arguments: &'a Arguments) {
for arg in &arguments.args {
visitor.visit_expr(arg);
}
for keyword in &arguments.keywords {
visitor.visit_keyword(keyword);
}
}

pub fn walk_parameters<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, parameters: &'a Parameters) {
// Defaults are evaluated before annotations.
for arg in &parameters.posonlyargs {
Expand Down
Loading

0 comments on commit 485f6b5

Please sign in to comment.