psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
39.12k stars 2.47k forks source link

Line wrapping on generic functions using the type parameter syntax #3929

Open PaulWassermann opened 1 year ago

PaulWassermann commented 1 year ago

Describe the style change

The suggested style change regards the type parameter syntax introduced in Python 3.12.

When formatting lines too long, I think line-wrapping should occur in the arguments' parenthesis rather than in the type parameters' brackets.

Examples in the current Black style

This would work with any generic function using the new type parameter syntax with a long enough signature (longer than the line-length).


type SupportedDataTypes = float
type Tensor = list

def sum_backward[
T: SupportedDataTypes
](a: Tensor[T], grad: Tensor[T]) -> dict[Tensor[T], Tensor[T]]:
    ...

Desired style

I think this kind of line-wrapping, keeping the type parameters list on a single line when possible, should be preferred over what currently happens as it is more readable.


type SupportedDataTypes = float
type Tensor = list

def sum_backward[T: SupportedDataTypes](
    a: Tensor[T], 
    grad: Tensor[T]
) -> dict[Tensor[T], Tensor[T]]:
    ...
arre-ankit commented 1 year ago

In which section we have to implement this syntax can you give more context about the files where we have to change the syntax ?

kanugurajesh commented 1 year ago

hi can you assign me this issue?

JelleZijlstra commented 1 year ago

We don't generally assign issues. However, PRs are welcome!