Add formatting of type alias statements (#6162)

Part of #5062 
Extends https://github.com/astral-sh/ruff/pull/6161
Closes #5929
This commit is contained in:
Zanie Blue 2023-08-02 15:40:32 -05:00 committed by GitHub
parent 1a60d1e3c6
commit 5b2e973fa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 259 additions and 80 deletions

View file

@ -188,6 +188,7 @@ impl Format<PyFormatContext<'_>> for NotYetImplemented {
pub(crate) struct NotYetImplementedCustomText(&'static str);
/// Formats a placeholder for nodes that have not yet been implemented
#[allow(dead_code)]
pub(crate) const fn not_yet_implemented_custom_text(
text: &'static str,
) -> NotYetImplementedCustomText {

View file

@ -1,4 +1,8 @@
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::StmtTypeAlias;
@ -6,12 +10,28 @@ use ruff_python_ast::StmtTypeAlias;
pub struct FormatStmtTypeAlias;
impl FormatNodeRule<StmtTypeAlias> for FormatStmtTypeAlias {
fn fmt_fields(&self, _item: &StmtTypeAlias, f: &mut PyFormatter) -> FormatResult<()> {
fn fmt_fields(&self, item: &StmtTypeAlias, f: &mut PyFormatter) -> FormatResult<()> {
let StmtTypeAlias {
name,
type_params,
value,
range: _,
} = item;
write!(f, [text("type"), space(), name.as_ref().format()])?;
if let Some(type_params) = type_params {
write!(f, [type_params.format()])?;
}
write!(
f,
[not_yet_implemented_custom_text(
"type NOT_YET_IMPLEMENTED_type_alias = int"
)]
[
space(),
text("="),
space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
]
)
}
}