Skip to content

Trailing commas not removed when dict/function call collapsed into single line #3229

@ghost

Description

Describe the bug

When a multiline dict/function call is collapsed into fewer lines by black, it seems to leave stray trailing commas. This does not happen when using --skip-magic-trailing-comma.

To Reproduce

For example, take this code:

def refresh_token(self, device_family, refresh_token, api_key):
    return self.orchestration.refresh_token(
        data={
            "refreshToken": refresh_token,
        },
        api_key=api_key,
    )["extensions"]["sdk"]["token"]

And run it with these arguments:

$ black file.py -l 120

The resulting output is:

def refresh_token(self, device_family, refresh_token, api_key):
    return self.orchestration.refresh_token(data={"refreshToken": refresh_token,}, api_key=api_key,)["extensions"][
        "sdk"
    ]["token"]

Expected behavior

The output should look something like this (without the stray trailing commas):

def refresh_token(self, device_family, refresh_token, api_key):
    return self.orchestration.refresh_token(data={"refreshToken": refresh_token}, api_key=api_key)["extensions"][
        "sdk"
    ]["token"]

Environment

  • Black's version: 22.6.0 / main
  • OS and Python version: Debian 10, Python 3.10.4

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions