@@ -948,6 +948,44 @@ def can_omit_invisible_parens(
948948 """
949949 line = rhs .body
950950
951+ # We can't omit parens if doing so would result in a type: ignore comment
952+ # sharing a line with other comments, as that breaks type: ignore parsing.
953+ # Check if the opening bracket (last leaf of head) has comments that would merge
954+ # with comments from the first line of the body.
955+ if rhs .head .leaves :
956+ opening_bracket = rhs .head .leaves [- 1 ]
957+ head_comments = rhs .head .comments .get (id (opening_bracket ), [])
958+
959+ # If there are comments on the opening bracket line, check if any would
960+ # conflict with type: ignore comments in the body
961+ if head_comments :
962+ has_type_ignore_in_head = any (
963+ is_type_ignore_comment (comment , mode = rhs .head .mode )
964+ for comment in head_comments
965+ )
966+ has_other_comment_in_head = any (
967+ not is_type_ignore_comment (comment , mode = rhs .head .mode )
968+ for comment in head_comments
969+ )
970+
971+ # Check for comments in the body that would potentially end up on the
972+ # same line as the head comments when parens are removed
973+ has_type_ignore_in_body = False
974+ has_other_comment_in_body = False
975+ for leaf in rhs .body .leaves :
976+ for comment in rhs .body .comments .get (id (leaf ), []):
977+ if is_type_ignore_comment (comment , mode = rhs .body .mode ):
978+ has_type_ignore_in_body = True
979+ else :
980+ has_other_comment_in_body = True
981+
982+ # Preserve parens if we have both type: ignore and other comments that
983+ # could end up on the same line
984+ if (has_type_ignore_in_head and has_other_comment_in_body ) or (
985+ has_other_comment_in_head and has_type_ignore_in_body
986+ ):
987+ return False
988+
951989 # We need optional parens in order to split standalone comments to their own lines
952990 # if there are no nested parens around the standalone comments
953991 closing_bracket : Leaf | None = None
0 commit comments