A basic StmtAssign formatter and better dummies for expressions (#4938)

* A basic StmtAssign formatter and better dummies for expressions

The goal of this PR was formatting StmtAssign since many nodes in the black tests (and in python in general) are after an assignment. This caused unstable formatting: The spacing of power op spacing depends on the type of the two involved expressions, but each expression was formatted as dummy string and re-parsed as a ExprName, so in the second round the different rules of ExprName were applied, causing unstable formatting.

This PR does not necessarily bring us closer to black's style, but it unlocks a good porting of black's test suite and is a basis for implementing the Expr nodes.

* fmt

* Review
This commit is contained in:
konstin 2023-06-08 12:20:25 +02:00 committed by GitHub
parent 651d89794c
commit 23abad0bd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
62 changed files with 1291 additions and 1023 deletions

View file

@ -1,7 +1,8 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::text;
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprAttribute;
@ -10,7 +11,16 @@ pub struct FormatExprAttribute;
impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
fn fmt_fields(&self, item: &ExprAttribute, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
// We need to write the value - which is also a dummy - already because power op spacing
// depends on it
write!(
f,
[
item.value.format(),
text("."),
not_yet_implemented_custom_text(item, "NOT_IMPLEMENTED_attr")
]
)
}
}

View file

@ -1,7 +1,8 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprAwait;
@ -10,7 +11,8 @@ pub struct FormatExprAwait;
impl FormatNodeRule<ExprAwait> for FormatExprAwait {
fn fmt_fields(&self, item: &ExprAwait, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let ExprAwait { range: _, value } = item;
write!(f, [text("await"), space(), value.format()])
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprBoolOp;
@ -10,7 +10,13 @@ pub struct FormatExprBoolOp;
impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
fn fmt_fields(&self, item: &ExprBoolOp, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2"
)]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprCall;
@ -10,7 +10,13 @@ pub struct FormatExprCall;
impl FormatNodeRule<ExprCall> for FormatExprCall {
fn fmt_fields(&self, item: &ExprCall, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"NOT_IMPLEMENTED_call()"
)]
)
}
}

View file

@ -1,7 +1,8 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprCompare;
@ -10,7 +11,13 @@ pub struct FormatExprCompare;
impl FormatNodeRule<ExprCompare> for FormatExprCompare {
fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right"
)]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprConstant;
@ -10,7 +10,7 @@ pub struct FormatExprConstant;
impl FormatNodeRule<ExprConstant> for FormatExprConstant {
fn fmt_fields(&self, item: &ExprConstant, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(f, [not_yet_implemented_custom_text(item, "0x42")])
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprDict;
@ -10,7 +10,13 @@ pub struct FormatExprDict;
impl FormatNodeRule<ExprDict> for FormatExprDict {
fn fmt_fields(&self, item: &ExprDict, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value}"
)]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprDictComp;
@ -10,7 +10,13 @@ pub struct FormatExprDictComp;
impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
fn fmt_fields(&self, item: &ExprDictComp, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}"
)]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprGeneratorExp;
@ -10,7 +10,10 @@ pub struct FormatExprGeneratorExp;
impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
fn fmt_fields(&self, item: &ExprGeneratorExp, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(item, "(i for i in [])")]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprIfExp;
@ -10,7 +10,13 @@ pub struct FormatExprIfExp;
impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
fn fmt_fields(&self, item: &ExprIfExp, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"NOT_IMPLEMENTED_true if NOT_IMPLEMENTED_cond else NOT_IMPLEMENTED_false"
)]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprLambda;
@ -10,7 +10,7 @@ pub struct FormatExprLambda;
impl FormatNodeRule<ExprLambda> for FormatExprLambda {
fn fmt_fields(&self, item: &ExprLambda, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(f, [not_yet_implemented_custom_text(item, "lambda x: True")])
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprListComp;
@ -10,7 +10,10 @@ pub struct FormatExprListComp;
impl FormatNodeRule<ExprListComp> for FormatExprListComp {
fn fmt_fields(&self, item: &ExprListComp, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(item, "[i for i in []]")]
)
}
}

View file

@ -1,8 +1,11 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::{FormatNodeRule, FormattedIterExt, PyFormatter};
use ruff_formatter::prelude::{
format_with, group, if_group_breaks, soft_block_indent, soft_line_break_or_space, text,
};
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use rustpython_parser::ast::ExprSet;
#[derive(Default)]
@ -10,7 +13,23 @@ pub struct FormatExprSet;
impl FormatNodeRule<ExprSet> for FormatExprSet {
fn fmt_fields(&self, item: &ExprSet, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let ExprSet { range: _, elts } = item;
// That would be a dict expression
assert!(!elts.is_empty());
// Avoid second mutable borrow of f
let joined = format_with(|f| {
f.join_with(format_args!(text(","), soft_line_break_or_space()))
.entries(elts.iter().formatted())
.finish()
});
write!(
f,
[group(&format_args![
text("{"),
soft_block_indent(&format_args![joined, if_group_breaks(&text(",")),]),
text("}")
])]
)
}
}

View file

@ -1,7 +1,8 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprSlice;
@ -10,7 +11,13 @@ pub struct FormatExprSlice;
impl FormatNodeRule<ExprSlice> for FormatExprSlice {
fn fmt_fields(&self, item: &ExprSlice, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"NOT_IMPLEMENTED_start:NOT_IMPLEMENTED_end"
)]
)
}
}

View file

@ -1,7 +1,8 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprSubscript;
@ -10,7 +11,13 @@ pub struct FormatExprSubscript;
impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
fn fmt_fields(&self, item: &ExprSubscript, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(
f,
[not_yet_implemented_custom_text(
item,
"NOT_IMPLEMENTED_value[NOT_IMPLEMENTED_key]"
)]
)
}
}

View file

@ -1,7 +1,7 @@
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprTuple;
@ -10,7 +10,7 @@ pub struct FormatExprTuple;
impl FormatNodeRule<ExprTuple> for FormatExprTuple {
fn fmt_fields(&self, item: &ExprTuple, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
write!(f, [not_yet_implemented_custom_text(item, "(1, 2)")])
}
}