@@ -82,7 +82,21 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
8282 splitter
8383 ):
8484 line_parts = re .split (exp , line_without_comment )
85- if comment and not (config .use_parentheses and "noqa" in comment ):
85+ _is_vertical_mode = wrap_mode in (
86+ Modes .VERTICAL_HANGING_INDENT , # type: ignore
87+ Modes .VERTICAL_GRID_GROUPED , # type: ignore
88+ )
89+ # Determine whether the comment should be hoisted to the opening
90+ # parenthesis line rather than embedded in the import line.
91+ # This happens for noqa comments (when use_parentheses is True)
92+ # and for all comments in vertical hanging modes (when
93+ # use_parentheses is False), so that multi_line_output=3/5 is
94+ # respected even without an explicit use_parentheses=True setting.
95+ _hoist_comment_to_paren = comment and (
96+ (config .use_parentheses and "noqa" in comment )
97+ or (_is_vertical_mode and not config .use_parentheses )
98+ )
99+ if comment and not _hoist_comment_to_paren :
86100 _comma_maybe = (
87101 ","
88102 if (
@@ -109,21 +123,18 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) ->
109123 line_separator ,
110124 config ,
111125 )
112- if config .use_parentheses :
126+ if config .use_parentheses or _is_vertical_mode :
113127 if splitter == "as " :
114128 output = f"{ content } { splitter } { cont_line .lstrip ()} "
115129 else :
116130 _comma = "," if config .include_trailing_comma and not comment else ""
117131
118- if wrap_mode in (
119- Modes .VERTICAL_HANGING_INDENT , # type: ignore
120- Modes .VERTICAL_GRID_GROUPED , # type: ignore
121- ):
132+ if _is_vertical_mode :
122133 _separator = line_separator
123134 else :
124135 _separator = ""
125136 noqa_comment = ""
126- if comment and "noqa" in comment :
137+ if _hoist_comment_to_paren :
127138 noqa_comment = f"{ config .comment_prefix } { comment } "
128139 cont_line = cont_line .rstrip ()
129140 _comma = "," if config .include_trailing_comma else ""
0 commit comments