Unverified Commit 6b994fdb authored by Jelle Zijlstra's avatar Jelle Zijlstra Committed by GitHub
Browse files

fix handling of comments in from imports (#829)

Fixes #671
parent e6ddb68c
Loading
Loading
Loading
Loading
+10 −3
Changes for black.py: 10 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -2405,10 +2405,17 @@ def bracket_split_build_line(
        if leaves:
            # Since body is a new indent level, remove spurious leading whitespace.
            normalize_prefix(leaves[0], inside_brackets=True)
            # Ensure a trailing comma when expected.
            # Ensure a trailing comma for imports, but be careful not to add one after
            # any comments.
            if original.is_import:
                if leaves[-1].type != token.COMMA:
                    leaves.append(Leaf(token.COMMA, ","))
                for i in range(len(leaves) - 1, -1, -1):
                    if leaves[i].type == STANDALONE_COMMENT:
                        continue
                    elif leaves[i].type == token.COMMA:
                        break
                    else:
                        leaves.insert(i + 1, Leaf(token.COMMA, ","))
                        break
    # Populate the line
    for leaf in leaves:
        result.append(leaf, preformatted=True)
+51 −0
Changes for tests/data/comments7.py: 51 added lines, 0 removed lines.
Original line number Diff line number Diff line
from .config import (
    Any,
    Bool,
    ConfigType,
    ConfigTypeAttributes,
    Int,
    Path,
    #  String,
    #  resolve_to_config_type,
    #  DEFAULT_TYPE_ATTRIBUTES,
)


from .config import (
    Any,
    Bool,
    ConfigType,
    ConfigTypeAttributes,
    Int,
    no_comma_here_yet
    #  and some comments,
    #  resolve_to_config_type,
    #  DEFAULT_TYPE_ATTRIBUTES,
)

# output

from .config import (
    Any,
    Bool,
    ConfigType,
    ConfigTypeAttributes,
    Int,
    Path,
    #  String,
    #  resolve_to_config_type,
    #  DEFAULT_TYPE_ATTRIBUTES,
)


from .config import (
    Any,
    Bool,
    ConfigType,
    ConfigTypeAttributes,
    Int,
    no_comma_here_yet,
    #  and some comments,
    #  resolve_to_config_type,
    #  DEFAULT_TYPE_ATTRIBUTES,
)
+8 −0
Changes for tests/test_black.py: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -388,6 +388,14 @@ def test_comments6(self) -> None:
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode())

    @patch("black.dump_to_file", dump_to_stderr)
    def test_comments7(self) -> None:
        source, expected = read_data("comments7")
        actual = fs(source)
        self.assertFormatEqual(expected, actual)
        black.assert_equivalent(source, actual)
        black.assert_stable(source, actual, black.FileMode())

    @patch("black.dump_to_file", dump_to_stderr)
    def test_cantfit(self) -> None:
        source, expected = read_data("cantfit")