diff --git a/CHANGELOG.md b/CHANGELOG.md index 354f0b482..2f548b3d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard. ### 5.3.0 TBD - Implemented ability to treat all or select comments as code (issue #1357) - Implemented ability to use different configs for different file extensions (issue #1162) + - Implemented ability to specify the types of imports (issue #1181) - Added experimental support for sorting literals (issue #1358) - Added experimental support for sorting and deduping groupings of assignments. - Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363) diff --git a/isort/settings.py b/isort/settings.py index 7f8bbe23d..00a67d4da 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -183,6 +183,9 @@ class _Config: treat_all_comments_as_code: bool = False supported_extensions: FrozenSet[str] = SUPPORTED_EXTENSIONS blocked_extensions: FrozenSet[str] = BLOCKED_EXTENSIONS + constants: FrozenSet[str] = frozenset() + classes: FrozenSet[str] = frozenset() + variables: FrozenSet[str] = frozenset() def __post_init__(self): py_version = self.py_version diff --git a/isort/sorting.py b/isort/sorting.py index ea5cafc8b..4d010a11a 100644 --- a/isort/sorting.py +++ b/isort/sorting.py @@ -26,9 +26,15 @@ def module_key( module_name = str(module_name) if sub_imports and config.order_by_type: - if module_name.isupper() and len(module_name) > 1: # see issue #376 + if module_name in config.constants: prefix = "A" - elif module_name[0:1].isupper(): + elif module_name in config.classes: + prefix = "B" + elif module_name in config.variables: + prefix = "C" + elif module_name.isupper() and len(module_name) > 1: # see issue #376 + prefix = "A" + elif module_name in config.classes or module_name[0:1].isupper(): prefix = "B" else: prefix = "C" diff --git a/tests/test_ticketed_features.py b/tests/test_ticketed_features.py index 7061a9e1f..2253cecf3 100644 --- a/tests/test_ticketed_features.py +++ b/tests/test_ticketed_features.py @@ -483,3 +483,27 @@ def method(): # isort: dict y = {"b": "c", "z": "z"}""" ) + + +def test_isort_allows_setting_import_types_issue_1181(): + """Test to ensure isort provides a way to set the type of imports. + See: https://github.com/timothycrosley/isort/issues/1181 + """ + assert isort.code("from x import AA, Big, variable") == "from x import AA, Big, variable\n" + assert ( + isort.code("from x import AA, Big, variable", constants=["variable"]) + == "from x import AA, variable, Big\n" + ) + assert ( + isort.code("from x import AA, Big, variable", variables=["AA"]) + == "from x import Big, AA, variable\n" + ) + assert ( + isort.code( + "from x import AA, Big, variable", + constants=["Big"], + variables=["AA"], + classes=["variable"], + ) + == "from x import Big, variable, AA\n" + )