Skip to content

Commit 1336094

Browse files
msullivanJelleZijlstra
authored andcommitted
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.
1 parent 6fe8009 commit 1336094

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

black.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,23 @@ def contains_unsplittable_type_ignore(self) -> bool:
13401340
# (unfortunately) need to check the actual source lines and
13411341
# only report an unsplittable 'type: ignore' if this line was
13421342
# one line in the original code.
1343-
if self.leaves[0].lineno == self.leaves[-1].lineno:
1344-
for comment in self.comments.get(id(self.leaves[-1]), []):
1345-
if is_type_comment(comment, " ignore"):
1346-
return True
1343+
1344+
# Like in the type comment check above, we need to skip a black added
1345+
# trailing comma or invisible paren, since it will be the original leaf
1346+
# before it that has the original line number.
1347+
last_idx = -1
1348+
last_leaf = self.leaves[-1]
1349+
if len(self.leaves) > 2 and (
1350+
last_leaf.type == token.COMMA
1351+
or (last_leaf.type == token.RPAR and not last_leaf.value)
1352+
):
1353+
last_idx = -2
1354+
1355+
if self.leaves[0].lineno == self.leaves[last_idx].lineno:
1356+
for node in self.leaves[last_idx:]:
1357+
for comment in self.comments.get(id(node), []):
1358+
if is_type_comment(comment, " ignore"):
1359+
return True
13471360

13481361
return False
13491362

tests/data/comments2.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,7 @@ def inline_comments_in_brackets_ruin_everything():
235235
body,
236236
parameters.children[-1], # )2
237237
]
238-
parameters.children = [
239-
parameters.what_if_this_was_actually_long.children[0],
240-
body,
241-
parameters.children[-1],
242-
] # type: ignore
238+
parameters.children = [parameters.what_if_this_was_actually_long.children[0], body, parameters.children[-1]] # type: ignore
243239
if (
244240
self._proc is not None
245241
# has the child process finished?

tests/data/comments6.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,10 @@ def func(
105105

106106

107107
result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # aaa
108+
109+
AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # type: ignore
110+
111+
call_to_some_function_asdf(
112+
foo,
113+
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # type: ignore
114+
)

0 commit comments

Comments
 (0)