mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 16:40:19 +00:00
Split Constant
to individual literal nodes (#8064)
## Summary This PR splits the `Constant` enum as individual literal nodes. It introduces the following new nodes for each variant: * `ExprStringLiteral` * `ExprBytesLiteral` * `ExprNumberLiteral` * `ExprBooleanLiteral` * `ExprNoneLiteral` * `ExprEllipsisLiteral` The main motivation behind this refactor is to introduce the new AST node for implicit string concatenation in the coming PR. The elements of that node will be either a string literal, bytes literal or a f-string which can be implemented using an enum. This means that a string or bytes literal cannot be represented by `Constant::Str` / `Constant::Bytes` which creates an inconsistency. This PR avoids that inconsistency by splitting the constant nodes into it's own literal nodes, literal being the more appropriate naming convention from a static analysis tool perspective. This also makes working with literals in the linter and formatter much more ergonomic like, for example, if one would want to check if this is a string literal, it can be done easily using `Expr::is_string_literal_expr` or matching against `Expr::StringLiteral` as oppose to matching against the `ExprConstant` and enum `Constant`. A few AST helper methods can be simplified as well which will be done in a follow-up PR. This introduces a new `Expr::is_literal_expr` method which is the same as `Expr::is_constant_expr`. There are also intermediary changes related to implicit string concatenation which are quiet less. This is done so as to avoid having a huge PR which this already is. ## Test Plan 1. Verify and update all of the existing snapshots (parser, visitor) 2. Verify that the ecosystem check output remains **unchanged** for both the linter and formatter ### Formatter ecosystem check #### `main` | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75803 | 1799 | 1647 | | django | 0.99983 | 2772 | 34 | | home-assistant | 0.99953 | 10596 | 186 | | poetry | 0.99891 | 317 | 17 | | transformers | 0.99966 | 2657 | 330 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99978 | 3669 | 20 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 22 | #### `dhruv/constant-to-literal` | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75803 | 1799 | 1647 | | django | 0.99983 | 2772 | 34 | | home-assistant | 0.99953 | 10596 | 186 | | poetry | 0.99891 | 317 | 17 | | transformers | 0.99966 | 2657 | 330 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99978 | 3669 | 20 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 22 |
This commit is contained in:
parent
78bbf6d403
commit
230c9ce236
268 changed files with 6663 additions and 6741 deletions
|
@ -7,7 +7,7 @@ use ruff_formatter::{
|
|||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::parenthesize::parentheses_iterator;
|
||||
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
|
||||
use ruff_python_ast::{AnyNodeRef, Constant, Expr, ExpressionRef, Operator};
|
||||
use ruff_python_ast::{AnyNodeRef, Expr, ExpressionRef, Operator};
|
||||
use ruff_python_trivia::CommentRanges;
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
|
@ -27,11 +27,13 @@ pub(crate) mod expr_attribute;
|
|||
pub(crate) mod expr_await;
|
||||
pub(crate) mod expr_bin_op;
|
||||
pub(crate) mod expr_bool_op;
|
||||
pub(crate) mod expr_boolean_literal;
|
||||
pub(crate) mod expr_bytes_literal;
|
||||
pub(crate) mod expr_call;
|
||||
pub(crate) mod expr_compare;
|
||||
pub(crate) mod expr_constant;
|
||||
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_formatted_value;
|
||||
pub(crate) mod expr_generator_exp;
|
||||
|
@ -42,10 +44,13 @@ 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_none_literal;
|
||||
pub(crate) mod expr_number_literal;
|
||||
pub(crate) mod expr_set;
|
||||
pub(crate) mod expr_set_comp;
|
||||
pub(crate) mod expr_slice;
|
||||
pub(crate) mod expr_starred;
|
||||
pub(crate) mod expr_string_literal;
|
||||
pub(crate) mod expr_subscript;
|
||||
pub(crate) mod expr_tuple;
|
||||
pub(crate) mod expr_unary_op;
|
||||
|
@ -94,7 +99,12 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
|||
Expr::Call(expr) => expr.format().fmt(f),
|
||||
Expr::FormattedValue(expr) => expr.format().fmt(f),
|
||||
Expr::FString(expr) => expr.format().fmt(f),
|
||||
Expr::Constant(expr) => expr.format().fmt(f),
|
||||
Expr::StringLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::BytesLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::NumberLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::BooleanLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::NoneLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::EllipsisLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::Attribute(expr) => expr.format().fmt(f),
|
||||
Expr::Subscript(expr) => expr.format().fmt(f),
|
||||
Expr::Starred(expr) => expr.format().fmt(f),
|
||||
|
@ -274,7 +284,12 @@ fn format_with_parentheses_comments(
|
|||
Expr::Call(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::FormattedValue(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::FString(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Constant(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::StringLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::BytesLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::NumberLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::BooleanLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::NoneLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::EllipsisLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Attribute(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Subscript(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Starred(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
|
@ -468,7 +483,12 @@ impl NeedsParentheses for Expr {
|
|||
Expr::Call(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::FormattedValue(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::FString(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Constant(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::StringLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::BytesLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::NumberLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::BooleanLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::NoneLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::EllipsisLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Attribute(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Subscript(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Starred(expr) => expr.needs_parentheses(parent, context),
|
||||
|
@ -692,16 +712,12 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
|
|||
return;
|
||||
}
|
||||
|
||||
Expr::Constant(ast::ExprConstant {
|
||||
value:
|
||||
Constant::Str(ast::StringConstant {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
})
|
||||
| Constant::Bytes(ast::BytesConstant {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
}),
|
||||
Expr::StringLiteral(ast::ExprStringLiteral {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
})
|
||||
| Expr::BytesLiteral(ast::ExprBytesLiteral {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
})
|
||||
| Expr::FString(ast::ExprFString {
|
||||
|
@ -726,7 +742,12 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
|
|||
| Expr::GeneratorExp(_)
|
||||
| Expr::FormattedValue(_)
|
||||
| Expr::FString(_)
|
||||
| Expr::Constant(_)
|
||||
| Expr::StringLiteral(_)
|
||||
| Expr::BytesLiteral(_)
|
||||
| Expr::NumberLiteral(_)
|
||||
| Expr::BooleanLiteral(_)
|
||||
| Expr::NoneLiteral(_)
|
||||
| Expr::EllipsisLiteral(_)
|
||||
| Expr::Name(_)
|
||||
| Expr::Slice(_)
|
||||
| Expr::IpyEscapeCommand(_) => {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue