@@ -340,9 +340,10 @@ def _with_from_imports(
340340 comment = (
341341 parsed .categorized_comments ["nested" ].get (module , {}).pop (from_import , None )
342342 )
343- if comment :
343+ if comment is not None :
344+ comment_text = f" { comment } " if comment else ""
344345 single_import_line += (
345- f"{ (comments and ';' ) or config .comment_prefix } { comment } "
346+ f"{ (comments and ';' ) or config .comment_prefix } { comment_text } "
346347 )
347348 if from_import in as_imports :
348349 if (
@@ -410,7 +411,7 @@ def _with_from_imports(
410411 .get (module , {})
411412 .pop (from_import , None )
412413 )
413- if specific_comment :
414+ if specific_comment is not None :
414415 from_comments .append (specific_comment )
415416 output .append (
416417 wrap .line (
@@ -432,7 +433,7 @@ def _with_from_imports(
432433 .get (module , {})
433434 .pop (as_import , None )
434435 )
435- if specific_comment :
436+ if specific_comment is not None :
436437 from_comments .append (specific_comment )
437438
438439 output .append (
@@ -465,7 +466,7 @@ def _with_from_imports(
465466 comment = (
466467 parsed .categorized_comments ["nested" ].get (module , {}).pop (from_import , None )
467468 )
468- if comment :
469+ if comment is not None :
469470 # If the comment is a noqa and hanging indent wrapping is used,
470471 # keep the name in the main list and hoist the comment to the statement.
471472 if (
@@ -488,8 +489,9 @@ def _with_from_imports(
488489 removed = config .ignore_comments ,
489490 comment_prefix = config .comment_prefix ,
490491 )
492+ comment_text = f" { comment } " if comment else ""
491493 single_import_line += (
492- f"{ (use_comments and ';' ) or config .comment_prefix } { comment } "
494+ f"{ (use_comments and ';' ) or config .comment_prefix } { comment_text } "
493495 )
494496 output .append (wrap .line (single_import_line , parsed .line_separator , config ))
495497
@@ -610,17 +612,17 @@ def _with_straight_imports(
610612 inline_comments .extend (parsed .categorized_comments ["straight" ][module ])
611613
612614 combined_straight_imports = ", " .join (straight_modules )
613- if inline_comments :
614- combined_inline_comments = " " .join (inline_comments )
615- else :
616- combined_inline_comments = ""
617615
618616 output .extend (above_comments )
619617
620- if combined_inline_comments :
621- output .append (
622- f"{ import_type } { combined_straight_imports } # { combined_inline_comments } "
623- )
618+ if inline_comments :
619+ combined_inline_comments = " " .join (c for c in inline_comments if c )
620+ if combined_inline_comments :
621+ output .append (
622+ f"{ import_type } { combined_straight_imports } # { combined_inline_comments } "
623+ )
624+ else :
625+ output .append (f"{ import_type } { combined_straight_imports } #" )
624626 else :
625627 output .append (f"{ import_type } { combined_straight_imports } " )
626628
0 commit comments