Add support for PEP 696 syntax (#11120)

This commit is contained in:
Jelle Zijlstra 2024-04-26 00:47:29 -07:00 committed by GitHub
parent 45725d3275
commit cd3e319538
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
49 changed files with 4338 additions and 669 deletions

View file

@ -439,6 +439,9 @@ def function_with_one_argument_and_a_keyword_separator(
# https://peps.python.org/pep-0646/#change-2-args-as-a-typevartuple
def function_with_variadic_generics(*args: *tuple[int]): ...
def function_with_variadic_generics(*args: *tuple[int],): ...
# Generic arguments (PEP 695)
def func[T](lotsoflongargs: T, lotsoflongargs2: T, lotsoflongargs3: T, lotsoflongargs4: T, lotsoflongargs5: T) -> T: ...
```
## Output
@ -1028,4 +1031,14 @@ def function_with_variadic_generics(*args: *tuple[int]): ...
def function_with_variadic_generics(
*args: *tuple[int],
): ...
# Generic arguments (PEP 695)
def func[T](
lotsoflongargs: T,
lotsoflongargs2: T,
lotsoflongargs3: T,
lotsoflongargs4: T,
lotsoflongargs5: T,
) -> T: ...
```

View file

@ -5,13 +5,17 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/
## Input
```python
# basic usage
type X = int
type X = int | str
type X = int | "ForwardRefY"
type X[T] = T | list[X[T]] # recursive
type X[T] = int
type X[T] = list[T] | set[T]
type X[T=int]=int
type X[T:int=int]=int
type X[**P=int]=int
type X[*Ts=int]=int
type X[*Ts=*int]=int
type X[T, *Ts, **P] = (T, Ts, P)
type X[T: int, *Ts, **P] = (T, Ts, P)
type X[T: (int, str), *Ts, **P] = (T, Ts, P)
@ -55,6 +59,18 @@ type X \
[T] = T
type X[T] \
= T
type X[T
] = T
# bounds and defaults with multiline definitions
type X[T
:int ] = int
type X[T:
int] = int
type X[T
= int] = int
type X[T=
int] = int
# type leading comment
type X = ( # trailing open paren comment
@ -100,18 +116,62 @@ type bounds_single_line[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, cccc
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
# bounds plus comments
type comment_before_colon[T # comment
: int] = T
type comment_after_colon[T: # comment
int] = T
type comment_on_its_own_line[T
# comment
:
# another comment
int
# why not another
] = T
# type variable defaults
type defaults_single_line[T= (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc)] = T
type defaults_on_their_own_line[T= (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)] = T
type defaults_one_per_line[T= (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc, ddddddddddddd, eeeeeeeeeeeeeeee, ffffffffffff)] = T
type defaults_trailing_comma[T= (a, b,)] = T
# defaults plus comments
type comment_before_colon[T # comment
= int] = T
type comment_after_colon[T = # comment
int] = T
type comment_on_its_own_line[T
# comment
=
# another comment
int
# why not another
] = T
type after_star[*Ts = *
# comment
int] = int
# both bounds and defaults
type bound_and_default[T:int=int] = int
type long_bound_short_default[T: (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)=a]=int
type short_bound_long_default[T:a= (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccc, ddddddddddddd, eeeeeee)]=int
```
## Output
```python
# basic usage
type X = int
type X = int | str
type X = int | "ForwardRefY"
type X[T] = T | list[X[T]] # recursive
type X[T] = int
type X[T] = list[T] | set[T]
type X[T = int] = int
type X[T: int = int] = int
type X[**P = int] = int
type X[*Ts = int] = int
type X[*Ts = *int] = int
type X[T, *Ts, **P] = (T, Ts, P)
type X[T: int, *Ts, **P] = (T, Ts, P)
type X[T: (int, str), *Ts, **P] = (T, Ts, P)
@ -162,6 +222,13 @@ type X = int
type X[T] = T
type X[T] = T
type X[T] = T
type X[T] = T
# bounds and defaults with multiline definitions
type X[T: int] = int
type X[T: int] = int
type X[T = int] = int
type X[T = int] = int
# type leading comment
type X = ( # trailing open paren comment
@ -248,7 +315,91 @@ type bounds_trailing_comma[
b,
)
] = T
# bounds plus comments
type comment_before_colon[
T: # comment
int
] = T
type comment_after_colon[
T: # comment
int
] = T
type comment_on_its_own_line[
T: # comment
# another comment
int
# why not another
] = T
# type variable defaults
type defaults_single_line[
T = (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbb, ccccccccccccccccc)
] = T
type defaults_on_their_own_line[
T = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccc,
ddddddddddddd,
eeeeeee,
)
] = T
type defaults_one_per_line[
T = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccccccccc,
ddddddddddddd,
eeeeeeeeeeeeeeee,
ffffffffffff,
)
] = T
type defaults_trailing_comma[
T = (
a,
b,
)
] = T
# defaults plus comments
type comment_before_colon[
T = # comment
int
] = T
type comment_after_colon[
T = # comment
int
] = T
type comment_on_its_own_line[
T = # comment
# another comment
int
# why not another
] = T
type after_star[
*Ts = # comment
*int
] = int
# both bounds and defaults
type bound_and_default[T: int = int] = int
type long_bound_short_default[
T: (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccc,
ddddddddddddd,
eeeeeee,
) = a
] = int
type short_bound_long_default[
T: a = (
aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbb,
ccccccccccc,
ddddddddddddd,
eeeeeee,
)
] = int
```