Remove Expr postfix from ExprNamed, ExprIf, and ExprGenerator (#10229)

The expression types in our AST are called `ExprYield`, `ExprAwait`,
`ExprStringLiteral` etc, except `ExprNamedExpr`, `ExprIfExpr` and
`ExprGenratorExpr`. This seems to align with [Python AST's
naming](https://docs.python.org/3/library/ast.html) but feels
inconsistent and excessive.

This PR removes the `Expr` postfix from `ExprNamedExpr`, `ExprIfExpr`,
and `ExprGeneratorExpr`.
This commit is contained in:
Micha Reiser 2024-03-04 12:55:01 +01:00 committed by GitHub
parent 8b749e1d4d
commit 184241f99a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
64 changed files with 418 additions and 428 deletions

View file

@ -1,6 +1,6 @@
use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprGeneratorExp;
use ruff_python_ast::ExprGenerator;
use crate::comments::SourceComment;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
@ -20,7 +20,7 @@ pub enum GeneratorExpParentheses {
Preserve,
}
impl FormatRuleWithOptions<ExprGeneratorExp, PyFormatContext<'_>> for FormatExprGeneratorExp {
impl FormatRuleWithOptions<ExprGenerator, PyFormatContext<'_>> for FormatExprGenerator {
type Options = GeneratorExpParentheses;
fn with_options(mut self, options: Self::Options) -> Self {
@ -30,13 +30,13 @@ impl FormatRuleWithOptions<ExprGeneratorExp, PyFormatContext<'_>> for FormatExpr
}
#[derive(Default)]
pub struct FormatExprGeneratorExp {
pub struct FormatExprGenerator {
parentheses: GeneratorExpParentheses,
}
impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
fn fmt_fields(&self, item: &ExprGeneratorExp, f: &mut PyFormatter) -> FormatResult<()> {
let ExprGeneratorExp {
impl FormatNodeRule<ExprGenerator> for FormatExprGenerator {
fn fmt_fields(&self, item: &ExprGenerator, f: &mut PyFormatter) -> FormatResult<()> {
let ExprGenerator {
range: _,
elt,
generators,
@ -87,7 +87,7 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
}
}
impl NeedsParentheses for ExprGeneratorExp {
impl NeedsParentheses for ExprGenerator {
fn needs_parentheses(
&self,
parent: AnyNodeRef,

View file

@ -1,6 +1,6 @@
use ruff_formatter::{write, FormatRuleWithOptions};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{Expr, ExprIfExp};
use ruff_python_ast::{Expr, ExprIf};
use crate::comments::leading_comments;
use crate::expression::parentheses::{
@ -10,11 +10,11 @@ use crate::expression::parentheses::{
use crate::prelude::*;
#[derive(Default, Copy, Clone)]
pub enum ExprIfExpLayout {
pub enum ExprIfLayout {
#[default]
Default,
/// The [`ExprIfExp`] is nested inside another [`ExprIfExp`], so it should not be given a new
/// The [`ExprIf`] is nested inside another [`ExprIf`], so it should not be given a new
/// group. For example, avoid grouping the `else` clause in:
/// ```python
/// clone._iterable_class = (
@ -29,12 +29,12 @@ pub enum ExprIfExpLayout {
}
#[derive(Default)]
pub struct FormatExprIfExp {
layout: ExprIfExpLayout,
pub struct FormatExprIf {
layout: ExprIfLayout,
}
impl FormatRuleWithOptions<ExprIfExp, PyFormatContext<'_>> for FormatExprIfExp {
type Options = ExprIfExpLayout;
impl FormatRuleWithOptions<ExprIf, PyFormatContext<'_>> for FormatExprIf {
type Options = ExprIfLayout;
fn with_options(mut self, options: Self::Options) -> Self {
self.layout = options;
@ -42,9 +42,9 @@ impl FormatRuleWithOptions<ExprIfExp, PyFormatContext<'_>> for FormatExprIfExp {
}
}
impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
fn fmt_fields(&self, item: &ExprIfExp, f: &mut PyFormatter) -> FormatResult<()> {
let ExprIfExp {
impl FormatNodeRule<ExprIf> for FormatExprIf {
fn fmt_fields(&self, item: &ExprIf, f: &mut PyFormatter) -> FormatResult<()> {
let ExprIf {
range: _,
test,
body,
@ -76,13 +76,13 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
});
match self.layout {
ExprIfExpLayout::Default => in_parentheses_only_group(&inner).fmt(f),
ExprIfExpLayout::Nested => inner.fmt(f),
ExprIfLayout::Default => in_parentheses_only_group(&inner).fmt(f),
ExprIfLayout::Nested => inner.fmt(f),
}
}
}
impl NeedsParentheses for ExprIfExp {
impl NeedsParentheses for ExprIf {
fn needs_parentheses(
&self,
parent: AnyNodeRef,
@ -104,14 +104,14 @@ struct FormatOrElse<'a> {
impl Format<PyFormatContext<'_>> for FormatOrElse<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
match self.orelse {
Expr::IfExp(expr)
Expr::If(expr)
if !is_expression_parenthesized(
expr.into(),
f.context().comments().ranges(),
f.context().source(),
) =>
{
write!(f, [expr.format().with_options(ExprIfExpLayout::Nested)])
write!(f, [expr.format().with_options(ExprIfLayout::Nested)])
}
_ => write!(f, [in_parentheses_only_group(&self.orelse.format())]),
}

View file

@ -1,6 +1,6 @@
use ruff_formatter::{format_args, write};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprNamedExpr;
use ruff_python_ast::ExprNamed;
use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::{
@ -9,11 +9,11 @@ use crate::expression::parentheses::{
use crate::prelude::*;
#[derive(Default)]
pub struct FormatExprNamedExpr;
pub struct FormatExprNamed;
impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
fn fmt_fields(&self, item: &ExprNamedExpr, f: &mut PyFormatter) -> FormatResult<()> {
let ExprNamedExpr {
impl FormatNodeRule<ExprNamed> for FormatExprNamed {
fn fmt_fields(&self, item: &ExprNamed, f: &mut PyFormatter) -> FormatResult<()> {
let ExprNamed {
target,
value,
range: _,
@ -53,7 +53,7 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
}
}
impl NeedsParentheses for ExprNamedExpr {
impl NeedsParentheses for ExprNamed {
fn needs_parentheses(
&self,
parent: AnyNodeRef,

View file

@ -34,14 +34,14 @@ pub(crate) mod expr_dict;
pub(crate) mod expr_dict_comp;
pub(crate) mod expr_ellipsis_literal;
pub(crate) mod expr_f_string;
pub(crate) mod expr_generator_exp;
pub(crate) mod expr_if_exp;
pub(crate) mod expr_generator;
pub(crate) mod expr_if;
pub(crate) mod expr_ipy_escape_command;
pub(crate) mod expr_lambda;
pub(crate) mod expr_list;
pub(crate) mod expr_list_comp;
pub(crate) mod expr_name;
pub(crate) mod expr_named_expr;
pub(crate) mod expr_named;
pub(crate) mod expr_none_literal;
pub(crate) mod expr_number_literal;
pub(crate) mod expr_set;
@ -77,17 +77,17 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
let format_expr = format_with(|f| match expression {
Expr::BoolOp(expr) => expr.format().fmt(f),
Expr::NamedExpr(expr) => expr.format().fmt(f),
Expr::Named(expr) => expr.format().fmt(f),
Expr::BinOp(expr) => expr.format().fmt(f),
Expr::UnaryOp(expr) => expr.format().fmt(f),
Expr::Lambda(expr) => expr.format().fmt(f),
Expr::IfExp(expr) => expr.format().fmt(f),
Expr::If(expr) => expr.format().fmt(f),
Expr::Dict(expr) => expr.format().fmt(f),
Expr::Set(expr) => expr.format().fmt(f),
Expr::ListComp(expr) => expr.format().fmt(f),
Expr::SetComp(expr) => expr.format().fmt(f),
Expr::DictComp(expr) => expr.format().fmt(f),
Expr::GeneratorExp(expr) => expr.format().fmt(f),
Expr::Generator(expr) => expr.format().fmt(f),
Expr::Await(expr) => expr.format().fmt(f),
Expr::Yield(expr) => expr.format().fmt(f),
Expr::YieldFrom(expr) => expr.format().fmt(f),
@ -265,17 +265,17 @@ fn format_with_parentheses_comments(
let fmt_fields = format_with(|f| match expression {
Expr::BoolOp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::NamedExpr(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Named(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::BinOp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::UnaryOp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Lambda(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::IfExp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::If(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Dict(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Set(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::ListComp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::SetComp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::DictComp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::GeneratorExp(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Generator(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Await(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::Yield(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
Expr::YieldFrom(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
@ -461,17 +461,17 @@ impl NeedsParentheses for Expr {
) -> OptionalParentheses {
match self {
Expr::BoolOp(expr) => expr.needs_parentheses(parent, context),
Expr::NamedExpr(expr) => expr.needs_parentheses(parent, context),
Expr::Named(expr) => expr.needs_parentheses(parent, context),
Expr::BinOp(expr) => expr.needs_parentheses(parent, context),
Expr::UnaryOp(expr) => expr.needs_parentheses(parent, context),
Expr::Lambda(expr) => expr.needs_parentheses(parent, context),
Expr::IfExp(expr) => expr.needs_parentheses(parent, context),
Expr::If(expr) => expr.needs_parentheses(parent, context),
Expr::Dict(expr) => expr.needs_parentheses(parent, context),
Expr::Set(expr) => expr.needs_parentheses(parent, context),
Expr::ListComp(expr) => expr.needs_parentheses(parent, context),
Expr::SetComp(expr) => expr.needs_parentheses(parent, context),
Expr::DictComp(expr) => expr.needs_parentheses(parent, context),
Expr::GeneratorExp(expr) => expr.needs_parentheses(parent, context),
Expr::Generator(expr) => expr.needs_parentheses(parent, context),
Expr::Await(expr) => expr.needs_parentheses(parent, context),
Expr::Yield(expr) => expr.needs_parentheses(parent, context),
Expr::YieldFrom(expr) => expr.needs_parentheses(parent, context),
@ -669,7 +669,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
return;
}
Expr::GeneratorExp(generator) if generator.parenthesized => {
Expr::Generator(generator) if generator.parenthesized => {
self.any_parenthesized_expressions = true;
// The values are always parenthesized, don't visit.
return;
@ -693,7 +693,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
range: _,
}) => self.update_max_precedence(OperatorPrecedence::from(*op)),
Expr::IfExp(_) => {
Expr::If(_) => {
// + 1 for the if and one for the else
self.update_max_precedence_with_count(OperatorPrecedence::Conditional, 2);
}
@ -763,7 +763,7 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
}
// Non terminal nodes that don't have a termination token.
Expr::NamedExpr(_) | Expr::GeneratorExp(_) | Expr::Tuple(_) => {}
Expr::Named(_) | Expr::Generator(_) | Expr::Tuple(_) => {}
// Expressions with sub expressions but a preceding token
// Mark this expression as first expression and not the sub expression.
@ -1035,7 +1035,7 @@ pub(crate) fn has_own_parentheses(
Some(OwnParentheses::NonEmpty)
}
Expr::GeneratorExp(generator) if generator.parenthesized => Some(OwnParentheses::NonEmpty),
Expr::Generator(generator) if generator.parenthesized => Some(OwnParentheses::NonEmpty),
// These expressions must contain _some_ child or trivia token in order to be non-empty.
Expr::List(ast::ExprList { elts, .. }) | Expr::Set(ast::ExprSet { elts, .. }) => {
@ -1118,12 +1118,12 @@ pub(crate) fn is_expression_huggable(expr: &Expr, context: &PyFormatContext) ->
Expr::Starred(ast::ExprStarred { value, .. }) => is_expression_huggable(value, context),
Expr::BoolOp(_)
| Expr::NamedExpr(_)
| Expr::Named(_)
| Expr::BinOp(_)
| Expr::UnaryOp(_)
| Expr::Lambda(_)
| Expr::IfExp(_)
| Expr::GeneratorExp(_)
| Expr::If(_)
| Expr::Generator(_)
| Expr::Await(_)
| Expr::Yield(_)
| Expr::YieldFrom(_)
@ -1193,7 +1193,7 @@ impl From<Operator> for OperatorPrecedence {
pub(crate) fn is_splittable_expression(expr: &Expr, context: &PyFormatContext) -> bool {
match expr {
// Single token expressions. They never have any split points.
Expr::NamedExpr(_)
Expr::Named(_)
| Expr::Name(_)
| Expr::NumberLiteral(_)
| Expr::BooleanLiteral(_)
@ -1206,8 +1206,8 @@ pub(crate) fn is_splittable_expression(expr: &Expr, context: &PyFormatContext) -
Expr::Compare(_)
| Expr::BinOp(_)
| Expr::BoolOp(_)
| Expr::IfExp(_)
| Expr::GeneratorExp(_)
| Expr::If(_)
| Expr::Generator(_)
| Expr::Subscript(_)
| Expr::Await(_)
| Expr::ListComp(_)