Skip to content

Commit

Permalink
Fix missed cases in the # type: ignore logic (#1059)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
msullivan authored and JelleZijlstra committed Oct 11, 2019
1 parent 6fe8009 commit 1336094
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
21 changes: 17 additions & 4 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,10 +1340,23 @@ 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]), []):
if is_type_comment(comment, " ignore"):
return True

# 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

return False

Expand Down
6 changes: 1 addition & 5 deletions tests/data/comments2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
7 changes: 7 additions & 0 deletions tests/data/comments6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 1336094

Please sign in to comment.