Skip to content

Commit c136ba5

Browse files
committed
Don't sort imports
When removing unused imports or breaking up lines, keep the original order. Users can use other linters/formatters to fix import order (e.g. isort or reorder-python-imports). Closes #229.
1 parent 9697f3f commit c136ba5

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

autoflake.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright (C) 2012-2019 Steven Myint
2+
# Copyright (C) Steven Myint
33
#
44
# Permission is hereby granted, free of charge, to any person obtaining
55
# a copy of this software and associated documentation files (the
@@ -470,7 +470,7 @@ def filter_from_import(line, unused_module):
470470

471471
indentation += "import "
472472

473-
return indentation + ", ".join(sorted(filtered_imports)) + get_line_ending(line)
473+
return indentation + ", ".join(filtered_imports) + get_line_ending(line)
474474

475475

476476
def break_up_import(line):
@@ -496,7 +496,7 @@ def break_up_import(line):
496496
assert newline
497497

498498
return "".join(
499-
[indentation + i.strip() + newline for i in sorted(imports.split(","))],
499+
[indentation + i.strip() + newline for i in imports.split(",")],
500500
)
501501

502502

test_autoflake.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,13 @@ def test_multiline_statement(self):
601601

602602
def test_break_up_import(self):
603603
self.assertEqual(
604-
"import abc\nimport math\nimport subprocess\n",
604+
"import abc\nimport subprocess\nimport math\n",
605605
autoflake.break_up_import("import abc, subprocess, math\n"),
606606
)
607607

608608
def test_break_up_import_with_indentation(self):
609609
self.assertEqual(
610-
" import abc\n import math\n import subprocess\n",
610+
" import abc\n import subprocess\n import math\n",
611611
autoflake.break_up_import(" import abc, subprocess, math\n"),
612612
)
613613

@@ -620,7 +620,7 @@ def test_break_up_import_should_do_nothing_on_no_line_ending(self):
620620
def test_filter_from_import_no_remove(self):
621621
self.assertEqual(
622622
"""\
623-
from foo import abc, math, subprocess\n""",
623+
from foo import abc, subprocess, math\n""",
624624
autoflake.filter_from_import(
625625
" from foo import abc, subprocess, math\n",
626626
unused_module=[],
@@ -630,7 +630,7 @@ def test_filter_from_import_no_remove(self):
630630
def test_filter_from_import_remove_module(self):
631631
self.assertEqual(
632632
"""\
633-
from foo import math, subprocess\n""",
633+
from foo import subprocess, math\n""",
634634
autoflake.filter_from_import(
635635
" from foo import abc, subprocess, math\n",
636636
unused_module=["foo.abc"],

0 commit comments

Comments
 (0)