Skip to content

Single line with binary operator in commented list causes additional line breaks #4026

Closed

Description

@zyzzyxdonta

Describe the bug

Inside a list, a comment followed by a single line with division operator causes additional lines breaks before each division operator.

To Reproduce

For example, take this code:

import pathlib

foobar = [
    # comment
    pathlib.Path("foo") / "bar" / "baz",
]

fizzbuzz = [
    # comment
    23 / 7
]

And run black on it with the default config.

The resulting formatted code is:

import pathlib

foobar = [
    # comment
    pathlib.Path("foo")
    / "bar"
    / "baz",
]

fizzbuzz = [
    # comment
    23
    / 7
]

Expected behavior

The formatting doesn't change (apart from maybe the trailing comma).

Environment

  • Black's version: 23.10.1 (compiled: yes)
  • OS and Python version: Fedora Linux 38 / Python 3.11.6

Additional context

Formatting does not occur if the expression is enclosed in parentheses, e.g.

(pathlib.Path("foo") / "bar" / "baz")

It also doesn't happen any more once additional lines are introduced to the lists.

Activity

  1. changed the title [-]Single line with division operator in commented list causes additional line breaks[/-] [+]Single line with binary operator in commented list causes additional line breaks[/+] on Nov 6, 2023
  2. zyzzyxdonta commented on Nov 6, 2023

    @zyzzyxdonta
    Author

    This also occurs with other operators like +, -, *.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: linebreakHow should we split up lines?T: bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions