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

@ -1,4 +1,4 @@
# Copied from https://github.com/RustPython/Parser/blob/704eb40108239a8faf9bd1d4217e8dad0ac7edb3/parser/src/parser.rs#L901-L936 # basic usage
type X = int type X = int
type X = int | str type X = int | str
@ -10,6 +10,17 @@ type X[T, *Ts, **P] = (T, Ts, P)
type X[T: int, *Ts, **P] = (T, Ts, P) type X[T: int, *Ts, **P] = (T, Ts, P)
type X[T: (int, str), *Ts, **P] = (T, Ts, P) type X[T: (int, str), *Ts, **P] = (T, Ts, P)
# long name
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[A] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[Aaaaaaaaaaaaaaaaaaaaaaaaaaaa] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[Aaaaaaaaaaaaaaaaaaaaaaaaaaaa, Bbbbbbbbbbbbb] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = Tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
# long value
type X = Ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
type X = Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | Ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
# soft keyword as alias name # soft keyword as alias name
type type = int type type = int
type match = int type match = int
@ -36,3 +47,44 @@ type X \
[T] = T [T] = T
type X[T] \ type X[T] \
= T = T
# type leading comment
type X = ( # trailing open paren comment
# value leading comment
int # value trailing comment
# leading close paren comment
) # type trailing comment
# type leading comment
type X = (
# value leading comment
int # value trailing comment
# leading close paren comment
)
# type parameters
type type_params_single_line[aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc] = int
type type_params_arguments_on_their_own_line[aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee] = int
type type_params_argument_per_line[aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, ffffffffffff] = int
type type_params_trailing_comma[a, b,] = int
type type_params_comments[ # trailing open bracket comment
# leading comment
A,
# in between comment
B,
# another leading comment
C,
D, # trailing comment
# leading close bracket comment
] = int # trailing value comment
type type_params_all_kinds[type_var, *type_var_tuple, **param_spec] = int
# type variable bounds
type bounds_single_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc)] = T
type bounds_arguments_on_their_own_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)] = T
type bounds_argument_per_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, ffffffffffff)] = T
type bounds_trailing_comma[T: (a, b,)] = T

View file

@ -188,6 +188,7 @@ impl Format<PyFormatContext<'_>> for NotYetImplemented {
pub(crate) struct NotYetImplementedCustomText(&'static str); pub(crate) struct NotYetImplementedCustomText(&'static str);
/// Formats a placeholder for nodes that have not yet been implemented /// Formats a placeholder for nodes that have not yet been implemented
#[allow(dead_code)]
pub(crate) const fn not_yet_implemented_custom_text( pub(crate) const fn not_yet_implemented_custom_text(
text: &'static str, text: &'static str,
) -> NotYetImplementedCustomText { ) -> 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_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::StmtTypeAlias; use ruff_python_ast::StmtTypeAlias;
@ -6,12 +10,28 @@ use ruff_python_ast::StmtTypeAlias;
pub struct FormatStmtTypeAlias; pub struct FormatStmtTypeAlias;
impl FormatNodeRule<StmtTypeAlias> for 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!( write!(
f, f,
[not_yet_implemented_custom_text( [
"type NOT_YET_IMPLEMENTED_type_alias = int" space(),
)] text("="),
space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
]
) )
} }
} }

View file

@ -1,50 +0,0 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/black/py_312/type_aliases.py
---
## Input
```py
type A=int
type Gen[T]=list[T]
type = aliased
print(type(42))
```
## Black Differences
```diff
--- Black
+++ Ruff
@@ -1,5 +1,5 @@
-type A = int
-type Gen[T] = list[T]
+type NOT_YET_IMPLEMENTED_type_alias = int
+type NOT_YET_IMPLEMENTED_type_alias = int
type = aliased
print(type(42))
```
## Ruff Output
```py
type NOT_YET_IMPLEMENTED_type_alias = int
type NOT_YET_IMPLEMENTED_type_alias = int
type = aliased
print(type(42))
```
## Black Output
```py
type A = int
type Gen[T] = list[T]
type = aliased
print(type(42))
```

View file

@ -4,7 +4,7 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/
--- ---
## Input ## Input
```py ```py
# Copied from https://github.com/RustPython/Parser/blob/704eb40108239a8faf9bd1d4217e8dad0ac7edb3/parser/src/parser.rs#L901-L936 # basic usage
type X = int type X = int
type X = int | str type X = int | str
@ -16,6 +16,17 @@ type X[T, *Ts, **P] = (T, Ts, P)
type X[T: int, *Ts, **P] = (T, Ts, P) type X[T: int, *Ts, **P] = (T, Ts, P)
type X[T: (int, str), *Ts, **P] = (T, Ts, P) type X[T: (int, str), *Ts, **P] = (T, Ts, P)
# long name
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[A] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[Aaaaaaaaaaaaaaaaaaaaaaaaaaaa] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[Aaaaaaaaaaaaaaaaaaaaaaaaaaaa, Bbbbbbbbbbbbb] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = Tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
# long value
type X = Ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
type X = Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb | Ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
# soft keyword as alias name # soft keyword as alias name
type type = int type type = int
type match = int type match = int
@ -42,40 +53,185 @@ type X \
[T] = T [T] = T
type X[T] \ type X[T] \
= T = T
# type leading comment
type X = ( # trailing open paren comment
# value leading comment
int # value trailing comment
# leading close paren comment
) # type trailing comment
# type leading comment
type X = (
# value leading comment
int # value trailing comment
# leading close paren comment
)
# type parameters
type type_params_single_line[aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc] = int
type type_params_arguments_on_their_own_line[aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee] = int
type type_params_argument_per_line[aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, ffffffffffff] = int
type type_params_trailing_comma[a, b,] = int
type type_params_comments[ # trailing open bracket comment
# leading comment
A,
# in between comment
B,
# another leading comment
C,
D, # trailing comment
# leading close bracket comment
] = int # trailing value comment
type type_params_all_kinds[type_var, *type_var_tuple, **param_spec] = int
# type variable bounds
type bounds_single_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc)] = T
type bounds_arguments_on_their_own_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)] = T
type bounds_argument_per_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, ffffffffffff)] = T
type bounds_trailing_comma[T: (a, b,)] = T
``` ```
## Output ## Output
```py ```py
# Copied from https://github.com/RustPython/Parser/blob/704eb40108239a8faf9bd1d4217e8dad0ac7edb3/parser/src/parser.rs#L901-L936 # basic usage
type NOT_YET_IMPLEMENTED_type_alias = int type X = int
type NOT_YET_IMPLEMENTED_type_alias = int type X = int | str
type NOT_YET_IMPLEMENTED_type_alias = int type X = int | "ForwardRefY"
type NOT_YET_IMPLEMENTED_type_alias = int # recursive type X[T] = T | list[X[T]] # recursive
type NOT_YET_IMPLEMENTED_type_alias = int type X[T] = int
type NOT_YET_IMPLEMENTED_type_alias = int type X[T] = list[T] | set[T]
type NOT_YET_IMPLEMENTED_type_alias = int type X[T, *Ts, **P] = (T, Ts, P)
type NOT_YET_IMPLEMENTED_type_alias = int type X[T: int, *Ts, **P] = (T, Ts, P)
type NOT_YET_IMPLEMENTED_type_alias = int type X[T: (int, str), *Ts, **P] = (T, Ts, P)
# long name
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[
A
] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[
Aaaaaaaaaaaaaaaaaaaaaaaaaaaa
] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[
Aaaaaaaaaaaaaaaaaaaaaaaaaaaa,
Bbbbbbbbbbbbb,
] = int
type Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = Tttttttttttttttttttttttttttttttttttttttttttttttttttttttt
# long value
type X = Ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
type X = (
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
| Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
| Ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
)
# soft keyword as alias name # soft keyword as alias name
type NOT_YET_IMPLEMENTED_type_alias = int type type = int
type NOT_YET_IMPLEMENTED_type_alias = int type match = int
type NOT_YET_IMPLEMENTED_type_alias = int type case = int
# soft keyword as value # soft keyword as value
type NOT_YET_IMPLEMENTED_type_alias = int type foo = type
type NOT_YET_IMPLEMENTED_type_alias = int type foo = match
type NOT_YET_IMPLEMENTED_type_alias = int type foo = case
# multine definitions # multine definitions
type NOT_YET_IMPLEMENTED_type_alias = int type X = int
type NOT_YET_IMPLEMENTED_type_alias = int type X = int
type NOT_YET_IMPLEMENTED_type_alias = int type X = int
type NOT_YET_IMPLEMENTED_type_alias = int type X = int
type NOT_YET_IMPLEMENTED_type_alias = int type X[T] = T
type NOT_YET_IMPLEMENTED_type_alias = int type X[T] = T
type NOT_YET_IMPLEMENTED_type_alias = int type X[T] = T
# type leading comment
type X = ( # trailing open paren comment
# value leading comment
int # value trailing comment
# leading close paren comment
) # type trailing comment
# type leading comment
type X = (
# value leading comment
int # value trailing comment
# leading close paren comment
)
# type parameters
type type_params_single_line[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccccccccc,
] = int
type type_params_arguments_on_their_own_line[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccc,
ddddddddddddd,
eeeeeee,
] = int
type type_params_argument_per_line[
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccccccccc,
ddddddddddddd,
eeeeeeeeeeeeeeee,
ffffffffffff,
] = int
type type_params_trailing_comma[
a,
b,
] = int
type type_params_comments[ # trailing open bracket comment
# leading comment
A,
# in between comment
B,
# another leading comment
C,
D, # trailing comment
# leading close bracket comment
] = int # trailing value comment
type type_params_all_kinds[type_var, *type_var_tuple, **param_spec] = int
# type variable bounds
type bounds_single_line[
T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc)
] = T
type bounds_arguments_on_their_own_line[
T: (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccc,
ddddddddddddd,
eeeeeee,
)
] = T
type bounds_argument_per_line[
T: (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccccccccc,
ddddddddddddd,
eeeeeeeeeeeeeeee,
ffffffffffff,
)
] = T
type bounds_trailing_comma[
T: (
a,
b,
)
] = T
``` ```