From: Michael J. Sullivan Date: Fri, 11 Oct 2019 01:25:33 +0000 (-0700) Subject: Fix missed cases in the `# type: ignore` logic (#1059) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/133609463459b2b6a98f08bcf41a07f6b14ab747 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. --- diff --git a/black.py b/black.py index d8aa356..b8736e7 100644 --- a/black.py +++ b/black.py @@ -1340,10 +1340,23 @@ class Line: # (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 diff --git a/tests/data/comments2.py b/tests/data/comments2.py index 248552c..e928f6d 100644 --- a/tests/data/comments2.py +++ b/tests/data/comments2.py @@ -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? diff --git a/tests/data/comments6.py b/tests/data/comments6.py index 7bc83ae..3de8b2e 100644 --- a/tests/data/comments6.py +++ b/tests/data/comments6.py @@ -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 +)