Search code examples
python-black

How to merge lines previously broken by black due to violating the line length constraint when removing a few arguments?


Assume I have a single-line function call that exceeds black's line length constraint. Black will reformat this function call to a multi-line function call to satisfy the constraint, e.g.,

replace_config(user_args, tune_args, container_type, class_name, param_names)
replace_config(
    user_args,
    tune_args,
    container_type,
    class_name,
    param_names,
)

Further assume I would like to remove the last three arguments, which are optional, from this function call. My new code would look like this:

replace_config(
    user_args,
    tune_args,
)

How can I tell black to shorten this multi-line function call to a single-line function call because it no longer violates the line length constraint?

The output should look like this:

replace_config(user_args, tune_args)

Solution

  • You're running into the magic trailing comma feature. Black will never collapse any collections or function argument/parameter lists if it has a trailing comma.

    You can either disable the magic trailing comma feature by passing -C or --skip-magic-trailing-comma, or simply just remove the trailing comma (and the next time Black formats the file, it'll happily collapse the function call as you'd expect).

    FWIW there's an open issue on the issue tracker about this.