Skip to content

Commit

Permalink
Implemented #1603: Support for disabling float_to_top from the comman…
Browse files Browse the repository at this point in the history
…d line.
  • Loading branch information
timothycrosley committed Nov 29, 2020
1 parent 6ed25c6 commit e9b1bd4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .isort.cfg
Expand Up @@ -2,3 +2,4 @@
profile=hug
src_paths=isort,test
skip=tests/unit/example_crlf_file.py
float_to_top=true
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Implemented #1562, #1592 & #1593: Better more useful fatal error messages.
- Implemented #1575: Support for automatically fixing mixed indentation of import sections.
- Implemented #1582: Added a CLI option for skipping symlinks.
- Implemented #1603: Support for disabling float_to_top from the command line.

### 5.6.4 October 12, 2020
- Fixed #1556: Empty line added between imports that should be skipped.
Expand Down
12 changes: 12 additions & 0 deletions isort/main.py
Expand Up @@ -430,6 +430,12 @@ def _build_arg_parser() -> argparse.ArgumentParser:
"*NOTE*: It currently doesn't work with cimports and introduces some extra over-head "
"and a performance penalty.",
)
output_group.add_argument(
"--dont-float-to-top",
dest="dont_float_to_top",
action="store_true",
help="Forces --float-to-top setting off. See --float-to-top for more information.",
)
output_group.add_argument(
"--ca",
"--combine-as",
Expand Down Expand Up @@ -864,6 +870,12 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
del arguments["dont_order_by_type"]
if "dont_follow_links" in arguments:
arguments["follow_links"] = False
if "dont_float_to_top" in arguments:
del arguments["dont_float_to_top"]
if arguments.get("float_to_top", False):
sys.exit("Can't set both --float-to-top and --dont-float-to-top.")
else:
arguments["float_to_top"] = False
multi_line_output = arguments.get("multi_line_output", None)
if multi_line_output:
if multi_line_output.isdigit():
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/test_main.py
Expand Up @@ -408,6 +408,53 @@ def function():
main.main([str(tmp_file), "--filename", str(tmp_file)], stdin=build_input_content())


def test_isort_float_to_top_overrides(tmpdir, capsys):
"""Tests isorts supports overriding float to top from CLI"""
test_input = """
import b
def function():
pass
import a
"""
config_file = tmpdir.join(".isort.cfg")
config_file.write(
"""
[settings]
float_to_top=True
"""
)
python_file = tmpdir.join("file.py")
python_file.write(test_input)

main.main([str(python_file)])
out, error = capsys.readouterr()
assert not error
assert "Fixing" in out
assert python_file.read_text(encoding="utf8") == (
"""
import a
import b
def function():
pass
"""
)

python_file.write(test_input)
main.main([str(python_file), "--dont-float-to-top"])
_, error = capsys.readouterr()
assert not error
assert python_file.read_text(encoding="utf8") == test_input

with pytest.raises(SystemExit):
main.main([str(python_file), "--float-to-top", "--dont-float-to-top"])


def test_isort_with_stdin(capsys):
# ensures that isort sorts stdin without any flags

Expand Down

0 comments on commit e9b1bd4

Please sign in to comment.