Skip to content

Single-argument function includes trailing comma inconsistently #4080

Description

@charliermarsh

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions