Rename Arguments to Parameters in the AST (#6253)

## Summary

This PR renames a few AST nodes for clarity:

- `Arguments` is now `Parameters`
- `Arg` is now `Parameter`
- `ArgWithDefault` is now `ParameterWithDefault`

For now, the attribute names that reference `Parameters` directly are
changed (e.g., on `StmtFunctionDef`), but the attributes on `Parameters`
itself are not (e.g., `vararg`). We may revisit that decision in the
future.

For context, the AST node formerly known as `Arguments` is used in
function definitions. Formally (outside of the Python context),
"arguments" typically refers to "the values passed to a function", while
"parameters" typically refers to "the variables used in a function
definition". E.g., if you Google "arguments vs parameters", you'll get
some explanation like:

> A parameter is a variable in a function definition. It is a
placeholder and hence does not have a concrete value. An argument is a
value passed during function invocation.

We're thus deviating from Python's nomenclature in favor of a scheme
that we find to be more precise.
This commit is contained in:
Charlie Marsh 2023-08-01 13:53:28 -04:00 committed by GitHub
parent a82eb9544c
commit adc8bb7821
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
102 changed files with 2585 additions and 2529 deletions

View file

@ -3,9 +3,9 @@
pub mod preorder;
use crate::{
self as ast, Alias, Arg, Arguments, BoolOp, CmpOp, Comprehension, Decorator, ElifElseClause,
ExceptHandler, Expr, ExprContext, Keyword, MatchCase, Operator, Pattern, Stmt, TypeParam,
TypeParamTypeVar, UnaryOp, WithItem,
self as ast, Alias, 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.
@ -52,11 +52,11 @@ 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);
}
fn visit_arg(&mut self, arg: &'a Arg) {
walk_arg(self, arg);
fn visit_parameter(&mut self, parameter: &'a Parameter) {
walk_parameter(self, parameter);
}
fn visit_keyword(&mut self, keyword: &'a Keyword) {
walk_keyword(self, keyword);
@ -103,7 +103,7 @@ pub fn walk_elif_else_clause<'a, V: Visitor<'a> + ?Sized>(
pub fn walk_stmt<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, stmt: &'a Stmt) {
match stmt {
Stmt::FunctionDef(ast::StmtFunctionDef {
args,
parameters,
body,
decorator_list,
returns,
@ -116,14 +116,14 @@ 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);
}
visitor.visit_arguments(args);
visitor.visit_parameters(parameters);
for expr in returns {
visitor.visit_annotation(expr);
}
visitor.visit_body(body);
}
Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
args,
parameters,
body,
decorator_list,
returns,
@ -136,7 +136,7 @@ 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);
}
visitor.visit_arguments(args);
visitor.visit_parameters(parameters);
for expr in returns {
visitor.visit_annotation(expr);
}
@ -411,11 +411,11 @@ pub fn walk_expr<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, expr: &'a Expr) {
visitor.visit_expr(operand);
}
Expr::Lambda(ast::ExprLambda {
args,
parameters,
body,
range: _range,
}) => {
visitor.visit_arguments(args);
visitor.visit_parameters(parameters);
visitor.visit_expr(body);
}
Expr::IfExp(ast::ExprIfExp {
@ -645,43 +645,43 @@ 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) {
pub fn walk_parameters<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, parameters: &'a Parameters) {
// Defaults are evaluated before annotations.
for arg in &arguments.posonlyargs {
for arg in &parameters.posonlyargs {
if let Some(default) = &arg.default {
visitor.visit_expr(default);
}
}
for arg in &arguments.args {
for arg in &parameters.args {
if let Some(default) = &arg.default {
visitor.visit_expr(default);
}
}
for arg in &arguments.kwonlyargs {
for arg in &parameters.kwonlyargs {
if let Some(default) = &arg.default {
visitor.visit_expr(default);
}
}
for arg in &arguments.posonlyargs {
visitor.visit_arg(&arg.def);
for arg in &parameters.posonlyargs {
visitor.visit_parameter(&arg.def);
}
for arg in &arguments.args {
visitor.visit_arg(&arg.def);
for arg in &parameters.args {
visitor.visit_parameter(&arg.def);
}
if let Some(arg) = &arguments.vararg {
visitor.visit_arg(arg);
if let Some(arg) = &parameters.vararg {
visitor.visit_parameter(arg);
}
for arg in &arguments.kwonlyargs {
visitor.visit_arg(&arg.def);
for arg in &parameters.kwonlyargs {
visitor.visit_parameter(&arg.def);
}
if let Some(arg) = &arguments.kwarg {
visitor.visit_arg(arg);
if let Some(arg) = &parameters.kwarg {
visitor.visit_parameter(arg);
}
}
pub fn walk_arg<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, arg: &'a Arg) {
if let Some(expr) = &arg.annotation {
pub fn walk_parameter<'a, V: Visitor<'a> + ?Sized>(visitor: &mut V, parameter: &'a Parameter) {
if let Some(expr) = &parameter.annotation {
visitor.visit_annotation(expr);
}
}