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

Single-argument function includes trailing comma inconsistently #4080

Closed
charliermarsh opened this issue Nov 30, 2023 · 3 comments · Fixed by #4164
Closed

Single-argument function includes trailing comma inconsistently #4080

charliermarsh opened this issue Nov 30, 2023 · 3 comments · Fixed by #4164
Labels
T: bug Something isn't working

Comments

@charliermarsh
Copy link
Contributor

Describe the bug

When a single-argument function definition breaks across multiple lines, Black will add a trailing comma (after the argument) in some cases but not others.

To Reproduce

For example, given the following Black adds a trailing comma after the annotation on x in the first case, but not the second, third, or fourth:

# Black inserts a trailing comma after the first argument's annotation here.
def long_function_name_goes_here(
    x: Callable[List[int]]
) -> Union[List[int], float, str, bytes, Tuple[int]]:
    pass

# But not here, or in any of the below.
def long_function_name_goes_here(
    x: Callable[[str, Any], int]
) -> Union[List[int], float, str, bytes, Tuple[int]]:
    pass

def long_function_name_goes_here(
    x: Callable[List[int, str]]
) -> Union[List[int], float, str, bytes, Tuple[int]]:
    pass

def long_function_name_goes_here(
    x: Callable[List[int], str]
) -> Union[List[int], float, str, bytes, Tuple[int]]:
    pass

So the inclusion of the trailing comma seems to depend on the content of the annotation (but not its length). One observation: Black omits the trailing comma when the annotation itself contains a comma.

(Here's the same example in the playground.)

@JelleZijlstra
Copy link
Collaborator

Thanks. I think we should always add a trailing comma here.

@drhagen
Copy link

drhagen commented Dec 10, 2023

I would argue that Black preserving the trailing comma would be more consistent with the overall behavior of Black. For example, both of these function calls are acceptable to Black:

# Preserves lack of trailing comma
really_really_really_really_really_long_name(
    really_really_really_really_really_long_name
)

# Preserves trailing comma
really_really_really_really_really_long_name(
    really_really_really_really_really_long_name,
)

The reason for this is that lists of things is one of the places that Black gives users a choice of formatting: (1) I want as few of lines as possible, (2) one line per item. This creates an ambiguity when there is only one element: Did the user intend to choose the everything-on-one-line option (no comma) or did the user intend to choose the split-elements-across-lines option (trailing comma)? Everywhere else, Black resolves this ambiguity by preserving trailing commas, and I think it should do that in function definitions too, even single parameter functions.

cobaltt7 added a commit to cobaltt7/black that referenced this issue Jan 24, 2024
cobaltt7 added a commit to cobaltt7/black that referenced this issue Jan 25, 2024
cobaltt7 added a commit to cobaltt7/black that referenced this issue Jan 26, 2024
@JelleZijlstra
Copy link
Collaborator

@drhagen possibly, but we already add the trailing comma for non-annotated parameters, and if nothing else we should be consistent.

% black  -l 20 -c 'def some_call(aaaaaarg): pass'
def some_call(
    aaaaaarg,
):
    pass

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

Successfully merging a pull request may close this issue.

3 participants