Skip to content

Incorrectly adds space in slice #1859

@RadixSeven

Description

@RadixSeven

Describe the bug
Black adds a space before the colon in a string slice with no upper bound
To Reproduce

a = "foo"
b = a[len(a) - 2:]

when run on black with no arguments produces

a = "foo"
b = a[len(a) - 2 :]

Expected behavior A clear and concise description of what you expected to happen.
The correct behavior would be to output

a = "foo"
b = a[len(a) - 2:]

Environment (please complete the following information):

  • Version: 20.8b1
  • OS and Python version: Linux/Python 3.8.1

Does this bug also happen on master? To answer this, you have two options:
Yes

Additional context Add any other context about the problem here.
flake8 and PyCharm both report "E203 whitespace before ':'" with Black's current output

The correct behavior is inferred from this example from the pep8 document ham[1:9:]

I'm currently working around this with # fmt: off since our files have to pass flake8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    R: duplicateThis issue or pull request already existsR: not a bugThis is deliberate behavior of Black.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions