diff --git a/CHANGELOG.md b/CHANGELOG.md index 587259041..21f1b7254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard. - Implemented support for automatic redundant alias removal (issue #1281). - Fixed #1178: support for semicolons in decorators. - Fixed #1315: Extra newline before comment with -n + --fss. + - Fixed #1192: `-k` or `--keep-direct-and-as-imports` option has been deprecated as it is now always on. **Formatting changes implied:** - Fixed #1280: rewrite of as imports changes the behavior of the imports. diff --git a/isort/main.py b/isort/main.py index 30b405728..589db60fc 100644 --- a/isort/main.py +++ b/isort/main.py @@ -289,13 +289,6 @@ def _build_arg_parser() -> argparse.ArgumentParser: parser.add_argument( "-j", "--jobs", help="Number of files to process in parallel.", dest="jobs", type=int ) - parser.add_argument( - "-k", - "--keep-direct-and-as", - dest="keep_direct_and_as_imports", - action="store_true", - help="Turns off default behavior that removes direct imports when as imports exist.", - ) parser.add_argument("--lai", "--lines-after-imports", dest="lines_after_imports", type=int) parser.add_argument("--lbt", "--lines-between-types", dest="lines_between_types", type=int) parser.add_argument( @@ -621,10 +614,17 @@ def _build_arg_parser() -> argparse.ArgumentParser: "--apply", dest="deprecated_flags", action="append_const", - const="-y", + const="--apply", + help=argparse.SUPPRESS, + ) + parser.add_argument( + "-k", + "--keep-direct-and-as", + dest="deprecated_flags", + action="append_const", + const="--keep-direct-and-as", help=argparse.SUPPRESS, ) - return parser diff --git a/isort/output.py b/isort/output.py index b3cc785ce..247dbaeb6 100644 --- a/isort/output.py +++ b/isort/output.py @@ -267,10 +267,7 @@ def _with_from_imports( for from_import in copy.copy(from_imports): if from_import in as_imports: idx = from_imports.index(from_import) - if ( - config.keep_direct_and_as_imports - and parsed.imports[section]["from"][module][from_import] - ): + if parsed.imports[section]["from"][module][from_import]: from_imports[(idx + 1) : (idx + 1)] = as_imports.pop(from_import) else: from_imports[idx : (idx + 1)] = as_imports.pop(from_import) @@ -313,10 +310,7 @@ def _with_from_imports( f"{comments and ';' or config.comment_prefix} " f"{comment}" ) if from_import in as_imports: - if ( - config.keep_direct_and_as_imports - and parsed.imports[section]["from"][module][from_import] - ): + if parsed.imports[section]["from"][module][from_import]: new_section_output.append( wrap.line(single_import_line, parsed.line_separator, config) ) @@ -344,10 +338,7 @@ def _with_from_imports( from_comments = parsed.categorized_comments["straight"].get( f"{module}.{from_import}" ) - if ( - config.keep_direct_and_as_imports - and parsed.imports[section]["from"][module][from_import] - ): + if parsed.imports[section]["from"][module][from_import]: new_section_output.append( with_comments( from_comments, @@ -383,8 +374,6 @@ def _with_from_imports( comments = None for from_import in copy.copy(from_imports): - if from_import in as_imports and not config.keep_direct_and_as_imports: - continue comment = ( parsed.categorized_comments["nested"].get(module, {}).pop(from_import, None) ) @@ -408,8 +397,7 @@ def _with_from_imports( while from_imports and ( from_imports[0] not in as_imports or ( - config.keep_direct_and_as_imports - and config.combine_as_imports + config.combine_as_imports and parsed.imports[section]["from"][module][from_import] ) ): @@ -488,7 +476,7 @@ def _with_straight_imports( import_definition = [] if module in parsed.as_map["straight"]: - if config.keep_direct_and_as_imports and parsed.imports[section]["straight"][module]: + if parsed.imports[section]["straight"][module]: import_definition.append(f"{import_type} {module}") import_definition.extend( f"{import_type} {module} as {as_import}" diff --git a/isort/settings.py b/isort/settings.py index 80938dbd5..437cca597 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -61,7 +61,7 @@ RUNTIME_SOURCE = "runtime" -DEPRECATED_SETTINGS = ("not_skip",) +DEPRECATED_SETTINGS = ("not_skip", "keep_direct_and_as_imports") _STR_BOOLEAN_MAPPING = { "y": True, @@ -141,7 +141,6 @@ class _Config: lines_between_types: int = 0 combine_as_imports: bool = False combine_star: bool = False - keep_direct_and_as_imports: bool = True include_trailing_comma: bool = False from_first: bool = False verbose: bool = False diff --git a/tests/test_isort.py b/tests/test_isort.py index e39b7ed86..639d35be4 100644 --- a/tests/test_isort.py +++ b/tests/test_isort.py @@ -807,7 +807,7 @@ def test_add_imports() -> None: ) # On a file that has no pre-existing imports - test_input = '"""Module docstring"""\n' "\nclass MyClass(object):\n pass\n" + test_input = '"""Module docstring"""\n' "class MyClass(object):\n pass\n" test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"]) assert test_output == ( '"""Module docstring"""\n' @@ -1376,7 +1376,7 @@ def test_combined_from_and_as_imports() -> None: assert isort.code(test_input, combine_as_imports=True) == test_input test_input = "import os \nimport os as _os" test_output = "import os\nimport os as _os\n" - assert isort.code(test_input, keep_direct_and_as_imports=True) == test_output + assert isort.code(test_input) == test_output def test_as_imports_with_line_length() -> None: @@ -3310,11 +3310,9 @@ def test_multiple_as_imports() -> None: assert test_output == test_input test_output = isort.code(test_input, combine_as_imports=True) assert test_output == "from a import b as b, b as bb, b as bb_\n" - test_output = isort.code(test_input, keep_direct_and_as_imports=True) + test_output = isort.code(test_input) assert test_output == test_input - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=True - ) + test_output = isort.code(code=test_input, combine_as_imports=True) assert test_output == "from a import b as b, b as bb, b as bb_\n" test_input = ( @@ -3323,17 +3321,9 @@ def test_multiple_as_imports() -> None: "from a import b as bb\n" "from a import b as bb_\n" ) - test_output = isort.code(test_input, keep_direct_and_as_imports=False) - assert test_output == "from a import b as b\nfrom a import b as bb\nfrom a import b as bb_\n" - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=False - ) - assert test_output == "from a import b as b, b as bb, b as bb_\n" - test_output = isort.code(test_input, keep_direct_and_as_imports=True) + test_output = isort.code(test_input) assert test_output == test_input - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=True - ) + test_output = isort.code(code=test_input, combine_as_imports=True) assert test_output == "from a import b, b as b, b as bb, b as bb_\n" test_input = ( @@ -3342,57 +3332,27 @@ def test_multiple_as_imports() -> None: "from a import b\n" "from a import b as f\n" ) - test_output = isort.code(test_input, keep_direct_and_as_imports=False) - assert test_output == "from a import b as c\nfrom a import b as e\nfrom a import b as f\n" - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=False - ) - assert test_output == "from a import b as c, b as e, b as f\n" - test_output = isort.code(test_input, keep_direct_and_as_imports=True) + test_output = isort.code(test_input) assert ( test_output == "from a import b\nfrom a import b as c\nfrom a import b as e\nfrom a import b as f\n" ) - test_output = isort.code(code=test_input, no_inline_sort=True, keep_direct_and_as_imports=False) - assert test_output == "from a import b as c\nfrom a import b as e\nfrom a import b as f\n" - test_output = isort.code(code=test_input, keep_direct_and_as_imports=True, no_inline_sort=True) + test_output = isort.code(code=test_input, no_inline_sort=True) assert ( test_output == "from a import b\nfrom a import b as c\nfrom a import b as e\nfrom a import b as f\n" ) - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=True - ) + test_output = isort.code(code=test_input, combine_as_imports=True) assert test_output == "from a import b, b as c, b as e, b as f\n" - test_output = isort.code( - code=test_input, - combine_as_imports=True, - no_inline_sort=True, - keep_direct_and_as_imports=False, - ) - assert test_output == "from a import b as e, b as c, b as f\n" - test_output = isort.code( - code=test_input, - combine_as_imports=True, - keep_direct_and_as_imports=True, - no_inline_sort=True, - ) + test_output = isort.code(code=test_input, combine_as_imports=True, no_inline_sort=True) assert test_output == "from a import b, b as e, b as c, b as f\n" test_input = "import a as a\nimport a as aa\nimport a as aa_\n" - test_output = isort.code(test_input, keep_direct_and_as_imports=False) - assert test_output == test_input - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=True - ) + test_output = isort.code(code=test_input, combine_as_imports=True) assert test_output == test_input - test_input = "import a\nimport a as a\nimport a as aa\nimport a as aa_\n" - test_output = isort.code(test_input, keep_direct_and_as_imports=False) assert test_output == "import a as a\nimport a as aa\nimport a as aa_\n" - test_output = isort.code( - code=test_input, combine_as_imports=True, keep_direct_and_as_imports=True - ) + test_output = isort.code(code=test_input, combine_as_imports=True) assert test_output == test_input @@ -3411,47 +3371,6 @@ def test_all_imports_from_single_module() -> None: code=test_input, combine_star=False, combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=False, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as c\n" - "from a import b as d\n" - "from a import e as f\n" - "from a import g as h\n" - "from a import i as j\n" - "from a import w, x, y, z\n" - ) - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=False, - ) - assert test_output == "import a\nfrom a import *\n" - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=True, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=False, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as c, b as d, e as f, g as h, i as j, w, x, y, z\n" - ) - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=False, ) @@ -3466,86 +3385,18 @@ def test_all_imports_from_single_module() -> None: "from a import i as j\n" "from a import w, x, y, z\n" ) - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=True, - no_inline_sort=False, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as c\n" - "from a import b as d\n" - "from a import e as f\n" - "from a import g as h\n" - "from a import i as j\n" - "from a import w\n" - "from a import x\n" - "from a import y\n" - "from a import z\n" - ) - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=True, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as c\n" - "from a import b as d\n" - "from a import z, x, y, w\n" - "from a import i as j\n" - "from a import g as h\n" - "from a import e as f\n" - ) - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=True, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=False, - ) - assert test_output == "import a\nfrom a import *\n" test_output = isort.code( code=test_input, combine_star=True, combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=False, ) assert test_output == "import a\nfrom a import *\n" - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=True, - no_inline_sort=False, - ) - assert test_output == "import a\nfrom a import *\n" - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=True, - ) - assert test_output == "import a\nfrom a import *\n" test_output = isort.code( code=test_input, combine_star=False, combine_as_imports=True, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=False, ) @@ -3554,45 +3405,10 @@ def test_all_imports_from_single_module() -> None: "from a import *\n" "from a import b, b as c, b as d, e as f, g as h, i as j, w, x, y, z\n" ) - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=True, - keep_direct_and_as_imports=False, - force_single_line=True, - no_inline_sort=False, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as c\n" - "from a import b as d\n" - "from a import e as f\n" - "from a import g as h\n" - "from a import i as j\n" - "from a import w\n" - "from a import x\n" - "from a import y\n" - "from a import z\n" - ) - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=True, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=True, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as d, b as c, z, x, y, w, i as j, g as h, e as f\n" - ) test_output = isort.code( code=test_input, combine_star=False, combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=True, no_inline_sort=False, ) @@ -3614,7 +3430,6 @@ def test_all_imports_from_single_module() -> None: code=test_input, combine_star=False, combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=True, ) @@ -3629,59 +3444,18 @@ def test_all_imports_from_single_module() -> None: "from a import g as h\n" "from a import e as f\n" ) - test_output = isort.code( - code=test_input, - combine_star=False, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=True, - no_inline_sort=True, - ) - assert test_output == ( - "import a\n" - "from a import *\n" - "from a import b as c\n" - "from a import b as d\n" - "from a import e as f\n" - "from a import g as h\n" - "from a import i as j\n" - "from a import w\n" - "from a import x\n" - "from a import y\n" - "from a import z\n" - ) test_output = isort.code( code=test_input, combine_star=True, combine_as_imports=True, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=False, ) assert test_output == "import a\nfrom a import *\n" - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=True, - keep_direct_and_as_imports=False, - force_single_line=True, - no_inline_sort=False, - ) - assert test_output == "import a\nfrom a import *\n" - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=True, - keep_direct_and_as_imports=False, - force_single_line=False, - no_inline_sort=True, - ) - assert test_output == "import a\nfrom a import *\n" test_output = isort.code( code=test_input, combine_star=True, combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=True, no_inline_sort=False, ) @@ -3690,25 +3464,14 @@ def test_all_imports_from_single_module() -> None: code=test_input, combine_star=True, combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=True, ) assert test_output == "import a\nfrom a import *\n" - test_output = isort.code( - code=test_input, - combine_star=True, - combine_as_imports=False, - keep_direct_and_as_imports=False, - force_single_line=True, - no_inline_sort=True, - ) - assert test_output == "import a\nfrom a import *\n" test_output = isort.code( code=test_input, combine_star=False, combine_as_imports=True, - keep_direct_and_as_imports=True, force_single_line=True, no_inline_sort=False, ) @@ -3730,7 +3493,6 @@ def test_all_imports_from_single_module() -> None: code=test_input, combine_star=False, combine_as_imports=True, - keep_direct_and_as_imports=True, force_single_line=False, no_inline_sort=True, ) @@ -3743,7 +3505,6 @@ def test_all_imports_from_single_module() -> None: code=test_input, combine_star=False, combine_as_imports=False, - keep_direct_and_as_imports=True, force_single_line=True, no_inline_sort=True, ) @@ -3765,7 +3526,6 @@ def test_all_imports_from_single_module() -> None: code=test_input, combine_star=True, combine_as_imports=True, - keep_direct_and_as_imports=True, force_single_line=True, no_inline_sort=False, ) @@ -4793,7 +4553,7 @@ def test_multiple_aliases(): import datetime as dt import datetime as dt2 """ - assert isort.code(keep_direct_and_as_imports=True, code=test_input) == test_input + assert isort.code(code=test_input) == test_input def test_parens_in_comment(): @@ -4814,7 +4574,7 @@ def test_as_imports_mixed(): expected_output = """import datetime.datetime as dt from datetime import datetime """ - assert isort.code(test_input, keep_direct_and_as_imports=True) == expected_output + assert isort.code(test_input) == expected_output def test_no_sections_with_future():