Commit 13360946 authored by Michael J. Sullivan's avatar Michael J. Sullivan Committed by Jelle Zijlstra
Browse files

Fix missed cases in the `# type: ignore` logic (#1059)

In #1040 I had convinced myself that the type ignore logic didn't
need anything like the ignored_ids from the type comment logic, but I
was wrong, and we do.

We hit these cases in practice a bunch.
parent 6fe80093
Loading
Loading
Loading
Loading
+15 −2
Changes for black.py: 15 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -1340,8 +1340,21 @@ def contains_unsplittable_type_ignore(self) -> bool:
        # (unfortunately) need to check the actual source lines and
        # only report an unsplittable 'type: ignore' if this line was
        # one line in the original code.
        if self.leaves[0].lineno == self.leaves[-1].lineno:
            for comment in self.comments.get(id(self.leaves[-1]), []):

        # Like in the type comment check above, we need to skip a black added
        # trailing comma or invisible paren, since it will be the original leaf
        # before it that has the original line number.
        last_idx = -1
        last_leaf = self.leaves[-1]
        if len(self.leaves) > 2 and (
            last_leaf.type == token.COMMA
            or (last_leaf.type == token.RPAR and not last_leaf.value)
        ):
            last_idx = -2

        if self.leaves[0].lineno == self.leaves[last_idx].lineno:
            for node in self.leaves[last_idx:]:
                for comment in self.comments.get(id(node), []):
                    if is_type_comment(comment, " ignore"):
                        return True

+1 −5
Changes for tests/data/comments2.py: 1 added line, 5 removed lines.
Original line number Diff line number Diff line
@@ -235,11 +235,7 @@ def inline_comments_in_brackets_ruin_everything():
            body,
            parameters.children[-1],  # )2
        ]
        parameters.children = [
            parameters.what_if_this_was_actually_long.children[0],
            body,
            parameters.children[-1],
        ]  # type: ignore
        parameters.children = [parameters.what_if_this_was_actually_long.children[0], body, parameters.children[-1]]  # type: ignore
    if (
        self._proc is not None
        # has the child process finished?
+7 −0
Changes for tests/data/comments6.py: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -105,3 +105,10 @@ def func(


result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # aaa

AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA  # type: ignore

call_to_some_function_asdf(
    foo,
    [AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB],  # type: ignore
)