From 4b449e7471c31ae2d3a890510322c40594cacc8f Mon Sep 17 00:00:00 2001
From: "Michael J. Sullivan" <sully@msully.net>
Date: Mon, 25 Nov 2019 14:16:00 -0800
Subject: [PATCH] Fix unstable formatting with some `# type: ignore`s (#1113)

`type: ignore` shouldn't block collapsing a line, since it will still
apply fine to the merged line. This prevents an issue where a reformat
causes it to shift lines and then be merged on a subsequent pass.

There is a downside to this, which is that it can cause a `type:
ignore` to apply to more code than was originally intended. There
might be a way to apply this in a more limited situation, but I'm not
sure what it is.

Fixes #1061.
---
 black.py                |  5 ++++-
 tests/data/comments2.py | 13 +++++++++++++
 tests/data/comments7.py |  4 +---
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/black.py b/black.py
index 68c0052..1c35643 100644
--- a/black.py
+++ b/black.py
@@ -1407,7 +1407,10 @@ class Line:
         for leaf_id, comments in self.comments.items():
             for comment in comments:
                 if is_type_comment(comment):
-                    if leaf_id not in ignored_ids or comment_seen:
+                    if comment_seen or (
+                        not is_type_comment(comment, " ignore")
+                        and leaf_id not in ignored_ids
+                    ):
                         return True
 
                 comment_seen = True
diff --git a/tests/data/comments2.py b/tests/data/comments2.py
index f4a30b8..89c2910 100644
--- a/tests/data/comments2.py
+++ b/tests/data/comments2.py
@@ -148,6 +148,12 @@ short
 
 CONFIG_FILES = [CONFIG_FILE, ] + SHARED_CONFIG_FILES + USER_CONFIG_FILES  # type: Final
 
+class Test:
+    def _init_host(self, parsed) -> None:
+        if (parsed.hostname is None or  # type: ignore
+                not parsed.hostname.strip()):
+            pass
+
 #######################
 ### SECTION COMMENT ###
 #######################
@@ -312,6 +318,13 @@ short
 
 CONFIG_FILES = [CONFIG_FILE,] + SHARED_CONFIG_FILES + USER_CONFIG_FILES  # type: Final
 
+
+class Test:
+    def _init_host(self, parsed) -> None:
+        if parsed.hostname is None or not parsed.hostname.strip():  # type: ignore
+            pass
+
+
 #######################
 ### SECTION COMMENT ###
 #######################
diff --git a/tests/data/comments7.py b/tests/data/comments7.py
index 948b3b0..4095125 100644
--- a/tests/data/comments7.py
+++ b/tests/data/comments7.py
@@ -93,9 +93,7 @@ result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
 
 def func():
-    c = call(
-        0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1],  # type: ignore
-    )
+    c = call(0.0123, 0.0456, 0.0789, 0.0123, 0.0789, a[-1],)  # type: ignore
 
     # The type: ignore exception only applies to line length, not
     # other types of formatting.
-- 
2.39.5