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

Trailing commas in function params cause line splits in type params instead #4254

Open
sumezulike opened this issue Feb 27, 2024 · 0 comments
Labels
T: bug Something isn't working

Comments

@sumezulike
Copy link
Contributor

Describe the bug

A function with type parameters gets formatted incorrectly if the function parameters have a magic trailing comma.

To Reproduce

For example, take this code:

def func1[T](a,): pass
def func3[T,](a,): pass
def func3[T,](a,) -> R[X, Y,]: pass

And run it with these arguments:

$ black file.py --target-version py312

The resulting code is:

def func1[
    T
](a,):
    pass


def func3[
    T,
](a,):
    pass


def func3[
    T,
](a,) -> R[
    X,
    Y,
]:
    pass

The type parameters are split instead of the function parameters.

Expected behavior

I would expect this result.

def func1[T](
    a,
):
    pass


def func2[
    T,
](
    a,
):
    pass


def func3[
    T,
](
    a,
) -> R[
    X,
    Y,
]:
    pass

Environment

$ black --version
black, 24.2.0 (compiled: yes)
Python (CPython) 3.12.1

Additional context

I am currently drafting a PR to fix this bug.
My current approach is to rework should_split_funcdef_with_rhs to also check for the position of the trailing comma in type and function parameters.
If I am overlooking something with that approach or if you have a better idea on how to fix this, please feel free to let me know.

@sumezulike sumezulike added the T: bug Something isn't working label Feb 27, 2024
sumezulike added a commit to sumezulike/black that referenced this issue Feb 27, 2024
sumezulike added a commit to sumezulike/black that referenced this issue Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant