From 8b58270f1cdec26ef0f3c7901c4b4d63e27a6cce Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Wed, 16 Jun 2021 02:23:27 +0530 Subject: [PATCH 1/6] Change description of only-sections --- isort/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/isort/main.py b/isort/main.py index ae1ab60e0..068a25e3f 100644 --- a/isort/main.py +++ b/isort/main.py @@ -685,8 +685,8 @@ def _build_arg_parser() -> argparse.ArgumentParser: dest="only_sections", action="store_true", help="Causes imports to be sorted based on their sections like STDLIB,THIRDPARTY etc. " - "Within sections, the imports are ordered by their import style and the imports with " - "same style maintain their relative positions.", + "Within sections, the imports maintain their relative positions irrespective of their " + "import_type", ) section_group.add_argument( "--ds", From fa55281f13a5d6f9b891686e4f06aa32f7fc8cc0 Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Wed, 16 Jun 2021 02:24:16 +0530 Subject: [PATCH 2/6] Add original_order field in parse.py and add logic to output.py --- isort/output.py | 11 ++++++++++- isort/parse.py | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/isort/output.py b/isort/output.py index f53d81b72..43470b2dd 100644 --- a/isort/output.py +++ b/isort/output.py @@ -85,7 +85,16 @@ def sorted_imports( lines_between = [""] * ( config.lines_between_types if from_modules and straight_modules else 0 ) - if config.from_first: + + if config.only_sections: + section_output = [] + for section_import_type in parsed.original_order[section]: + if section_import_type == "from": + section_output.append(from_imports.pop(0)) + else: + section_output.append(straight_imports.pop(0)) + + elif config.from_first: section_output = from_imports + lines_between + straight_imports else: section_output = straight_imports + lines_between + from_imports diff --git a/isort/parse.py b/isort/parse.py index 2a15c90fd..80c98b942 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -139,6 +139,7 @@ class ParsedContent(NamedTuple): line_separator: str sections: Any verbose_output: List[str] + original_order: Dict[str, List[str]] def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedContent: @@ -165,9 +166,12 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte } imports: OrderedDict[str, Dict[str, Any]] = OrderedDict() verbose_output: List[str] = [] + original_order: Dict[str, List[str]] = {} for section in chain(config.sections, config.forced_separate): imports[section] = {"straight": OrderedDict(), "from": OrderedDict()} + original_order[section] = [] + categorized_comments: CommentsDict = { "from": {}, "straight": {}, @@ -447,6 +451,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte " Do you need to define a default section?" ) root = imports[placed_module][type_of_import] # type: ignore + original_order[placed_module].append(type_of_import) for import_name in just_imports: associated_comment = nested_comments.get(import_name) if associated_comment: @@ -549,6 +554,7 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte module, False ) imports[placed_module][type_of_import][module] = straight_import # type: ignore + original_order[placed_module].append(type_of_import) change_count = len(out_lines) - original_line_count @@ -566,4 +572,5 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte line_separator=line_separator, sections=config.sections, verbose_output=verbose_output, + original_order=original_order, ) From 02c09b8941a0a6c5c36739117ae67a38f61ca669 Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Wed, 16 Jun 2021 02:24:46 +0530 Subject: [PATCH 3/6] Add/modify tests for only-sections option --- tests/unit/test_isort.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 2cbc93a43..20b8b5186 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -5071,8 +5071,8 @@ def test_only_sections() -> None: == ( "import sys\n" "import os\n" - "import math\n" "from os import path as ospath\n" + "import math\n" "from collections import defaultdict\n" "\n" "import numpy as np\n" @@ -5083,6 +5083,39 @@ def test_only_sections() -> None: == isort.code(test_input, only_sections=True, force_single_line=True) ) + test_input = ( + "from requests import post as POST, get as GET\n" + "import sys\n" + "from math import sqrt\n" + "\n" + "import numpy as np\n" + "\n" + "import os\n" + "\n" + "from .url import *\n" + "import pandas as pd\n" + "\n" + "import .views\n" + "from collections import defaultdict\n" + ) + + assert ( + isort.code(test_input, only_sections=True) + == ( + "import sys\n" + "from math import sqrt\n" + "import os\n" + "from collections import defaultdict\n" + "\n" + "from requests import post as POST, get as GET\n" + "import numpy as np\n" + "import pandas as pd\n" + "\n" + "from .url import *\n" + "import .views\n" + ) + ) + # test to ensure that from_imports remain intact with only_sections test_input = "from foo import b, a, c\n" From c9a4b1dbdb617c9363b7b379277c1aa672af5cab Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Sun, 20 Jun 2021 22:34:57 +0530 Subject: [PATCH 4/6] Correct links in docs --- docs/configuration/black_compatibility.md | 6 +++--- docs/configuration/config_files.md | 2 +- docs/configuration/custom_sections_and_ordering.md | 4 ++-- docs/configuration/multi_line_output_modes.md | 2 +- docs/configuration/options.md | 2 +- docs/upgrade_guides/5.0.0.md | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/configuration/black_compatibility.md b/docs/configuration/black_compatibility.md index c46552548..ed96f7ef5 100644 --- a/docs/configuration/black_compatibility.md +++ b/docs/configuration/black_compatibility.md @@ -7,7 +7,7 @@ Compatibility with black is very important to the isort project and comes baked All that's required to use isort alongside black is to set the isort profile to "black". !!! tip - Beyond the profile, it is common to set [skip_gitignore](https://pycqa.github.io/isort/docs/configuration/options/#skip-gitignore) (which is not enabled by default for isort as it requires git to be installed) and [line_length](https://pycqa.github.io/isort/docs/configuration/options/#skip-gitignore) as it is common to deviate from black's default of 88. + Beyond the profile, it is common to set [skip_gitignore](https://pycqa.github.io/isort/docs/configuration/options.html#skip-gitignore) (which is not enabled by default for isort as it requires git to be installed) and [line_length](https://pycqa.github.io/isort/docs/configuration/options.html#line-length) as it is common to deviate from black's default of 88. ## Using a config file (such as .isort.cfg) @@ -23,7 +23,7 @@ profile = "black" multi_line_output = 3 ``` -Read More about supported [config files](https://pycqa.github.io/isort/docs/configuration/config_files/). +Read More about supported [config files](https://pycqa.github.io/isort/docs/configuration/config_files.html). ## CLI @@ -51,7 +51,7 @@ after_success: ``` -See [built-in profiles](https://pycqa.github.io/isort/docs/configuration/profiles/) for more profiles. +See [built-in profiles](https://pycqa.github.io/isort/docs/configuration/profiles.html) for more profiles. ## Integration with pre-commit diff --git a/docs/configuration/config_files.md b/docs/configuration/config_files.md index 814b1ec63..8216a7aa0 100644 --- a/docs/configuration/config_files.md +++ b/docs/configuration/config_files.md @@ -81,7 +81,7 @@ src_paths=isort,test ## Custom config files -Optionally, you can also create a config file with a custom name, or directly point isort to a config file that falls lower in the priority order, by using [--settings-file](https://pycqa.github.io/isort/docs/configuration/options/#settings-path). +Optionally, you can also create a config file with a custom name, or directly point isort to a config file that falls lower in the priority order, by using [--settings-file](https://pycqa.github.io/isort/docs/configuration/options.html#settings-path). This can be useful, for instance, if you want to have one configuration for `.py` files and another for `.pyx` - while keeping the config files at the root of your repository. !!! tip diff --git a/docs/configuration/custom_sections_and_ordering.md b/docs/configuration/custom_sections_and_ordering.md index df145e889..8f12c5d81 100644 --- a/docs/configuration/custom_sections_and_ordering.md +++ b/docs/configuration/custom_sections_and_ordering.md @@ -115,7 +115,7 @@ import b from a import a # This will always appear below because it is a from import. ``` -However, if you prefer to keep strict alphabetical sorting you can set [force sort within sections](https://pycqa.github.io/isort/docs/configuration/options/#force-sort-within-sections) to true. Resulting in: +However, if you prefer to keep strict alphabetical sorting you can set [force sort within sections](https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections) to true. Resulting in: ```python @@ -123,7 +123,7 @@ from a import a # This will now appear at top because a appears in the alphabet import b ``` -You can even tell isort to always place from imports on top, instead of the default of placing them on bottom, using [from first](https://pycqa.github.io/isort/docs/configuration/options/#from-first). +You can even tell isort to always place from imports on top, instead of the default of placing them on bottom, using [from first](https://pycqa.github.io/isort/docs/configuration/options.html#from-first). ```python from b import b # If from first is set to True, all from imports will be placed before non-from imports. diff --git a/docs/configuration/multi_line_output_modes.md b/docs/configuration/multi_line_output_modes.md index aed2897e9..ebc102e5c 100644 --- a/docs/configuration/multi_line_output_modes.md +++ b/docs/configuration/multi_line_output_modes.md @@ -1,6 +1,6 @@ # Multi Line Output Modes -This [config option](https://pycqa.github.io/isort/docs/configuration/options/#multi-line-output) defines how from imports wrap when they extend past the line\_length +This [config option](https://pycqa.github.io/isort/docs/configuration/options.html#multi-line-output) defines how from imports wrap when they extend past the line\_length limit and has 12 possible settings: ## 0 - Grid diff --git a/docs/configuration/options.md b/docs/configuration/options.md index e07deb222..ca9f64a60 100644 --- a/docs/configuration/options.md +++ b/docs/configuration/options.md @@ -5,7 +5,7 @@ isort will disagree but commit to your way of formatting. To enable this, isort how you want your imports sorted, organized, and formatted. Too busy to build your perfect isort configuration? For curated common configurations, see isort's [built-in -profiles](https://pycqa.github.io/isort/docs/configuration/profiles/). +profiles](https://pycqa.github.io/isort/docs/configuration/profiles.html). ## Python Version diff --git a/docs/upgrade_guides/5.0.0.md b/docs/upgrade_guides/5.0.0.md index ba8e8288e..743df0297 100644 --- a/docs/upgrade_guides/5.0.0.md +++ b/docs/upgrade_guides/5.0.0.md @@ -6,7 +6,7 @@ This guide is meant to help migrate projects from using isort 4.x.x unto the 5.0 Related documentation: * [isort 5.0.0 changelog](https://pycqa.github.io/isort/CHANGELOG/#500-penny-july-4-2020) -* [isort 5 release document](https://pycqa.github.io/isort/docs/major_releases/introducing_isort_5/) +* [isort 5 release document](https://pycqa.github.io/isort/docs/major_releases/introducing_isort_5.html) !!! important - "If you use pre-commit remove seed-isort-config." If you currently use pre-commit, make sure to see the pre-commit section of this document. In particular, make sure to remove any `seed-isort-config` pre-step. @@ -19,7 +19,7 @@ for those of us who have gotten used to placing imports right above their usage If you want to move all imports to the top, you can use the new`--float-to-top` flag in the CLI or `float_to_top=true` option in your config file. -See: [https://pycqa.github.io/isort/docs/configuration/options/#float-to-top](https://pycqa.github.io/isort/docs/configuration/options/#float-to-top) +See: [https://pycqa.github.io/isort/docs/configuration/options.html#float-to-top](https://pycqa.github.io/isort/docs/configuration/options.html#float-to-top) ## Migrating CLI options @@ -46,7 +46,7 @@ The `-v` (previously for version now for verbose) and `-V` (previously for verbo The first thing to keep in mind is how isort loads config options has changed in isort 5. It will no longer merge multiple config files, instead you must have 1 isort config per a project. If you have multiple configs, they will need to be merged into 1 single one. You can see the priority order of configuration files and the manner in which they are loaded on the -[config files documentation page](https://pycqa.github.io/isort/docs/configuration/config_files/). +[config files documentation page](https://pycqa.github.io/isort/docs/configuration/config_files.html). !!! tip - "Config options are loaded relative to the file, not the isort instance." isort looks for a config file based on the path of the file you request to sort. If you have your config placed outside of the project, you can use `--settings-path` to manually specify the config location instead. Full information about how config files are loaded is in the linked config files documentation page. @@ -62,7 +62,7 @@ The option was originally added to allow working around this, and was then turne ### `known_standard_library` isort settings no longer merge together, instead they override. The old behavior of merging together caused many hard to track down errors, but the one place it was very convenient was for adding a few additional standard library modules. -In isort 5, you can still get this behavior by moving your extra modules from the `known_standard_library` setting to [`extra_standard_library`](https://pycqa.github.io/isort/docs/configuration/options/#extra-standard-library). +In isort 5, you can still get this behavior by moving your extra modules from the `known_standard_library` setting to [`extra_standard_library`](https://pycqa.github.io/isort/docs/configuration/options.html#extra-standard-library). ### module placement changes: `known_third_party`, `known_first_party`, `default_section`, etc... isort has completely rewritten its logic for placing modules in 5.0.0 to ensure the same behavior across environments. You can see the details of this change [here](https://github.com/pycqa/isort/issues/1147). From c249cacab2dc9006041da54422a8980e6c7b7e8e Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Sun, 20 Jun 2021 22:52:33 +0530 Subject: [PATCH 5/6] Revert back to the upstream version --- isort/parse.py | 7 ------- tests/unit/test_isort.py | 35 +---------------------------------- 2 files changed, 1 insertion(+), 41 deletions(-) diff --git a/isort/parse.py b/isort/parse.py index 403816cc3..aec508745 100644 --- a/isort/parse.py +++ b/isort/parse.py @@ -139,7 +139,6 @@ class ParsedContent(NamedTuple): line_separator: str sections: Any verbose_output: List[str] - original_order: Dict[str, List[str]] def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedContent: @@ -166,12 +165,9 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte } imports: OrderedDict[str, Dict[str, Any]] = OrderedDict() verbose_output: List[str] = [] - original_order: Dict[str, List[str]] = {} for section in chain(config.sections, config.forced_separate): imports[section] = {"straight": OrderedDict(), "from": OrderedDict()} - original_order[section] = [] - categorized_comments: CommentsDict = { "from": {}, "straight": {}, @@ -451,7 +447,6 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte " Do you need to define a default section?" ) root = imports[placed_module][type_of_import] # type: ignore - original_order[placed_module].append(type_of_import) for import_name in just_imports: associated_comment = nested_comments.get(import_name) if associated_comment: @@ -568,7 +563,6 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte module, False ) imports[placed_module][type_of_import][module] = straight_import # type: ignore - original_order[placed_module].append(type_of_import) change_count = len(out_lines) - original_line_count @@ -586,5 +580,4 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte line_separator=line_separator, sections=config.sections, verbose_output=verbose_output, - original_order=original_order, ) diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 4fe739587..9593c42c8 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -5142,8 +5142,8 @@ def test_only_sections() -> None: == ( "import sys\n" "import os\n" - "from os import path as ospath\n" "import math\n" + "from os import path as ospath\n" "from collections import defaultdict\n" "\n" "import numpy as np\n" @@ -5154,39 +5154,6 @@ def test_only_sections() -> None: == isort.code(test_input, only_sections=True, force_single_line=True) ) - test_input = ( - "from requests import post as POST, get as GET\n" - "import sys\n" - "from math import sqrt\n" - "\n" - "import numpy as np\n" - "\n" - "import os\n" - "\n" - "from .url import *\n" - "import pandas as pd\n" - "\n" - "import .views\n" - "from collections import defaultdict\n" - ) - - assert ( - isort.code(test_input, only_sections=True) - == ( - "import sys\n" - "from math import sqrt\n" - "import os\n" - "from collections import defaultdict\n" - "\n" - "from requests import post as POST, get as GET\n" - "import numpy as np\n" - "import pandas as pd\n" - "\n" - "from .url import *\n" - "import .views\n" - ) - ) - # test to ensure that from_imports remain intact with only_sections test_input = "from foo import b, a, c\n" From 321b1db13a381ac6617e071aa667c84c191d2f53 Mon Sep 17 00:00:00 2001 From: Aniruddha Date: Sun, 20 Jun 2021 23:30:18 +0530 Subject: [PATCH 6/6] Revert back to the upstream version --- isort/main.py | 4 ++-- isort/output.py | 11 +---------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/isort/main.py b/isort/main.py index df5793080..e8af743fe 100644 --- a/isort/main.py +++ b/isort/main.py @@ -705,8 +705,8 @@ def _build_arg_parser() -> argparse.ArgumentParser: dest="only_sections", action="store_true", help="Causes imports to be sorted based on their sections like STDLIB,THIRDPARTY etc. " - "Within sections, the imports maintain their relative positions irrespective of their " - "import_type", + "Within sections, the imports are ordered by their import style and the imports with " + "same style maintain their relative positions.", ) section_group.add_argument( "--ds", diff --git a/isort/output.py b/isort/output.py index 3286256f1..76f2637d4 100644 --- a/isort/output.py +++ b/isort/output.py @@ -87,16 +87,7 @@ def sorted_imports( lines_between = [""] * ( config.lines_between_types if from_modules and straight_modules else 0 ) - - if config.only_sections: - section_output = [] - for section_import_type in parsed.original_order[section]: - if section_import_type == "from": - section_output.append(from_imports.pop(0)) - else: - section_output.append(straight_imports.pop(0)) - - elif config.from_first: + if config.from_first: section_output = from_imports + lines_between + straight_imports else: section_output = straight_imports + lines_between + from_imports