@@ -474,6 +474,8 @@ def _build_module_content(
474474 body : str ,
475475 header : str ,
476476 custom_file_header : str | None ,
477+ * ,
478+ future_imports : str = "" ,
477479) -> str :
478480 """Build module content by combining header and body.
479481
@@ -484,11 +486,12 @@ def _build_module_content(
484486 if custom_file_header and body :
485487 # Extract future imports from body for correct placement after custom_file_header
486488 body_without_future = body
487- extracted_future = ""
489+ extracted_future = future_imports
488490 body_lines = body .split ("\n " )
489491 future_indices = [i for i , line in enumerate (body_lines ) if line .strip ().startswith ("from __future__" )]
490492 if future_indices :
491- extracted_future = "\n " .join (body_lines [i ] for i in future_indices )
493+ if not extracted_future :
494+ extracted_future = "\n " .join (body_lines [i ] for i in future_indices )
492495 remaining_lines = [line for i , line in enumerate (body_lines ) if i not in future_indices ]
493496 body_without_future = "\n " .join (remaining_lines ).lstrip ("\n " )
494497
@@ -972,46 +975,20 @@ def _emit_results( # noqa: PLR0912, PLR0913, PLR0914, PLR0915
972975 for path , (body , future_imports , filename ) in modules .items ():
973976 if not path .parent .exists ():
974977 path .parent .mkdir (parents = True )
975- file = path .open ("wt" , encoding = config .encoding )
976978
977979 safe_filename = filename .replace ("\n " , " " ).replace ("\r " , " " ) if filename else ""
978980 effective_header = custom_file_header or header .format (safe_filename )
979-
981+ file = path . open ( "wt" , encoding = config . encoding )
980982 if custom_file_header and body :
981- # Extract future imports from body for correct placement after custom_file_header
982- body_without_future = body
983- extracted_future = future_imports # Use pre-extracted if available
984- lines = body .split ("\n " )
985- future_indices = [i for i , line in enumerate (lines ) if line .strip ().startswith ("from __future__" )]
986- if future_indices :
987- if not extracted_future :
988- # Extract future imports from body
989- extracted_future = "\n " .join (lines [i ] for i in future_indices )
990- remaining_lines = [line for i , line in enumerate (lines ) if i not in future_indices ]
991- body_without_future = "\n " .join (remaining_lines ).lstrip ("\n " )
992-
993- if extracted_future :
994- insertion_point = _find_future_import_insertion_point (custom_file_header )
995- header_before = custom_file_header [:insertion_point ].rstrip ()
996- header_after = custom_file_header [insertion_point :].strip ()
997- if header_after :
998- content = header_before + "\n " + extracted_future + "\n \n " + header_after
999- else :
1000- content = header_before + "\n \n " + extracted_future
1001- print (content , file = file )
1002- print (file = file )
1003- print (body_without_future .rstrip (), file = file )
1004- else :
1005- print (effective_header , file = file )
1006- print (file = file )
1007- print (body .rstrip (), file = file )
983+ file .write (
984+ _build_module_content (body , effective_header , custom_file_header , future_imports = future_imports ) + "\n "
985+ )
1008986 else :
1009- # Body already contains future imports, just print as-is
1010- print (effective_header , file = file )
987+ file .write (effective_header )
1011988 if body :
1012- print ( file = file )
1013- print (body .rstrip (), file = file )
1014-
989+ file . write ( " \n \n " )
990+ file . write (body .rstrip ())
991+ file . write ( " \n " )
1015992 file .close ()
1016993
1017994 if (
0 commit comments