@@ -293,7 +293,8 @@ def is_import_stmt(node):
293293
294294def touch_import_top (package , name_to_import , node ):
295295 """Works like `does_tree_import` but adds an import statement at the
296- top if it was not imported (but below any __future__ imports).
296+ top if it was not imported (but below any __future__ imports) and below any
297+ comments such as shebang lines).
297298
298299 Based on lib2to3.fixer_util.touch_import()
299300
@@ -376,10 +377,12 @@ def touch_import_top(package, name_to_import, node):
376377 else :
377378 children_hooks = []
378379
379- FromImport (package , [Leaf (token .NAME , name_to_import , prefix = u" " )])
380+ # FromImport(package, [Leaf(token.NAME, name_to_import, prefix=u" ")])
380381
381382 children_import = [import_ , Newline ()]
382- root .insert_child (insert_pos , Node (syms .simple_stmt , children_import ))
383+ old_prefix = root .children [insert_pos ].prefix
384+ root .children [insert_pos ].prefix = u''
385+ root .insert_child (insert_pos , Node (syms .simple_stmt , children_import , prefix = old_prefix ))
383386 if len (children_hooks ) > 0 :
384387 root .insert_child (insert_pos + 1 , Node (syms .simple_stmt , children_hooks ))
385388
0 commit comments