diff --git a/.isort.cfg b/.isort.cfg index 567d1abd6..9ab265a44 100644 --- a/.isort.cfg +++ b/.isort.cfg @@ -2,3 +2,4 @@ profile=hug src_paths=isort,test skip=tests/unit/example_crlf_file.py +float_to_top=true diff --git a/CHANGELOG.md b/CHANGELOG.md index e2b4f92a0..6e81a6877 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/isort/main.py b/isort/main.py index 8e7d0e017..cdf9ba00d 100644 --- a/isort/main.py +++ b/isort/main.py @@ -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", @@ -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(): diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 08f274630..4af4d58c2 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -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