Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping lines of a function signature that uses the new python 3.12 generic type parameter syntax #4071

Open
Questionsdenombres opened this issue Nov 26, 2023 · 9 comments
Labels
F: linebreak How should we split up lines? S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?

Comments

@Questionsdenombres
Copy link

Context:

def func(a: int, b: int) -> int:
    return a + b 

def func(
    a: int,
    b: int,
) -> int:
    return a + b

python 3.12 new feature:

def func[T](a: T, b: T) -> T:
    return a

Example in the current Black style:
Enforcing black to wrap lines gives the ugly:

def func[
    T
](a: T, b: T,) -> T:
    return a

Desired style:

def func[T](
    a: T,
    b: T,
) -> T:
    return a + b

Additional context
python==3.12
black==23.11.0
Windows==11
VScode==1.84.2
VScode extension! Black Formatter==v2023.6.0

Credits:
Thank you for your great tool.
Patrick, Paris, France.

@Questionsdenombres Questionsdenombres added the T: style What do we want Blackened code to look like? label Nov 26, 2023
@JelleZijlstra
Copy link
Collaborator

Yes, we should change this. PR welcome!

@JackismyShephard
Copy link

What is the ETA on this?

@hauntsaninja
Copy link
Collaborator

Are you interested in making a PR?

@JackismyShephard
Copy link

Well, sadly I don't quite have the time for that right now.

@AndreaCensi
Copy link

@JackismyShephard if you need to undo what black did, try ruff: it will do exactly what you want

@FlorinAsavoaie
Copy link

FYI: An attempt to fix this is found in #4272 but doesn't seem trivial.

@Feuermurmel
Copy link

Feuermurmel commented Jan 6, 2025

I just converted a large-ish code-base to the new syntax and found an even more egregious example. 😅 I thought that this example could maybe serve as an additional test case where the return type is also wrapped:

def something_something_function[
    T: Model
](param: list[int], other_param: type[T], *, some_other_param: bool = True) -> QuerySet[
    T
]:
    pass

(playground)

Where I would prefer this instead. :)

def something_something_function[T: Model](
    param: list[int], other_param: type[T], *, some_other_param: bool = True
) -> QuerySet[T]:
	pass

@Pedro-Muller29
Copy link

Context

I am currently working on a PR for this, and I am facing some doubts on what should be the valid syntax on some trailing comma cases:

def trailing_comma1[T=int,](a: str):
    pass

def trailing_comma2[T=int](a: str,):
    pass

Current Behaviour

Right now the expected output in the type_param_defaults.py test case gives:

def trailing_comma1[
    T = int,
](a: str):
    pass


def trailing_comma2[
    T = int
](a: str,):
    pass

My Assessment

I believe the following adjustments should be made to align with valid and consistent formatting:

  1. trailing_comma1 should keep the same output.
  2. trailing_comma2 output should change to:
def trailing_comma2[T = int](
    a: str,
):
    pass

Am I correct in my assessment?

Pedro-Muller29 added a commit to Pedro-Muller29/black that referenced this issue Jan 21, 2025
@hauntsaninja
Copy link
Collaborator

Yes, that sounds right (assuming default settings, not --skip-magic-trailing-comma)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

8 participants